import 'package:hmg_patient_app/core/enum/viewstate.dart';
import 'package:hmg_patient_app/core/model/blooddonation/blood_groub_details.dart';
import 'package:hmg_patient_app/core/model/blooddonation/get_all_cities.dart';
import 'package:hmg_patient_app/core/model/hospitals/hospitals_model.dart';
import 'package:hmg_patient_app/core/model/my_balance/AdvanceModel.dart';
import 'package:hmg_patient_app/core/model/my_balance/patient_advance_balance_amount.dart';
import 'package:hmg_patient_app/core/model/my_balance/patient_info.dart';
import 'package:hmg_patient_app/core/model/my_balance/patient_info_and_mobile_number.dart';
import 'package:hmg_patient_app/core/service/blood/blood_details_servies.dart';
import 'package:hmg_patient_app/core/service/blood/blood_donation_service.dart';
import 'package:hmg_patient_app/core/service/hospital_service.dart';
import 'package:hmg_patient_app/core/service/medical/my_balance_service.dart';
import 'package:hmg_patient_app/core/viewModels/base_view_model.dart';
import 'package:hmg_patient_app/locator.dart';
import 'package:hmg_patient_app/models/FamilyFiles/GetAllSharedRecordByStatusResponse.dart';
import 'package:hmg_patient_app/uitl/app_toast.dart';

class MyBalanceViewModel extends BaseViewModel {
  MyBalanceService _myBalanceService = locator<MyBalanceService>();

  HospitalService _hospitalService = locator<HospitalService>();

  List<HospitalsModel> get hospitals => _hospitalService.hospitals;

  List<PatientAdvanceBalanceAmount> get patientAdvanceBalanceAmountList =>
      _myBalanceService.patientAdvanceBalanceAmountList;

  //========================
  BloodDonationService _bloodDonationService = locator<BloodDonationService>();

  List<CitiesModel> get CitiesModelList =>
      _bloodDonationService.CitiesModelList;
  BloodDetailsService _bloodDetailsService = locator<BloodDetailsService>();

  List<List_BloodGroupDetailsModel> get bloodDetailsModelList =>
      _bloodDetailsService
          .BloodModelList; //_bloodDonationService.CitiesModelList;

  //===========================

  dynamic get totalAdvanceBalanceAmount =>
      _myBalanceService.totalAdvanceBalanceAmount;

  String get updatedRegisterBloodMessage =>
      _myBalanceService.updatedRegisterBloodMessage;

  GetAllSharedRecordsByStatusResponse get getAllSharedRecordsByStatusResponse =>
      _myBalanceService.getAllSharedRecordsByStatusResponse;

  List<PatientInfo> get patientInfoList => _myBalanceService.patientInfoList;

  PatientInfoAndMobileNumber get patientInfoAndMobileNumber =>
      _myBalanceService.patientInfoAndMobileNumber!;

  List<List_BloodGroupDetailsModel> get bloodModelList =>
      _bloodDetailsService.BloodModelList;

  String get logInTokenID => _myBalanceService.logInTokenID!;

  String get verificationCode => _myBalanceService.verificationCode!;

  getPatientAdvanceBalanceAmount() async {
    setState(ViewState.Busy);
    await _myBalanceService.getPatientAdvanceBalanceAmount();
    if (_myBalanceService.hasError) {
      error = _myBalanceService.error!;
      setState(ViewState.Error);
    } else {
      setState(ViewState.Idle);
    }
  }

  Future getHospitals(int languageID, {bool isAdvancePayment = false}) async {
    setState(ViewState.Busy);
    await _hospitalService.getHospitals(languageID, isAdvancePayment: isAdvancePayment);
    if (_hospitalService.hasError) {
      error = _hospitalService.error!;
      setState(ViewState.Error);
    } else
      setState(ViewState.Idle);
  }

  Future getCities() async {
    if(isLogin) {
      setState(ViewState.Busy);
      await _bloodDonationService.getAllCitiesOrders();
      if (_bloodDonationService.hasError) {
        error = _bloodDonationService.error!;
        setState(ViewState.Error);
      } else
        setState(ViewState.Idle);
    }
  }

  Future getBlood() async {
    setState(ViewState.Busy);
    await _bloodDetailsService.getAllBloodOrders();
    if (_bloodDetailsService.hasError) {
      error = _bloodDetailsService.error!;
      setState(ViewState.Error);
    } else
      setState(ViewState.Idle);
  }

  Future getPatientInfoByPatientID({required String id}) async {
    setState(ViewState.Busy);
    await _myBalanceService.getPatientInfoByPatientID(id: id);
    if (_myBalanceService.hasError) {
      error = _myBalanceService.error!;
      setState(ViewState.ErrorLocal);
      AppToast.showErrorToast(message: error);
    } else {
      setState(ViewState.Idle);
    }
  }

  Future getPatientInfoByPatientIDAndMobileNumber(
      AdvanceModel advanceModel) async {
    setState(ViewState.Busy);
    await _myBalanceService
        .getPatientInfoByPatientIDAndMobileNumber(advanceModel);
    if (_myBalanceService.hasError) {
      error = _myBalanceService.error!;
      setState(ViewState.ErrorLocal);
      AppToast.showErrorToast(message: error);
    } else {
      setState(ViewState.Idle);
    }
  }

  Future sendActivationCodeForAdvancePayment(
      {required int patientID, required int projectID}) async {
    setState(ViewState.Busy);
    await _myBalanceService.sendActivationCodeForAdvancePayment(
        patientID: patientID, projectID: projectID);
    if (_myBalanceService.hasError) {
      error = _myBalanceService.error!;
      setState(ViewState.ErrorLocal);
      AppToast.showErrorToast(message: error);
    } else {
      setState(ViewState.Idle);
    }
  }

  Future checkActivationCodeForAdvancePayment(
      {required String activationCode, String? patientMobileNumber}) async {
    setState(ViewState.Busy);
    await _myBalanceService.checkActivationCodeForAdvancePayment(
        activationCode: activationCode);
    if (_myBalanceService.hasError) {
      error = _myBalanceService.error!;
      setState(ViewState.ErrorLocal);
    } else {
      setState(ViewState.Idle);
    }
  }

  Future getFamilyFiles() async {
    setState(ViewState.Busy);
    await _myBalanceService.getFamilyFiles();
    if (_myBalanceService.hasError) {
      error = _myBalanceService.error!;
      setState(ViewState.ErrorLocal);
      AppToast.showErrorToast(message: error);
    } else {
      setState(ViewState.Idle);
    }
  }

  Future updateBloodGroup(List_BloodGroupDetailsModel detailsModel) async {
    setState(ViewState.Busy);
    await _myBalanceService.updateBloodGroup(detailsModel);
    if (_myBalanceService.hasError) {
      error = _myBalanceService.error!;
      setState(ViewState.ErrorLocal);
    } else {
      await _myBalanceService.addUserAgreementForBloodDonation();
      setState(ViewState.Idle);
    }
  }
}
