import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';
import 'package:service/constant/colors.dart';
import 'package:service/constant/style.dart';
import 'package:service/views/widgets/elevated_btn.dart';
import 'package:service/views/widgets/my_scafold.dart';

import '../main_controller.dart';

class WelcomeCall extends StatelessWidget {
  const WelcomeCall({super.key});

  @override
  Widget build(BuildContext context) {
    Get.put(MainController());
    return MyScaffold(
      title: 'Welcome Drivers'.tr,
      isleading: true,
      body: [
        GetBuilder<MainController>(builder: (mainController) {
          return Expanded(
            child: CupertinoScrollbar(
              child: ListView.builder(
                itemCount: mainController.newDriverRegister.length,
                itemBuilder: (context, index) {
                  final driver = mainController.newDriverRegister[index];
                  return DriverCard(driver: driver);
                },
              ),
            ),
          );
        }),
      ],
    );
  }
}

class DriverCard extends StatelessWidget {
  final Map<String, dynamic> driver;

  const DriverCard({super.key, required this.driver});

  @override
  Widget build(BuildContext context) {
    return CupertinoCard(
      margin: const EdgeInsets.all(16.0),
      child: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Container(
              decoration: AppStyle.boxDecoration1.copyWith(
                  color: driver['isCall'].toString() == '1'
                      ? AppColor.greenColor
                      : AppColor.accentColor),
              child: Padding(
                padding:
                    const EdgeInsets.symmetric(horizontal: 10, vertical: 2),
                child: Text(
                  'Driver Information'.tr,
                  style: CupertinoTheme.of(context).textTheme.navTitleTextStyle,
                ),
              ),
            ),
            const SizedBox(height: 16),
            InfoText('Name'.tr, driver['name_arabic'].toString()),
            InfoText('Phone'.tr, driver['phone'].toString()),
            InfoText('Email'.tr, driver['email'].toString()),
            InfoText('License Type'.tr, driver['license_type'].toString()),
            InfoText(
                'License Categories'.tr, driver['license_categories'] ?? ''),
            InfoText(
                'National Number'.tr, driver['national_number'].toString()),
            InfoText('Occupation'.tr, driver['occupation'].toString()),
            const SizedBox(height: 16),
            Text(
              'Notes:'.tr,
              style: CupertinoTheme.of(context).textTheme.navTitleTextStyle,
            ),
            const SizedBox(height: 8),
            CupertinoTextField(
              controller: Get.find<MainController>().notesController,
              placeholder: driver['notes'] ?? 'Enter notes here...'.tr,
              maxLines: 3,
              padding: const EdgeInsets.all(12.0),
              decoration: BoxDecoration(
                border: Border.all(color: CupertinoColors.systemGrey),
                borderRadius: BorderRadius.circular(8.0),
              ),
            ),
            const SizedBox(height: 16),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                SizedBox(
                    width: Get.width * .4,
                    child: MyElevatedButton(
                        title: 'Call Driver'.tr,
                        onPressed: () {
                          Get.find<MainController>()
                              .makePhoneCall(driver['phone'].toString());
                        })),
                CupertinoButton(
                  onPressed: () async {
                    await Get.find<MainController>().addWelcomeCall(
                      driver['id'].toString(),
                    );
                  },
                  child: Text('Save Changes'.tr),
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }
}

class InfoText extends StatelessWidget {
  final String label;
  final String value;

  const InfoText(this.label, this.value, {super.key});

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.only(bottom: 4.0),
      child: Text(
        '$label: $value',
        style: CupertinoTheme.of(context).textTheme.textStyle,
      ),
    );
  }
}

class CupertinoCard extends StatelessWidget {
  final Widget child;
  final EdgeInsetsGeometry margin;

  const CupertinoCard(
      {super.key, required this.child, this.margin = EdgeInsets.zero});

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: margin,
      decoration: BoxDecoration(
        color: CupertinoColors.systemBackground,
        borderRadius: BorderRadius.circular(12.0),
        boxShadow: [
          BoxShadow(
            color: CupertinoColors.systemGrey.withOpacity(0.2),
            spreadRadius: 1,
            blurRadius: 5,
            offset: const Offset(0, 3),
          ),
        ],
      ),
      child: child,
    );
  }
}
