import 'package:diplomaticquarterapp/core/model/AlHabibMedicalService/ComprehensiveMedicalCheckup/GetCMCAllOrdersResponseModel.dart';
import 'package:diplomaticquarterapp/core/model/prescriptions/perscription_pharmacy.dart';
import 'package:diplomaticquarterapp/core/model/prescriptions/prescription_info_rc_model.dart';
import 'package:diplomaticquarterapp/core/model/prescriptions/prescription_report.dart';
import 'package:diplomaticquarterapp/core/model/prescriptions/prescription_report_enh.dart';
import 'package:diplomaticquarterapp/core/model/prescriptions/prescription_report_inp.dart';
import 'package:diplomaticquarterapp/core/model/prescriptions/prescriptions_order.dart';
import 'package:diplomaticquarterapp/uitl/app_toast.dart';
import 'package:flutter/cupertino.dart';

import '../../../core/enum/filter_type.dart';
import '../../../core/enum/viewstate.dart';
import '../../../core/model/prescriptions/Prescriptions.dart';
import '../../../core/service/medical/prescriptions_service.dart';
import '../../../locator.dart';
import '../base_view_model.dart';

class PrescriptionsViewModel extends BaseViewModel {
  FilterType filterType = FilterType.Clinic;
  PrescriptionsService _prescriptionsService = locator<PrescriptionsService>();

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

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



  List<PrescriptionReportINP> get prescriptionReportListINP => _prescriptionsService.prescriptionReportListINP;

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

  List<GetCMCAllOrdersResponseModel> get prescriptionsHistory => _prescriptionsService.prescriptionsOrderList;

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

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

  List<PrescriptionInfoRCModel> get prescriptionsOrderListRC => _prescriptionsService.prescriptionsOrderListRC;

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

  List<PrescriptionsList> prescriptionsOrderListByValue(filterValue) {
    return filterValue == FilterType.Clinic ? _prescriptionsOrderListClinic : _prescriptionsOrderListHospital;
  }

  get isMedDeliveryAllowed => _prescriptionsService.isMedDeliveryAllowed;
  getPrescriptions() async {
    setState(ViewState.Busy);
    await _prescriptionsService.getPrescriptions();
    if (_prescriptionsService.hasError) {
      error = _prescriptionsService.error;
      setState(ViewState.Error);
    } else {
      _filterList();
//      await getPrescriptionsOrders();
      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));
      }
    });
  }

  getPrescriptionsOrders({bool showLoading = false}) async {
    setState(ViewState.Busy);
    Future.delayed(new Duration(milliseconds: 300)).then((value) async {
      // await _prescriptionsService.getPrescriptionsOrders();
      await _prescriptionsService.getPrescriptionsOrdersRC();
      if (_prescriptionsService.hasError) {
        error = _prescriptionsService.error;
        setState(ViewState.ErrorLocal);
      } else {
        setState(ViewState.Idle);
      }
    });
  }

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

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

  sendPrescriptionEmail({String appointmentDate, int patientID, String clinicName, String doctorName, int doctorID, String mes, int projectID}) async {
    setState(ViewState.BusyLocal);
    await _prescriptionsService.sendPrescriptionEmail(appointmentDate, patientID, clinicName, doctorName, doctorID, projectID);
    if (_prescriptionsService.hasError) {
      error = _prescriptionsService.error;
      setState(ViewState.ErrorLocal);
      AppToast.showErrorToast(message: error);
    } else {
      AppToast.showSuccessToast(message: mes);
      setState(ViewState.Idle);
    }
  }

  getListPharmacyForPrescriptions({int itemId}) async {
    setState(ViewState.Busy);
    await _prescriptionsService.getListPharmacyForPrescriptions(itemId: itemId);
    if (_prescriptionsService.hasError) {
      error = _prescriptionsService.error;
      setState(ViewState.ErrorLocal);
    } else {
      setState(ViewState.Idle);
    }
  }

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

  getPrescriptionReportDetailsRC(int orderID, dynamic patientID) async {
    setState(ViewState.Busy);
    await _prescriptionsService.getPrescriptionsInfoRC(orderID, patientID);
    if (_prescriptionsService.hasError) {
      error = _prescriptionsService.error;
      setState(ViewState.Error);
    } else {
      setState(ViewState.Idle);
    }
  }

  Future updatePressOrder({@required int presOrderID}) async {
    setState(ViewState.Busy);
    // await _prescriptionsService.updatePressOrder(presOrderID: presOrderID);
    await _prescriptionsService.updatePressOrderRC(presOrderID: presOrderID);
    if (_prescriptionsService.hasError) {
      error = _prescriptionsService.error;
      setState(ViewState.Error);
    } else {
      await getPrescriptions();
    }
  }
}
