import 'package:flutter/cupertino.dart';
import 'package:flutter/material.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) {
    final controller = Get.put(MainController());

    return MyScaffold(
      title: 'Welcome Drivers'.tr,
      isleading: true,
      body: [
        GetBuilder<MainController>(builder: (mainController) {
          final drivers = mainController.newDriverRegister;

          if (drivers.isEmpty) {
            return const Padding(
              padding: EdgeInsets.all(32.0),
              child: Center(
                child: Text(
                  'No new drivers found.',
                  style: TextStyle(fontSize: 18),
                ),
              ),
            );
          }

          return CupertinoScrollbar(
            child: ListView.builder(
              padding: const EdgeInsets.only(bottom: 20),
              itemCount: drivers.length,
              itemBuilder: (context, index) {
                final driver = drivers[index];
                return DriverCard(driver: driver);
              },
            ),
          );
        }),
      ],
    );
  }
}

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

  const DriverCard({super.key, required this.driver});
  Widget buildActionButton({
    required IconData icon,
    required Color color,
    required VoidCallback onPressed,
  }) {
    return CupertinoButton(
      padding: const EdgeInsets.symmetric(horizontal: 6.0),
      onPressed: onPressed,
      child: Icon(icon, color: color, size: 28),
    );
  }

  @override
  Widget build(BuildContext context) {
    final controller = Get.find<MainController>();

    return CupertinoCard(
      margin: const EdgeInsets.all(16.0),
      child: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            // حالة التلوين حسب isCall
            Container(
              decoration: AppStyle.boxDecoration1.copyWith(
                color: driver['isCall'].toString() == '1'
                    ? AppColor.greenColor
                    : AppColor.accentColor,
              ),
              padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 4),
              child: Text(
                'Driver Information'.tr,
                style: CupertinoTheme.of(context).textTheme.navTitleTextStyle,
              ),
            ),
            const SizedBox(height: 12),

            InfoText(
                'Name'.tr, driver['first_name'] + ' ' + driver['last_name']),
            InfoText('Phone'.tr, driver['phone']),
            InfoText('Email'.tr, driver['email']),
            InfoText('License Type'.tr, driver['license_type']),
            InfoText('License Categories'.tr, driver['license_categories']),
            InfoText('National Number'.tr, driver['national_number']),
            InfoText('Occupation'.tr, driver['occupation']),
            const SizedBox(height: 12),

            Text('Notes:'.tr,
                style: CupertinoTheme.of(context).textTheme.navTitleTextStyle),
            const SizedBox(height: 8),

            CupertinoTextField(
              controller: controller.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(
              children: [
                Expanded(
                  child: MyElevatedButton(
                    title: 'Call Driver'.tr,
                    onPressed: () {
                      controller.makePhoneCall(driver['phone'].toString());
                    },
                  ),
                ),
                const SizedBox(width: 16),
                Expanded(
                  child: CupertinoButton(
                    onPressed: () async {
                      await controller.addWelcomeCall(driver['id'].toString());
                    },
                    child: Text('Save Changes'.tr),
                  ),
                ),
                Expanded(
                  child: CupertinoButton(
                    onPressed: () async {
                      final phone = driver['phone'];

                      if (phone == null || phone.toString().isEmpty) {
                        Get.snackbar("خطأ", "لا يوجد رقم هاتف لهذا السائق");
                        return;
                      }

                      String message = "مرحباً،\n\n"
                          "يعطيك العافية أستاذ. نرحب بك في شركة *انطلق* للنقل الذكي.\n"
                          "نود تعريفك بالتطبيق ومميزاته لتتمكن من الاستفادة الكاملة وبدء العمل معنا بسهولة.\n\n"
                          "لأي استفسار أو مساعدة، يمكنك التواصل معنا على الأرقام التالية:\n\n"
                          "+963 952 475 742\n"
                          "+963 952 475 740\n"
                          "+963 952 475 734\n\n"
                          "فريق انطلق يتمنى لك تجربة موفقة ويوم سعيد.";

                      Get.find<MainController>().launchCommunication(
                        'whatsapp',
                        phone,
                        message,
                      );
                    },
                    child: Text('send'.tr),
                  ),
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }
}

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

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

  @override
  Widget build(BuildContext context) {
    final display = value?.toString() ?? 'N/A';
    return Padding(
      padding: const EdgeInsets.symmetric(vertical: 2.0),
      child: Text(
        '$label: $display',
        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,
    );
  }
}
