import 'package:doctor_app_flutter/core/enum/viewstate.dart';
import 'package:doctor_app_flutter/core/service/patient_service.dart';
import 'package:doctor_app_flutter/models/patient/lab_orders/lab_orders_res_model.dart';
import 'package:doctor_app_flutter/models/patient/lab_result/lab_result.dart';
import 'package:doctor_app_flutter/models/patient/prescription/prescription_report.dart';
import 'package:doctor_app_flutter/models/patient/prescription/prescription_report_for_in_patient.dart';
import 'package:doctor_app_flutter/models/patient/prescription/prescription_res_model.dart';
import 'package:doctor_app_flutter/models/patient/radiology/radiology_res_model.dart';
import 'package:doctor_app_flutter/models/patient/vital_sign/vital_sign_res_model.dart';

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

class PatientViewModel extends BaseViewModel {
  PatientService _patientService = locator<PatientService>();

  List<VitalSignResModel> get patientVitalSignList =>
      _patientService.patientVitalSignList;

  List<VitalSignResModel> get patientVitalSignOrderdSubList =>
      _patientService.patientVitalSignOrderdSubList;

  List<LabOrdersResModel> get patientLabResultOrdersList =>
      _patientService.patientLabResultOrdersList;

  List<PrescriptionResModel> get patientPrescriptionsList =>
      _patientService.patientPrescriptionsList;

  List<PrescriptionReportForInPatient> get prescriptionReportForInPatientList =>
      _patientService.prescriptionReportForInPatientList;

  List<PrescriptionReport> get prescriptionReport =>
      _patientService.prescriptionReport;

  List<RadiologyResModel> get patientRadiologyList =>
      _patientService.patientRadiologyList;

  List<LabResult> get labResultList => _patientService.labResultList;

  get insuranceApporvalsList => _patientService.insuranceApporvalsList;

  get patientProgressNoteList => _patientService.patientProgressNoteList;

  List<dynamic> get clinicsList => _patientService.clinicsList;

  List<dynamic> get doctorsList => _patientService.doctorsList;

  List<dynamic> get referralFrequencyList =>
      _patientService.referalFrequancyList;

  Future getPatientList(patient, patientType,

      {bool isBusyLocal = false}) async {
     var localRes ;
    if (isBusyLocal) {
      setState(ViewState.BusyLocal);
    } else {
      setState(ViewState.Busy);
    }
    localRes = await _patientService.getPatientList(patient, patientType);

    if (_patientService.hasError) {
      error = _patientService.error;
      setState(ViewState.Error);

    }
    return localRes;

  }

  Future getPatientVitalSign(patient) async {
    setState(ViewState.Busy);
    await _patientService.getPatientVitalSign(patient);
    if (_patientService.hasError) {
      error = _patientService.error;
      setState(ViewState.Error);
    } else
      setState(ViewState.Idle);
  }

  Future getLabResultOrders(patient) async {
    setState(ViewState.Busy);
    await _patientService.getLabResultOrders(patient);
    if (_patientService.hasError) {
      error = _patientService.error;
      setState(ViewState.Error);
    } else
      setState(ViewState.Idle);
  }

  Future getOutPatientPrescriptions(patient) async {
    setState(ViewState.Busy);
    await _patientService.getOutPatientPrescriptions(patient);
    if (_patientService.hasError) {
      error = _patientService.error;
      setState(ViewState.Error);
    } else
      setState(ViewState.Idle);
  }

  Future getInPatientPrescriptions(patient) async {
    setState(ViewState.Busy);
    await _patientService.getInPatientPrescriptions(patient);
    if (_patientService.hasError) {
      error = _patientService.error;
      setState(ViewState.Error);
    } else
      setState(ViewState.Idle);
  }

  Future getPrescriptionReport(patient) async {
    setState(ViewState.Busy);
    await _patientService.getPrescriptionReport(patient);
    if (_patientService.hasError) {
      error = _patientService.error;
      setState(ViewState.Error);
    } else
      setState(ViewState.Idle);
  }

  Future getPatientRadiology(patient) async {
    setState(ViewState.Busy);
    await _patientService.getPatientRadiology(patient);
    if (_patientService.hasError) {
      error = _patientService.error;
      setState(ViewState.Error);
    } else
      setState(ViewState.Idle);
  }

  Future getLabResult(LabOrdersResModel labOrdersResModel) async {
    setState(ViewState.Busy);
    await _patientService.getLabResult(labOrdersResModel);
    if (_patientService.hasError) {
      error = _patientService.error;
      setState(ViewState.Error);
    } else
      setState(ViewState.Idle);
  }

  Future getPatientInsuranceApprovals(patient) async {
    setState(ViewState.Busy);
    await _patientService.getPatientInsuranceApprovals(patient);
    if (_patientService.hasError) {
      error = _patientService.error;
      setState(ViewState.Error);
    } else
      setState(ViewState.Idle);
  }

  Future getPatientProgressNote(patient) async {
    setState(ViewState.Busy);
    await _patientService.getPatientProgressNote(patient);
    if (_patientService.hasError) {
      error = _patientService.error;
      setState(ViewState.Error);
    } else
      setState(ViewState.Idle);
  }

  Future getClinicsList() async {
    setState(ViewState.Busy);
    await _patientService.getClinicsList();
    if (_patientService.hasError) {
      error = _patientService.error;
      setState(ViewState.Error);
    } else {
      {
        await getReferralFrequancyList();
        setState(ViewState.Idle);
      }
    }
  }

  Future getDoctorsList(String clinicId) async {
    setState(ViewState.BusyLocal);
    await _patientService.getDoctorsList(clinicId);
    if (_patientService.hasError) {
      error = _patientService.error;
      setState(ViewState.ErrorLocal);
    } else {
      {
        await getReferralFrequancyList();
        setState(ViewState.Idle);
      }
    }
  }

  List getDoctorNameList() {
    var doctorNamelist = _patientService.doctorsList
        .map((value) => value['DoctorName'].toString())
        .toList();
    return doctorNamelist;
  }

  List getClinicNameList() {
    var clinicsNameslist = _patientService.clinicsList
        .map((value) => value['ClinicDescription'].toString())
        .toList();
    return clinicsNameslist;
  }

  Future getReferralFrequancyList() async {
    setState(ViewState.Busy);
    await _patientService.getReferralFrequancyList();
    if (_patientService.hasError) {
      error = _patientService.error;
      setState(ViewState.Error);
    } else
      setState(ViewState.Idle);
  }

  List getReferralNamesList() {
    var referralNamesList = _patientService.referalFrequancyList
        .map((value) => value['Description'].toString())
        .toList();
    return referralNamesList;
  }

  Future referToDoctor(
      {String selectedDoctorID,
      String selectedClinicID,
      int admissionNo,
      String extension,
      String priority,
      String frequency,
      String referringDoctorRemarks,
      int patientID,
      int patientTypeID,
      String roomID,
      int projectID}) async {
    setState(ViewState.BusyLocal);
    await _patientService.referToDoctor(
        selectedClinicID: selectedClinicID,
        selectedDoctorID: selectedDoctorID,
        admissionNo: admissionNo,
        extension: extension,
        priority: priority,
        frequency: frequency,
        referringDoctorRemarks: referringDoctorRemarks,
        patientID: patientID,
        patientTypeID: patientTypeID,
        roomID: roomID,
        projectID: projectID);
    if (_patientService.hasError) {
      error = _patientService.error;
      setState(ViewState.ErrorLocal);
    } else
      setState(ViewState.Idle);
  }

  Future getArrivedList() async {
    setState(ViewState.Busy);
    await _patientService.getArrivedList();
    if (_patientService.hasError) {
      error = _patientService.error;
      setState(ViewState.Error);
    } else
      setState(ViewState.Idle);
  }
}
