import 'dart:developer';

import 'package:easy_localization/easy_localization.dart';
import 'package:hmg_patient_app_new/core/common_models/nationality_country_model.dart';
import 'package:hmg_patient_app_new/core/dependencies.dart';
import 'package:hmg_patient_app_new/core/enums.dart';
import 'package:hmg_patient_app_new/core/utils/utils.dart';
import 'package:hmg_patient_app_new/generated/locale_keys.g.dart';
import 'package:hmg_patient_app_new/services/dialog_service.dart';

class ValidationUtils {
  static final DialogService _dialogService = getIt.get<DialogService>();

  static bool isValidatePhoneAndId({String? nationalId, String? phoneNumber}) {
    log("phoneNumber: $phoneNumber");
    log("nationalId: $nationalId");
    if (nationalId == null || nationalId.isEmpty) {
      _dialogService.showErrorBottomSheet(message: LocaleKeys.enterValidNationalId.tr(), onOkPressed: () {});
      return false;
    }

    if (phoneNumber == null || phoneNumber.isEmpty) {
      _dialogService.showErrorBottomSheet(message: LocaleKeys.enterValidPhoneNumber.tr(), onOkPressed: () {});
      return false;
    }
    return true;
  }

  static bool isValidatedId({String? nationalId, required Function() onOkPress, CountryEnum? selectedCountry, bool? isTermsAccepted, String? dob}) {
    bool isCorrectID = true;
    if (nationalId == null || nationalId.isEmpty) {
      _dialogService.showExceptionBottomSheet(message: "Please enter a national ID", onOkPressed: onOkPress);
      isCorrectID = false;
    }

    if (nationalId != null && nationalId.isNotEmpty && selectedCountry != null) {
      if (selectedCountry == CountryEnum.saudiArabia) {
        if (!validateIqama(nationalId)) {
          _dialogService.showExceptionBottomSheet(message: "Please enter a valid Iqama ID", onOkPressed: onOkPress);
          return false;
        }
      }

      if (selectedCountry == CountryEnum.unitedArabEmirates) {
        if (!validateUaeNationalId(nationalId)) {
          _dialogService.showExceptionBottomSheet(message: "Please enter a valid national ID", onOkPressed: onOkPress);
          return false;
        }
      }

      if (dob == null || dob.isEmpty) {
        _dialogService.showExceptionBottomSheet(message: "Please enter a valid date of birth", onOkPressed: onOkPress);
        return false;
      }

      if (isTermsAccepted != null && !isTermsAccepted) {
        _dialogService.showExceptionBottomSheet(message: "Please accept the terms and conditions", onOkPressed: onOkPress);
        return false;
      }
    }
    return isCorrectID;
  }

  static bool isValidatePhone({String? phoneNumber, required Function() onOkPress}) {
    if (phoneNumber == null || phoneNumber.isEmpty) {
      _dialogService.showExceptionBottomSheet(message: "Please enter a valid phone number", onOkPressed: onOkPress);
      return false;
    }
    return true;
  }

  static bool isValidate({String? phoneNumber, required Function() onOkPress}) {
    if (phoneNumber == null || phoneNumber.isEmpty) {
      _dialogService.showExceptionBottomSheet(message: "Please enter a valid phone number", onOkPressed: onOkPress);
      return false;
    }
    return true;
  }

  static bool validateIqama(String iqamaNumber) {
    String cleanedIqama = iqamaNumber.replaceAll(RegExp(r'[^0-9]'), '');
    if (cleanedIqama.length != 10) {
      return false;
    }
    int firstDigit = int.parse(cleanedIqama[0]);
    if (firstDigit != 2 && firstDigit != 1) {
      return false;
    }
    int sum = 0;
    for (int i = 0; i < 10; i++) {
      int digit = int.parse(cleanedIqama[i]);
      int weight = (i % 2 == 0) ? 2 : 1; // Alternate weights: 2, 1, 2, 1...
      int product = digit * weight;
      sum += (product > 9) ? product - 9 : product; // Sum digits if product > 9
    }
    return sum % 10 == 0;
  }

  static bool validateUaeNationalId(String id) {
    // Must be exactly 15 digits
    final regex = RegExp(r'^784\d{4}\d{7}\d{1}$');
    return regex.hasMatch(id);
  }

  static bool validateUaeRegistration({String? name, GenderTypeEnum? gender, NationalityCountries? country, MaritalStatusTypeEnum? maritalStatus, required Function() onOkPress}) {
    if (name == null || name.isEmpty) {
      _dialogService.showExceptionBottomSheet(message: "Please enter a valid name", onOkPressed: onOkPress);
      return false;
    }

    if (gender == null) {
      _dialogService.showExceptionBottomSheet(message: "Please select a gender", onOkPressed: onOkPress);
      return false;
    }

    if (maritalStatus == null) {
      _dialogService.showExceptionBottomSheet(message: "Please select a marital status", onOkPressed: onOkPress);
      return false;
    }

    if (country == null) {
      _dialogService.showExceptionBottomSheet(message: "Please select a country", onOkPressed: onOkPress);
      return false;
    }

    return true;
  }

  static bool isValidateEmail({String? email, required Function() onOkPress}) {
    if (email == null || email.isEmpty) {
      _dialogService.showExceptionBottomSheet(message: "Please enter email", onOkPressed: onOkPress);
      return false;
    }
    final bool emailIsValid = RegExp(r"^[a-zA-Z0-9.a-zA-Z0-9.!#$%&'*+-/=?^_`{|}~]+@[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,}$").hasMatch(email);

    if (!emailIsValid) {
      _dialogService.showExceptionBottomSheet(message: "Please enter a valid email format", onOkPressed: onOkPress);
      return false;
    }

    return true;
  }
}
