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/get_medication_response_model.dart';
import 'package:doctor_app_flutter/core/model/medical_file_model.dart';
import 'package:doctor_app_flutter/core/model/post_prescrition_req_model.dart';
import 'package:doctor_app_flutter/core/model/search_drug_model.dart';
import 'package:doctor_app_flutter/core/service/prescription_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/SOAP/master_key_model.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';

class PrescriptionViewModel extends BaseViewModel {
  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;

  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() async {
    setState(ViewState.Busy);
    await _prescriptionService.getMedicationList();
    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);
  }
}
