import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:hmg_patient_app_new/features/habib_wallet/habib_wallet_repo.dart';
import 'package:hmg_patient_app_new/features/my_appointments/models/resp_models/hospital_model.dart';
import 'package:hmg_patient_app_new/generated/locale_keys.g.dart';
import 'package:hmg_patient_app_new/services/error_handler_service.dart';

class HabibWalletViewModel extends ChangeNotifier {
  bool isWalletAmountLoading = false;
  num habibWalletAmount = 0;
  num walletRechargeAmount = 0;

  bool isBottomSheetContentLoading = false;

  bool isSearchedFileNumberDataShown = false;

  int currentIndex = 0;

  List<HospitalsModel> advancePaymentHospitals = [];

  HospitalsModel? selectedHospital;

  HabibWalletRepo habibWalletRepo;
  ErrorHandlerService errorHandlerService;

  String fileNumber = '';
  String depositorName = '';
  String mobileNumber = '';

  num selectedRechargeType = 1;

  HabibWalletViewModel({required this.habibWalletRepo, required this.errorHandlerService});

  initHabibWalletProvider() {
    isWalletAmountLoading = true;
    isBottomSheetContentLoading = false;
    habibWalletAmount = 0;
    walletRechargeAmount = 0;
    selectedRechargeType = 1;
    advancePaymentHospitals.clear();
    selectedHospital = null;
    fileNumber = '';
    depositorName = '';
    mobileNumber = '';
    notifyListeners();
  }

  setSelectedRechargeType(num type) {
    selectedRechargeType = type;
    notifyListeners();
  }

  setSelectedHospital(HospitalsModel hospital) {
    selectedHospital = hospital;
    notifyListeners();
  }

  setWalletRechargeAmount(num amount) {
    walletRechargeAmount = amount;
    notifyListeners();
  }

  setDepositorDetails(String fileNum, String depositor, String mobile) {
    fileNumber = fileNum;
    depositorName = depositor;
    mobileNumber = mobile;
    notifyListeners();
  }

  setCurrentIndex(int index) {
    currentIndex = index;
    notifyListeners();
  }

  String getSelectedRechargeTypeValue() {
    switch (selectedRechargeType) {
      case 1:
        return LocaleKeys.myAccount.tr();
      case 2:
        return LocaleKeys.family.tr();
      case 3:
        return LocaleKeys.otherAccount.tr();
      default:
        return LocaleKeys.myAccount.tr();
    }
  }

  Future<void> getPatientBalanceAmount({Function(dynamic)? onSuccess, Function(String)? onError}) async {
    isWalletAmountLoading = true;
    notifyListeners();

    final result = await habibWalletRepo.getPatientBalanceAmount();

    result.fold(
      (failure) async => await errorHandlerService.handleError(failure: failure),
      (apiResponse) {
        if (apiResponse.messageStatus == 2) {
          // dialogService.showErrorDialog(message: apiResponse.errorMessage!, onOkPressed: () {});
        } else if (apiResponse.messageStatus == 1) {
          habibWalletAmount = apiResponse.data!;
          isWalletAmountLoading = false;
          notifyListeners();
          if (onSuccess != null) {
            onSuccess(apiResponse);
          }
        }
      },
    );
  }

  Future<void> getProjectsList() async {
    advancePaymentHospitals.clear();
    notifyListeners();
    final result = await habibWalletRepo.getProjectList();

    result.fold(
      (failure) async => await errorHandlerService.handleError(failure: failure),
      (apiResponse) async {
        if (apiResponse.messageStatus == 2) {
          // dialogService.showErrorDialog(message: apiResponse.errorMessage!, onOkPressed: () {});
        } else if (apiResponse.messageStatus == 1) {
          advancePaymentHospitals = apiResponse.data!;
          notifyListeners();
        }
      },
    );
  }

  Future<void> HISCreateAdvancePayment(
      {required String paymentMethodName,
      required num paidAmount,
      required String paymentReference,
      required String patientID,
      required int projectID,
      required String depositorName,
      Function(dynamic)? onSuccess,
      Function(String)? onError}) async {
    final result = await habibWalletRepo.HISCreateAdvancePayment(
        paymentMethodName: paymentMethodName, paidAmount: paidAmount, paymentReference: paymentReference, patientID: patientID, projectID: projectID, depositorName: depositorName);

    result.fold(
      (failure) async => await errorHandlerService.handleError(failure: failure),
      (apiResponse) {
        if (apiResponse.messageStatus == 2) {
          // dialogService.showErrorDialog(message: apiResponse.errorMessage!, onOkPressed: () {});
        } else if (apiResponse.messageStatus == 1) {
          notifyListeners();
          if (onSuccess != null) {
            onSuccess(apiResponse);
          }
        }
      },
    );
  }

  Future<void> addAdvanceNumberRequest({required String advanceNumber, required String paymentReference, Function(dynamic)? onSuccess, Function(String)? onError}) async {
    final result = await habibWalletRepo.addAdvanceNumberRequest(advanceNumber: advanceNumber, paymentReference: paymentReference);

    result.fold(
      (failure) async => await errorHandlerService.handleError(failure: failure),
      (apiResponse) {
        if (apiResponse.messageStatus == 2) {
          // dialogService.showErrorDialog(message: apiResponse.errorMessage!, onOkPressed: () {});
        } else if (apiResponse.messageStatus == 1) {
          notifyListeners();
          if (onSuccess != null) {
            onSuccess(apiResponse);
          }
        }
      },
    );
  }

  Future<void> getPatientInfoByPatientID({required String patientID, Function(dynamic)? onSuccess, Function(String)? onError}) async {
    isBottomSheetContentLoading = true;
    notifyListeners();
    final result = await habibWalletRepo.getPatientInfoByPatientID(patientID: patientID);

    result.fold(
      (failure) async {
        isBottomSheetContentLoading = false;
        notifyListeners();
        await errorHandlerService.handleError(failure: failure);
      },
      (apiResponse) {
        if (apiResponse.messageStatus == 2) {
          // dialogService.showErrorDialog(message: apiResponse.errorMessage!, onOkPressed: () {});
        } else if (apiResponse.messageStatus == 1) {
          isBottomSheetContentLoading = false;
          notifyListeners();
          if (onSuccess != null) {
            onSuccess(apiResponse);
          }
        }
      },
    );
  }
}
