import 'dart:io';

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:siro_service/constant/colors.dart';
import 'package:siro_service/constant/links.dart';
import 'package:siro_service/controller/functions/crud.dart';
import 'package:siro_service/controller/mainController/pages/driver_page.dart';
import 'package:siro_service/main.dart';
import 'package:siro_service/views/widgets/my_dialog.dart';
import 'package:url_launcher/url_launcher.dart';

import '../../print.dart';
import 'pages/passengers_page.dart';

class MainController extends GetxController {
  final formKey = GlobalKey<FormState>();
  bool isLoading = false;
  final passengerPhoneController = TextEditingController();
  final driverPhoneController = TextEditingController();
  final notesController = TextEditingController();
  final carplateController = TextEditingController();
  TextEditingController colorController = TextEditingController();
  TextEditingController makeController = TextEditingController();
  TextEditingController modelController = TextEditingController();
  TextEditingController expirationDateController = TextEditingController();
  TextEditingController yearController = TextEditingController();
  TextEditingController ownerController = TextEditingController();
  TextEditingController carOwnerWorkController = TextEditingController();
  TextEditingController driverNameController = TextEditingController();
  TextEditingController nationalIdController = TextEditingController();
  TextEditingController birthDateController = TextEditingController();
  TextEditingController licenseTypeController = TextEditingController();
  TextEditingController phoneController = TextEditingController();
  TextEditingController phoneCarController = TextEditingController();
  TextEditingController carNumberController = TextEditingController();
  TextEditingController manufactureYearController = TextEditingController();
  TextEditingController carModelController = TextEditingController();
  TextEditingController carTypeController = TextEditingController();
  TextEditingController siteCarController = TextEditingController();
  TextEditingController siteDriverController = TextEditingController();
  TextEditingController registrationDateController = TextEditingController();
  Map passengerData = {};
  Map driverData = {};
  List filteredDrivers = [];
  var color = ''.obs;
  var colorHex = ''.obs;

  @override
  void onInit() {
    super.onInit();
    // refreshDashboardStats(); // Removed to save data consumption at start
  }

  Future<void> refreshDashboardStats() async {
    isLoading = true;
    update();
    try {
      await Future.wait<void>([
        getDriverWantCompleteRegistration(),
        getDriverNotCompleteRegistration(),
        getNewDriverRegister(),
      ]);
    } catch (e) {
      Log.print('Error refreshing stats: $e');
    }
    isLoading = false;
    update();
  }

  Future<void> searchPassengerByPhone() async {
    if (formKey.currentState == null || formKey.currentState!.validate()) {
      isLoading = true;
      update();
      await getPassengersByPhone();
      isLoading = false;
      update();
      Get.back();
      if (passengerData.isEmpty) {
        Get.snackbar('Error'.tr, 'Passenger not found'.tr,
            backgroundColor: Colors.red, colorText: Colors.white);
        return;
      }
      Get.to(() => PassengersPage());
    }
  }

  void searchDrivers(String query) {
    if (query.isEmpty) {
      filteredDrivers = driverNotCompleteRegistration;
      update();
    } else {
      filteredDrivers = driverNotCompleteRegistration
          .where((driver) => driver['phone_number']
              .toString()
              .toLowerCase()
              .contains(query.toLowerCase()))
          .toList();
      update();
    }
  }

  void updateDriverField(String key, dynamic value) async {
    // Update locally
    driverData['message'][0][key] = value;
    Log.print('driverData: ${driverData['message'][0]['driverID']}');
    update();

    var res = await CRUD().post(link: AppLink.updateDriver, payload: {
      'driverID': driverData['message'][0]['driverID'].toString(),
      key: value.toString(),
    });
    if (res == 'failure') {
      Get.snackbar('Error', 'Failed to update driver data',
          backgroundColor: AppColor.redColor);
    } else {
      Get.snackbar('Success', 'Driver data updated successfully',
          backgroundColor: AppColor.greenColor);
    }
    // Optionally fetch driver again
    // await getDriverData();
  }

  Future<void> makePhoneCall(String phoneNumber) async {
    final Uri launchUri = Uri(
      scheme: 'tel',
      path: phoneNumber,
    );
    await launchUrl(launchUri);
  }

  Future<void> launchCommunication(
      String method, String contactInfo, String message) async {
    // رقّم فقط (بدون + أو مسافات)
    final phone = contactInfo.replaceAll(RegExp(r'[^0-9]'), '');
    final encodedMsg = Uri.encodeComponent(message);

    Uri? uri;

    if (Platform.isIOS) {
      switch (method) {
        case 'phone':
          uri = Uri.parse('tel:$phone');
          break;
        case 'sms':
          uri = Uri.parse('sms:$phone?body=$encodedMsg');
          break;
        case 'whatsapp':
          uri = Uri.parse(
              'https://api.whatsapp.com/send?phone=$phone&text=$encodedMsg');
          break;
        case 'email':
          uri =
              Uri.parse('mailto:$contactInfo?subject=Subject&body=$encodedMsg');
          break;
        default:
          return;
      }
    } else if (Platform.isAndroid) {
      switch (method) {
        case 'phone':
          uri = Uri.parse('tel:$phone');
          break;
        case 'sms':
          uri = Uri.parse('sms:$phone?body=$encodedMsg');
          break;
        case 'whatsapp':
          {
            final waDeepLink =
                Uri.parse('whatsapp://send?phone=$phone&text=$encodedMsg');
            if (await canLaunchUrl(waDeepLink)) {
              await launchUrl(waDeepLink, mode: LaunchMode.externalApplication);
              return;
            } else {
              final webUri = Uri.parse(
                  'https://api.whatsapp.com/send?phone=$phone&text=$encodedMsg');
              if (await canLaunchUrl(webUri)) {
                await launchUrl(webUri, mode: LaunchMode.externalApplication);
                return;
              }
              // لو ما في متصفح أساسًا
              throw 'No handler for WhatsApp links';
            }
          }
        case 'email':
          uri =
              Uri.parse('mailto:$contactInfo?subject=Subject&body=$encodedMsg');
          break;
        default:
          return;
      }
    } else {
      return;
    }

    final ok = await canLaunchUrl(uri);
    if (ok) {
      await launchUrl(uri, mode: LaunchMode.externalApplication);
    } else {
      // ممكن تضيف Snackbar/Toast هنا
    }
  }

  List driverNotCompleteRegistration = [];
  Future<void> getDriverNotCompleteRegistration() async {
    var res = await CRUD()
        .get(link: AppLink.getDriverNotCompleteRegistration, payload: {});
    if (res != 'failure') {
      var d = res['message'];
      driverNotCompleteRegistration = d;
      filteredDrivers = driverNotCompleteRegistration;
      update();
    } else {
      driverNotCompleteRegistration = [];
      update();
    }
  }

  Future<void> deleteDriverNotCompleteRegistration(String phone) async {
    var res = await CRUD()
        .get(link: AppLink.deleteDriverNotCompleteRegistration, payload: {
      'phone': phone,
    });
    if (res != 'failure') {
      Get.snackbar(res, '', backgroundColor: AppColor.greenColor);
      // await getDriverWantCompleteRegistration();
      update();
    } else {
      Get.snackbar(res, '');
    }
  }

  List driverWantCompleteRegistration = [];
  Future<void> getDriverWantCompleteRegistration() async {
    var res =
        await CRUD().get(link: AppLink.getDriversWaitingActive, payload: {});
    if (res != 'failure') {
      var d = res['message'];
      driverWantCompleteRegistration = d;
      filteredDrivers = driverWantCompleteRegistration;
      update();
    } else {
      driverWantCompleteRegistration = [];
      update();
    }
  }

  List driversPhoneNotComplete = [];
  Future<void> getDriversPhoneNotComplete() async {
    var res =
        await CRUD().get(link: AppLink.getDriversPhoneNotComplete, payload: {});
    if (res != 'failure') {
      var d = res['message'];
      driverWantCompleteRegistration = d;
      filteredDrivers = driverWantCompleteRegistration;
      update();
    } else {
      Get.snackbar(res, '');
    }
  }

  List newDriverRegister = [];
  Future<void> getNewDriverRegister() async {
    var res = await CRUD().get(link: AppLink.getNewDriverRegister, payload: {});
    if (res != 'failure') {
      var d = res['message'];
      newDriverRegister = d;
      update();
    } else {
      newDriverRegister = [];
      update();
    }
  }

  Future<void> addWelcomeCall(String driveId) async {
    var res = await CRUD().post(link: AppLink.addWelcomeDriverNote, payload: {
      "driverId": driveId,
      "notes": notesController.text,
    });
    if (res != 'failure') {
      Get.snackbar('Success'.tr, '', backgroundColor: AppColor.greenColor);
    }
  }

  String selectedStatus = "I'm not ready yet".tr;
  List passengerNotCompleteRegistration = [];
  Future<void> getPassengerNotCompleteRegistration() async {
    var res = await CRUD()
        .get(link: AppLink.getPassengersNotCompleteRegistration, payload: {});
    if (res != 'failure') {
      var d = res['message'];
      passengerNotCompleteRegistration = d;
      update();
    } else {
      Get.snackbar(res, '');
    }
  }

  void setSelectedStatus(String status) {
    selectedStatus = status;
    update();
  }

  final List<String> statusOptions = [
    "I'm not ready yet".tr,
    "I don't have a suitable vehicle".tr,
    "I'll register when the app is fully launched".tr,
    "I need more help understanding the app".tr,
    "My documents have expired".tr,
  ];

  List carPlateNotEdit = [];

  getCarPlateNotEdit() async {
    var res = await CRUD().get(link: AppLink.getCarPlateNotEdit, payload: {});
    if (res != 'failure') {
      var d = res['message'];
      carPlateNotEdit = d;
      update();
    } else {
      MyDialog().getDialog('No Car found yet'.tr, 'thanks'.tr, const SizedBox(),
          () {
        Get.back();
      });
    }
  }

  List driverWithoutCar = [];
  getdriverWithoutCar() async {
    var res = await CRUD().get(link: AppLink.getdriverWithoutCar, payload: {});
    if (res != 'failure') {
      var d = res['message'];
      driverWithoutCar = d;
      update();
    } else {
      MyDialog().getDialog('No Car found yet'.tr, 'thanks'.tr, const SizedBox(),
          () {
        Get.back();
      });
    }
  }

  Future<void> addRegistrationCarEgyptHandling({
    required String driverId,
    required String carPlate,
    required String color,
    required String colorHex,
    required String year,
    required String make,
    required String model,
    required String expirationDate,
    required String owner,
  }) async {
    try {
      isLoading = true;
      update();

      var payload = {
        'driverID': driverId,
        'vin': 'vin',
        'car_plate': carPlate,
        'make': make,
        'model': model,
        'year': year,
        'expiration_date': expirationDate,
        'color': color,
        'owner': owner,
        'color_hex': colorHex,
        'address': 'addressCar',
        'displacement': 'displacement',
        'fuel': 'fuel',
        'registration_date': '2024-09-06',
      };

      Log.print('Payload: $payload');

      var res =
          await CRUD().post(link: AppLink.addCartoDriver, payload: payload);

      isLoading = false;
      update();

      var status = res;
      Log.print('res: $res');
      Log.print('status: $status');

      if (status != 'failure' && status['status'] == 'success') {
        await Future.wait([
          CRUD().post(
              link:
                  '${AppLink.seferAlexandriaServer}/ride/RegisrationCar/add.php',
              payload: payload),
          CRUD().post(
              link: '${AppLink.seferGizaServer}/ride/RegisrationCar/add.php',
              payload: payload),
        ]);

        Get.snackbar('Success', 'Registration successful',
            backgroundColor: AppColor.greenColor);
        Get.back();
      } else {
        Log.print('Error: Unexpected status: ${status['status']}');
        Get.snackbar('Error', 'Registration failed',
            backgroundColor: Colors.red);
      }
    } catch (e) {
      Log.print('Error: $e');
      Get.snackbar('Error', 'An error occurred during registration',
          backgroundColor: Colors.red);
    }
  }

  editCarPlateNotEdit(
    String driverId,
    String carPlate,
    String color,
    String colorHex,
    String year,
    String make,
    String model,
    String expirationDate,
    String owner,
  ) async {
    var res = await CRUD().post(link: AppLink.editCarPlate, payload: {
      "driverId": driverId,
      "carPlate": carPlate,
      "color": color,
      "color_hex": colorHex,
      "make": make,
      "year": year,
      "model": model,
      "expiration_date": expirationDate.toString(),
      "owner": owner,
      "employee": storage.read(key: 'name').toString(),
    });
    Log.print('res: ${res}');
    if (res != 'failure') {
      // Get.snackbar(res, '', backgroundColor: AppColor.greenColor);
      Get.back();
      carplateController.clear();
      yearController.clear();
      makeController.clear();
      modelController.clear();
      ownerController.clear();

      await getCarPlateNotEdit();
      update();
    } else {
      Get.snackbar(res, '', backgroundColor: AppColor.redColor);
    }
  }

  // editCarPlateNotEdit(String driverId, carPlate) async {
  //   var res = await CRUD().post(link: AppLink.editCarPlate, payload: {
  //     "driverId": driverId,
  //     "carPlate": carPlate,
  //   });
  //   if (res != 'failure') {
  //     Get.snackbar(res, '', backgroundColor: AppColor.greenColor);
  //     carplateController.clear();
  //     await getCarPlateNotEdit();
  //     update();
  //   } else {
  //     Get.snackbar(res, '', backgroundColor: AppColor.redColor);
  //   }
  // }

  saveNoteForDriverNotCompleteRegistration(String phone, editor, note) async {
    var res = await CRUD().post(
        link: AppLink.addNotesDriver,
        payload: {"phone": phone, "editor": editor, "note": note});
    if (res != 'failure') {
      Get.snackbar(res, '', backgroundColor: AppColor.greenColor);
      getDriversPhoneNotComplete();
      notesController.clear();
    } else {
      Get.snackbar(res, '', backgroundColor: AppColor.redColor);
    }
  }

  saveNoteForPassengerNotCompleteRegistration(
      String phone, editor, note) async {
    var res = await CRUD().post(
        link: AppLink.addNotesPassenger,
        payload: {"phone": phone, "editor": editor, "note": note});
    if (res != 'failure') {
      Get.snackbar(res, '', backgroundColor: AppColor.greenColor);
      notesController.clear();
    } else {
      Get.snackbar(res, '', backgroundColor: AppColor.redColor);
    }
  }

  searchDriverByPhone() async {
    if (formKey.currentState == null || formKey.currentState!.validate()) {
      isLoading = true;
      update();
      await getDriverByPhone();
      isLoading = false;
      update();
      Get.back();
      if (driverData.isEmpty) {
        Get.snackbar('Error'.tr, 'Driver not found'.tr,
            backgroundColor: Colors.red, colorText: Colors.white);
        return;
      }
      Get.to(() => DriverPage());
    }
  }

  searchDriverByNational() async {
    if (formKey.currentState == null || formKey.currentState!.validate()) {
      isLoading = true;
      update();
      await getDriverByNational();
      isLoading = false;
      update();
      Get.back();
      if (driverData.isEmpty) {
        Get.snackbar('Error'.tr, 'Driver not found'.tr,
            backgroundColor: Colors.red, colorText: Colors.white);
        return;
      }
      Get.to(() => DriverPage());
    }
  }

  getPassengersByPhone() async {
    var res = await CRUD().get(
        link: AppLink.getPassengersByPhone,
        payload: {"phone": passengerPhoneController.text});

    if (res != 'failure') {
      var d = res;
      passengerData = d;
      update();
    }
  }

  getDriverByPhone() async {
    var res = await CRUD().get(
        link: AppLink.getDriverByPhone,
        payload: {"phone": driverPhoneController.text});

    if (res != 'failure') {
      var d = res;
      driverData = d;
      update();
    } else {
      Get.snackbar('Error', 'Driver not found', backgroundColor: Colors.red);
    }
  }

  getDriverByNational() async {
    var res = await CRUD().get(
        link: AppLink.getDriverByNational,
        payload: {"national_number": driverPhoneController.text});

    if (res != 'failure') {
      var d = res;
      driverData = d;
      update();
    } else {
      Get.snackbar('Error', 'Driver not found', backgroundColor: Colors.red);
    }
  }
}
