import 'package:doctor_app_flutter/core/enum/filter_type.dart';
import 'package:doctor_app_flutter/core/enum/viewstate.dart';
import 'package:doctor_app_flutter/core/model/Prescription_model.dart';
import 'package:doctor_app_flutter/core/model/Prescriptions/Prescriptions.dart';
import 'package:doctor_app_flutter/core/model/Prescriptions/perscription_pharmacy.dart';
import 'package:doctor_app_flutter/core/model/Prescriptions/prescription_in_patient.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/Prescriptions/prescriptions_order.dart';
import 'package:doctor_app_flutter/core/model/get_medication_response_model.dart';
import 'package:doctor_app_flutter/core/model/post_prescrition_req_model.dart';
import 'package:doctor_app_flutter/core/service/prescription_service.dart';
import 'package:doctor_app_flutter/core/service/prescriptions_service.dart';
import 'package:doctor_app_flutter/core/viewModel/base_view_model.dart';
import 'package:doctor_app_flutter/locator.dart';
import 'package:doctor_app_flutter/models/SOAP/GetAllergiesResModel.dart';
import 'package:doctor_app_flutter/models/SOAP/GetAssessmentResModel.dart';
import 'package:doctor_app_flutter/models/patient/patiant_info_model.dart';
import 'package:doctor_app_flutter/models/patient/vital_sign/patient-vital-sign-data.dart';
import 'package:flutter/cupertino.dart';

class PrescriptionViewModel extends BaseViewModel {
  FilterType filterType = FilterType.Clinic;
  bool hasError = false;
  PrescriptionService _prescriptionService = locator<PrescriptionService>();
  List<GetMedicationResponseModel> get allMedicationList =>
      _prescriptionService.allMedicationList;

  List<PrescriptionModel> get prescriptionList =>
      _prescriptionService.prescriptionList;
  List<dynamic> get drugsList => _prescriptionService.doctorsList;
  //List<dynamic> get allMedicationList => _prescriptionService.allMedicationList;
  List<dynamic> get drugToDrug => _prescriptionService.drugToDrugList;

  List<dynamic> get itemMedicineList => _prescriptionService.itemMedicineList;
  PrescriptionsService _prescriptionsService = locator<PrescriptionsService>();

  List<PrescriptionsList> _prescriptionsOrderListClinic = List();
  List<PrescriptionsList> _prescriptionsOrderListHospital = List();

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

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

  List<PharmacyPrescriptions> get pharmacyPrescriptionsList =>
      _prescriptionsService.pharmacyPrescriptionsList;
  List<PrescriptionReportEnh> get prescriptionReportEnhList =>
      _prescriptionsService.prescriptionReportEnhList;

  List<PrescriptionsList> get prescriptionsOrderList =>
      filterType == FilterType.Clinic
          ? _prescriptionsOrderListClinic
          : _prescriptionsOrderListHospital;

  List<PrescriotionInPatient> get inPatientPrescription =>
      _prescriptionsService.prescriptionInPatientList;

  getPrescriptionsInPatient(PatiantInformtion patient) async {
    setState(ViewState.Busy);
    await _prescriptionsService.getPrescriptionInPatient(
        mrn: patient.patientId, adn: patient.admissionNo);
    if (_prescriptionsService.hasError) {
      error = _prescriptionsService.error;
      setState(ViewState.Error);
    } else {
      _filterList();
      await _getPrescriptionsOrders();

      setState(ViewState.Idle);
    }
  }

  Future getItem({int itemID}) async {
    hasError = false;
    //_insuranceCardService.clearInsuranceCard();
    setState(ViewState.BusyLocal);
    await _prescriptionService.getItem(itemID: itemID);
    if (_prescriptionService.hasError) {
      error = _prescriptionService.error;
      setState(ViewState.ErrorLocal);
    } else
      setState(ViewState.Idle);
  }

  Future getPrescription({int mrn}) async {
    hasError = false;
    //_insuranceCardService.clearInsuranceCard();
    setState(ViewState.Busy);
    await _prescriptionService.getPrescription(mrn: mrn);
    if (_prescriptionService.hasError) {
      error = _prescriptionService.error;
      setState(ViewState.ErrorLocal);
    } else
      setState(ViewState.Idle);
  }

  Future postPrescription(
      PostPrescriptionReqModel postProcedureReqModel, int mrn) async {
    hasError = false;
    //_insuranceCardService.clearInsuranceCard();
    setState(ViewState.Busy);
    await _prescriptionService.postPrescription(postProcedureReqModel);
    if (_prescriptionService.hasError) {
      error = _prescriptionService.error;
      setState(ViewState.ErrorLocal);
    } else {
      await getPrescription(mrn: mrn);
      setState(ViewState.Idle);
    }
  }

  Future getMedicationList({String drug}) async {
    setState(ViewState.Busy);
    await _prescriptionService.getMedicationList(drug: drug);
    if (_prescriptionService.hasError) {
      error = _prescriptionService.error;
      setState(ViewState.Error);
    } else
      setState(ViewState.Idle);
  }

  Future updatePrescription(
      PostPrescriptionReqModel updatePrescriptionReqModel, int mrn) async {
    hasError = false;
    //_insuranceCardService.clearInsuranceCard();
    setState(ViewState.Busy);
    await _prescriptionService.updatePrescription(updatePrescriptionReqModel);
    if (_prescriptionService.hasError) {
      error = _prescriptionService.error;
      setState(ViewState.ErrorLocal);
    } else {
      await getPrescription(mrn: mrn);
      setState(ViewState.Idle);
    }
  }

  Future getDrugs({String drugName}) async {
    hasError = false;
    //_insuranceCardService.clearInsuranceCard();
    setState(ViewState.BusyLocal);
    await _prescriptionService.getDrugs(drugName: drugName);
    if (_prescriptionService.hasError) {
      error = _prescriptionService.error;
      setState(ViewState.ErrorLocal);
    } else
      setState(ViewState.Idle);
  }

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

  setFilterType(FilterType filterType) {
    this.filterType = filterType;
    notifyListeners();
  }

  getPrescriptionReport(
      {Prescriptions prescriptions,
      @required PatiantInformtion patient}) async {
    setState(ViewState.Busy);
    await _prescriptionsService.getPrescriptionReport(
        prescriptions: prescriptions, patient: patient);
    if (_prescriptionsService.hasError) {
      error = _prescriptionsService.error;
      setState(ViewState.ErrorLocal);
    } else {
      setState(ViewState.Idle);
    }
  }

  getListPharmacyForPrescriptions(
      {int itemId, @required PatiantInformtion patient}) async {
    setState(ViewState.Busy);
    await _prescriptionsService.getListPharmacyForPrescriptions(
        itemId: itemId, patient: patient);
    if (_prescriptionsService.hasError) {
      error = _prescriptionsService.error;
      setState(ViewState.Error);
    } else {
      setState(ViewState.Idle);
    }
  }

  void _filterList() {
    _prescriptionsService.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));
      }
    });
  }

  getPrescriptionReportEnh(
      {PrescriptionsOrder prescriptionsOrder,
      @required PatiantInformtion patient}) async {
    setState(ViewState.Busy);
    await _prescriptionsService.getPrescriptionReportEnh(
        prescriptionsOrder: prescriptionsOrder, patient: patient);
    if (_prescriptionsService.hasError) {
      error = _prescriptionsService.error;
      setState(ViewState.Error);
    } else {
      setState(ViewState.Idle);
    }
  }

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

  getPrescriptions(PatiantInformtion patient, {String patientType}) async {
    setState(ViewState.Busy);
    await _prescriptionsService.getPrescriptions(patient);
    if (_prescriptionsService.hasError) {
      error = _prescriptionsService.error;
      if (patientType == "7")
        setState(ViewState.ErrorLocal);
      else
        setState(ViewState.Error);
    } else {
      _filterList();
      await _getPrescriptionsOrders();

      setState(ViewState.Idle);
    }
  }
}
