import 'package:doctor_app_flutter/core/enum/filter_type.dart';
import 'package:doctor_app_flutter/core/enum/view_state.dart';
import 'package:doctor_app_flutter/core/model/Prescriptions/Prescriptions.dart';
import 'package:doctor_app_flutter/core/model/Prescriptions/get_medication_for_inpatient_model.dart';
import 'package:doctor_app_flutter/core/model/Prescriptions/perscription_pharmacy.dart';
import 'package:doctor_app_flutter/core/model/Prescriptions/post_prescrition_req_model.dart';
import 'package:doctor_app_flutter/core/model/Prescriptions/prescription_entity_model.dart';
import 'package:doctor_app_flutter/core/model/Prescriptions/prescription_model.dart';
import 'package:doctor_app_flutter/core/model/Prescriptions/prescription_report.dart';
import 'package:doctor_app_flutter/core/model/Prescriptions/prescription_report_enh.dart';
import 'package:doctor_app_flutter/core/model/SOAP/Allergy/get_allergies_res_model.dart';
import 'package:doctor_app_flutter/core/model/SOAP/Assessment/get_assessment_res_model.dart';
import 'package:doctor_app_flutter/core/model/SOAP/master_key_model.dart';
import 'package:doctor_app_flutter/core/model/patient/patiant_info_model.dart';
import 'package:doctor_app_flutter/core/model/patient/vital_sign/patient-vital-sign-data.dart';
import 'package:doctor_app_flutter/core/service/patient_medical_file/prescription/prescription_service.dart';
import 'package:doctor_app_flutter/core/viewModel/base_view_model.dart';
import 'package:doctor_app_flutter/locator.dart';
import 'package:flutter/cupertino.dart';

class PrescriptionViewModel extends BaseViewModel {
  FilterType filterType = FilterType.Clinic;
  bool hasError = false;

  PrescriptionService _prescriptionService = locator<PrescriptionService>();

  List<GetMedicationForInPatientModel> get medicationForInPatient => _prescriptionService.medicationForInPatient;

  List<PrescriptionModel> get prescriptionList => _prescriptionService.prescriptionList;

  List<PrescriptionEntityModel> get prescriptionListNew => _prescriptionService.prescriptionListNew;

  List<PrescriptionReport> get prescriptionReportList => _prescriptionService.prescriptionReportList;

  List<PharmacyPrescriptions> get pharmacyPrescriptionsList => _prescriptionService.pharmacyPrescriptionsList;

  List<PrescriptionReportEnh> get prescriptionReportEnhList => _prescriptionService.prescriptionReportEnhList;

  List<dynamic> get drugToDrug => _prescriptionService.drugToDrugList;

  List<PrescriptionsList> _prescriptionsOrderListClinic = [];
  List<PrescriptionsList> _prescriptionsOrderListHospital = [];

  List<Prescriptions> get prescriptionsList => _prescriptionService.prescriptionsList;

  List<MasterKeyModel> get icd10DeseaseItems =>_prescriptionService.icd10CodeListByItem;
  bool get isPrincipalCovered_ => _prescriptionService.isDiagnosisCovered;
  Future getItem({int? itemID}) async {
    hasError = false;
    setState(ViewState.BusyLocal);
    await _prescriptionService.getItem(itemID: itemID!);
    if (_prescriptionService.hasError) {
      error = _prescriptionService.error!;
      setState(ViewState.ErrorLocal);
    } else
      setState(ViewState.Idle);
  }

  Future getPrescriptionListNew({int? mrn, int appNo = 0, bool isLocalBusy = false, int? episodeId}) async {
    hasError = false;
    if (isLocalBusy)
      setState(ViewState.BusyLocal);
    else
      setState(ViewState.Busy);
    await _prescriptionService.getPrescriptionListNew(mrn: mrn!, appNo: appNo, episodeID: episodeId);
    if (_prescriptionService.hasError) {
      error = _prescriptionService.error!;
      setState(ViewState.ErrorLocal);
    } else
      setState(ViewState.Idle);
  }

  Future postPrescription(PostPrescriptionReqModel postProcedureReqModel, int mrn) async {
    hasError = false;
    setState(ViewState.BusyLocal);
    await _prescriptionService.postPrescription(postProcedureReqModel);
    if (_prescriptionService.hasError) {
      error = _prescriptionService.error!;
      setState(ViewState.ErrorLocal);
    } else {
      await getPrescriptionListNew(mrn: mrn, episodeId: postProcedureReqModel.episodeID);
      setState(ViewState.Idle);
    }
  }

  Future getDrugToDrug(VitalSignData? vital, List<GetAssessmentResModel> lstAssessments, List<GetAllergiesResModel> allergy, PatiantInformtion patient, List<dynamic> prescription,
      {bool isLocalBusy = false}) async {
    hasError = false;
    // setState(ViewState.BusyLocal);
    if (isLocalBusy)
      setState(ViewState.BusyLocal);
    else
      setState(ViewState.Busy);
    await _prescriptionService.getDrugToDrug(vital, lstAssessments, allergy, patient, prescription);
    if (_prescriptionService.hasError) {
      error = _prescriptionService.error!;
      setState(ViewState.ErrorLocal);
    } else
      setState(ViewState.Idle);
  }

  void _filterList() {
    _prescriptionService.prescriptionsList.forEach((element) {
      ///  PrescriptionsList list sort clinic
      List<PrescriptionsList> prescriptionsByClinic = _prescriptionsOrderListClinic.where((elementClinic) => elementClinic.filterName == element.clinicDescription).toList();

      if (prescriptionsByClinic.length != 0) {
        _prescriptionsOrderListClinic[_prescriptionsOrderListClinic.indexOf(prescriptionsByClinic[0])].prescriptionsList!.add(element);
      } else {
        _prescriptionsOrderListClinic.add(PrescriptionsList(filterName: element.clinicDescription, prescriptions: element));
      }

      ///  PrescriptionsList list sort via hospital
      List<PrescriptionsList> prescriptionsByHospital = _prescriptionsOrderListHospital
          .where(
            (elementClinic) => elementClinic.filterName == element.name,
          )
          .toList();

      if (prescriptionsByHospital.length != 0) {
        _prescriptionsOrderListHospital[_prescriptionsOrderListHospital.indexOf(prescriptionsByHospital[0])].prescriptionsList!.add(element);
      } else {
        _prescriptionsOrderListHospital.add(PrescriptionsList(filterName: element.name, prescriptions: element));
      }
    });
  }

  _getPrescriptionsOrders() async {
    await _prescriptionService.getPrescriptionsOrders();
    if (_prescriptionService.hasError) {
      error = _prescriptionService.error!;
      setState(ViewState.ErrorLocal);
    } else {
      setState(ViewState.Idle);
    }
  }

  getPrescriptions(PatiantInformtion patient, {String? patientType, bool isLocalBusy = true}) async {
    if (isLocalBusy)
      setState(ViewState.BusyLocal);
    else
      setState(ViewState.Busy);
    await _prescriptionService.getPrescriptions(patient);
    if (_prescriptionService.hasError) {
      error = _prescriptionService.error!;
      if (isLocalBusy)
        setState(ViewState.ErrorLocal);
      else
        setState(ViewState.Error);
    } else {
      _filterList();
      await _getPrescriptionsOrders();

      setState(ViewState.Idle);
    }
  }

  getMedicationForInPatient(PatiantInformtion patient, bool isVidaPlusProject) async {
    setState(ViewState.Busy);
    await _prescriptionService.getMedicationForInPatient(patient, isVidaPlusProject: isVidaPlusProject);
    if (_prescriptionService.hasError) {
      error = _prescriptionService.error!;
      setState(ViewState.Error);
    } else {
      setState(ViewState.Idle);
    }
  }

  getPrescriptionReport({Prescriptions? prescriptions, required PatiantInformtion patient}) async {
    setState(ViewState.Busy);
    await _prescriptionService.getPrescriptionReport(prescriptions: prescriptions!, patient: patient);
    if (_prescriptionService.hasError) {
      error = _prescriptionService.error!;
      setState(ViewState.ErrorLocal);
    } else {
      setState(ViewState.Idle);
    }
  }
 Future isPrincipalCovered(
      {
        required PatiantInformtion patient}) async {
    setState(ViewState.Busy);
    await _prescriptionService.isPrincipalCovered(patient);
    if (_prescriptionService.hasError) {
      error = _prescriptionService.error;
      setState(ViewState.ErrorLocal);
    } else {
      setState(ViewState.Idle);
    }
  }
  Future getIcd10DISESECode(
      {
        required PatiantInformtion patient, int? itemID}) async {
    setState(ViewState.Busy);
    await _prescriptionService.getIcd10DISESECode(patient, itemID!);
    if (_prescriptionService.hasError) {
      error = _prescriptionService.error;
      setState(ViewState.ErrorLocal);
    } else {
      setState(ViewState.Idle);
    }
  }

}
