import 'package:doctor_app_flutter/core/enum/master_lookup_key.dart';
import 'package:doctor_app_flutter/core/enum/viewstate.dart';
import 'package:doctor_app_flutter/core/model/get_medication_response_model.dart';
import 'package:doctor_app_flutter/core/service/SOAP_service.dart';
import 'package:doctor_app_flutter/core/service/medicine_service.dart';
import 'package:doctor_app_flutter/core/service/prescription_service.dart';
import 'package:doctor_app_flutter/models/SOAP/GetAssessmentReqModel.dart';
import 'package:doctor_app_flutter/models/SOAP/GetAssessmentResModel.dart';

import '../../locator.dart';
import 'base_view_model.dart';

class MedicineViewModel extends BaseViewModel {
  MedicineService _medicineService = locator<MedicineService>();
  PrescriptionService _prescriptionService = locator<PrescriptionService>();

  get pharmacyItemsList => _medicineService.pharmacyItemsList;
  get searchText=>_medicineService.searchText;
  get pharmaciesList => _medicineService.pharmaciesList;

  get medicationStrengthList => _prescriptionService.medicationStrengthList;

  get medicationDurationList => _prescriptionService.medicationDurationList;

  get medicationRouteList => _prescriptionService.medicationRouteList;

  get medicationFrequencyList => _prescriptionService.medicationFrequencyList;
  get boxQuintity => _prescriptionService.boxQuantity;

  get medicationIndicationsList =>
      _prescriptionService.medicationIndicationsList;

  get medicationDoseTimeList => _prescriptionService.medicationDoseTimeList;
  List<GetAssessmentResModel> get patientAssessmentList =>
      _prescriptionService.patientAssessmentList;


  List<GetMedicationResponseModel> get allMedicationList =>
      _prescriptionService.allMedicationList;
  List<dynamic> get itemMedicineList => _prescriptionService.itemMedicineList;
  List<dynamic> get itemMedicineListRoute =>
      _prescriptionService.itemMedicineListRoute;
  List<dynamic> get itemMedicineListUnit =>
      _prescriptionService.itemMedicineListUnit;

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

  Future getMedicineItem(String itemName) async {
    setState(ViewState.Busy);
    await _medicineService.getMedicineItem(itemName);
    if (_medicineService.hasError) {
      error = _medicineService.error;
      setState(ViewState.Error);
    } else
      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 getPatientAssessment(
      GetAssessmentReqModel getAssessmentReqModel) async {
    setState(ViewState.Busy);
    await _prescriptionService.getPatientAssessment(getAssessmentReqModel);
    if (_prescriptionService.hasError) {
      error = _prescriptionService.error;
      setState(ViewState.Error);
    } else
      setState(ViewState.Idle);
  }

  Future getMedicationStrength() async {
    setState(ViewState.Busy);
    await _prescriptionService
        .getMasterLookup(MasterKeysService.MedicationStrength);
    if (_prescriptionService.hasError) {
      error = _prescriptionService.error;
      setState(ViewState.Error);
    } else
      setState(ViewState.Idle);
  }

  Future getMedicationRoute() async {
    setState(ViewState.Busy);
    await _prescriptionService
        .getMasterLookup(MasterKeysService.MedicationRoute);
    if (_prescriptionService.hasError) {
      error = _prescriptionService.error;
      setState(ViewState.Error);
    } else
      setState(ViewState.Idle);
  }

  Future getMedicationIndications() async {
    setState(ViewState.Busy);
    await _prescriptionService
        .getMasterLookup(MasterKeysService.MedicationIndications);
    if (_prescriptionService.hasError) {
      error = _prescriptionService.error;
      setState(ViewState.Error);
    } else
      setState(ViewState.Idle);
  }

  Future getMedicationDoseTime() async {
    setState(ViewState.Busy);
    await _prescriptionService
        .getMasterLookup(MasterKeysService.MedicationDoseTime);
    if (_prescriptionService.hasError) {
      error = _prescriptionService.error;
      setState(ViewState.Error);
    } else
      setState(ViewState.Idle);
  }

  Future getMedicationFrequency() async {
    setState(ViewState.Busy);
    await _prescriptionService
        .getMasterLookup(MasterKeysService.MedicationFrequency);
    if (_prescriptionService.hasError) {
      error = _prescriptionService.error;
      setState(ViewState.Error);
    } else
      setState(ViewState.Idle);
  }

  Future getMedicationDuration() async {
    setState(ViewState.Busy);
    await _prescriptionService
        .getMasterLookup(MasterKeysService.MedicationDuration);
    if (_prescriptionService.hasError) {
      error = _prescriptionService.error;
      setState(ViewState.Error);
    } else
      setState(ViewState.Idle);
  }

  Future getBoxQuantity(
      {int itemCode, int duration, double strength, int freq}) async {
    setState(ViewState.Busy);
    await _prescriptionService.calculateBoxQuantity(
        strength: strength, itemCode: itemCode, duration: duration, freq: freq);
    if (_prescriptionService.hasError) {
      error = _prescriptionService.error;
      setState(ViewState.Error);
    } else
      setState(ViewState.Idle);
  }

  Future getPharmaciesList(int itemId) async {
    setState(ViewState.Busy);
    await _medicineService.getPharmaciesList(itemId);
    if (_medicineService.hasError) {
      error = _medicineService.error;
      setState(ViewState.Error);
    } else
      setState(ViewState.Idle);
  }

  dynamic getLookupById(List<dynamic> list, String id) {
    for (int i = 0; i < list.length; i++) {
      if (list[i]['id'].toString() == id) {
        return list[i];
      }
    }
    return null;
  }

  dynamic getLookupByIdFilter(List<dynamic> list, String id) {
    for (int i = 0; i < list.length; i++) {
      if (list[i]['parameterCode'].toString() == id) {
        return list[i];
      }
    }
    return null;
  }
}
