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/prescription_service.dart';
import 'package:doctor_app_flutter/models/SOAP/ChiefComplaint/GetChiefComplaintReqModel.dart';
import 'package:doctor_app_flutter/models/SOAP/ChiefComplaint/GetChiefComplaintResModel.dart';
import 'package:doctor_app_flutter/models/SOAP/GeneralGetReqForSOAP.dart';
import 'package:doctor_app_flutter/models/SOAP/GetAllergiesResModel.dart';
import 'package:doctor_app_flutter/models/SOAP/GetAssessmentReqModel.dart';
import 'package:doctor_app_flutter/models/SOAP/GetAssessmentResModel.dart';
import 'package:doctor_app_flutter/models/SOAP/GetGetProgressNoteReqModel.dart';
import 'package:doctor_app_flutter/models/SOAP/GetGetProgressNoteResModel.dart';
import 'package:doctor_app_flutter/models/SOAP/GetHistoryReqModel.dart';
import 'package:doctor_app_flutter/models/SOAP/GetHistoryResModel.dart';
import 'package:doctor_app_flutter/models/SOAP/GetPhysicalExamListResModel.dart';
import 'package:doctor_app_flutter/models/SOAP/GetPhysicalExamReqModel.dart';
import 'package:doctor_app_flutter/models/SOAP/PostEpisodeReqModel.dart';
import 'package:doctor_app_flutter/models/SOAP/get_Allergies_request_model.dart';
import 'package:doctor_app_flutter/models/SOAP/master_key_model.dart';
import 'package:doctor_app_flutter/models/SOAP/post_allergy_request_model.dart';
import 'package:doctor_app_flutter/models/SOAP/post_assessment_request_model.dart';
import 'package:doctor_app_flutter/models/SOAP/post_chief_complaint_request_model.dart';
import 'package:doctor_app_flutter/models/SOAP/post_histories_request_model.dart';
import 'package:doctor_app_flutter/models/SOAP/post_physical_exam_request_model.dart';
import 'package:doctor_app_flutter/models/SOAP/post_progress_note_request_model.dart';
import 'package:doctor_app_flutter/widgets/patients/profile/soap_update/update_assessment_page.dart';
import 'package:flutter/material.dart';

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

class SOAPViewModel extends BaseViewModel {
  SOAPService _SOAPService = locator<SOAPService>();
  PrescriptionService _prescriptionService = locator<PrescriptionService>();

  List<MasterKeyModel> get allergiesList => _SOAPService.allergiesList;

  List<MasterKeyModel> get allergySeverityList =>
      _SOAPService.allergySeverityList;


  List<MasterKeyModel> get historyFamilyList => _SOAPService.historyFamilyList;

  List<MasterKeyModel> get historyMedicalList => _SOAPService.historyMedicalList;

  List<MasterKeyModel> get historySportList => _SOAPService.historySportList;

  List<MasterKeyModel> get historySocialList => _SOAPService.historySocialList;

  List<MasterKeyModel> get historySurgicalList =>
      _SOAPService.historySurgicalList;

  List<MasterKeyModel> get mergeHistorySurgicalWithHistorySportList =>
      [...historySurgicalList, ...historySportList];

  List<MasterKeyModel> get physicalExaminationList =>
      _SOAPService.physicalExaminationList;

  List<MasterKeyModel> get listOfDiagnosisType =>
      _SOAPService.listOfDiagnosisType;

  List<MasterKeyModel> get listOfDiagnosisCondition =>
      _SOAPService.listOfDiagnosisCondition;

  List<MasterKeyModel> get listOfICD10 => _SOAPService.listOfICD10;

  List<GetChiefComplaintResModel> get patientChiefComplaintList =>
      _SOAPService.patientChiefComplaintList;

  List<GetAllergiesResModel> get patientAllergiesList =>
      _SOAPService.patientAllergiesList;

  List<GetHistoryResModel> get patientHistoryList =>
      _SOAPService.patientHistoryList;

  List<GetPhysicalExamResModel> get patientPhysicalExamList =>
      _SOAPService.patientPhysicalExamList;

  List<GetPatientProgressNoteResModel> get patientProgressNoteList =>
      _SOAPService.patientProgressNoteList;

  List<GetAssessmentResModel> get patientAssessmentList =>
      _SOAPService.patientAssessmentList;
  int get episodeID =>
      _SOAPService.episodeID;

  get medicationStrengthList => _SOAPService.medicationStrengthListWithModel;
  get medicationDoseTimeList => _SOAPService.medicationDoseTimeListWithModel;
  get medicationRouteList => _SOAPService.medicationRouteListWithModel;
  get medicationFrequencyList => _SOAPService.medicationFrequencyListWithModel;
  List<GetMedicationResponseModel> get allMedicationList =>
      _prescriptionService.allMedicationList;

  Future getAllergies(GetAllergiesRequestModel getAllergiesRequestModel) async {
    setState(ViewState.Busy);
    await _SOAPService.getAllergies(getAllergiesRequestModel);
    if (_SOAPService.hasError) {
      error = _SOAPService.error;
      setState(ViewState.Error);
    } else
      setState(ViewState.Idle);
  }

  Future getMasterLookup(MasterKeysService masterKeys, {bool isBusyLocal = false}) async {
    if(isBusyLocal){
      setState(ViewState.Busy);
    }else
    setState(ViewState.Busy);
    await _SOAPService.getMasterLookup(masterKeys);
    if (_SOAPService.hasError) {
      error = _SOAPService.error;
      setState(ViewState.Error);
    } else
      setState(ViewState.Idle);
  }


  Future postEpisode(PostEpisodeReqModel postEpisodeReqModel) async {
    setState(ViewState.BusyLocal);
    await _SOAPService.postEpisode(postEpisodeReqModel);
    if (_SOAPService.hasError) {
      error = _SOAPService.error;
      setState(ViewState.ErrorLocal);
    } else
      setState(ViewState.Idle);
  }

  Future postAllergy(PostAllergyRequestModel postAllergyRequestModel) async {
    setState(ViewState.BusyLocal);
    await _SOAPService.postAllergy(postAllergyRequestModel);
    if (_SOAPService.hasError) {
      error = _SOAPService.error;
      setState(ViewState.ErrorLocal);
    } else
      setState(ViewState.Idle);
  }

  Future postHistories(PostHistoriesRequestModel postHistoriesRequestModel) async {
    setState(ViewState.BusyLocal);
    await _SOAPService.postHistories(postHistoriesRequestModel);
    if (_SOAPService.hasError) {
      error = _SOAPService.error;
      setState(ViewState.ErrorLocal);
    } else
      setState(ViewState.Idle);
  }

  Future postChiefComplaint(PostChiefComplaintRequestModel postChiefComplaintRequestModel) async {
    setState(ViewState.BusyLocal);
    await _SOAPService.postChiefComplaint(postChiefComplaintRequestModel);
    if (_SOAPService.hasError) {
      error = _SOAPService.error;
      setState(ViewState.ErrorLocal);
    } else
      setState(ViewState.Idle);
  }

  Future postPhysicalExam(PostPhysicalExamRequestModel postPhysicalExamRequestModel) async {
    setState(ViewState.BusyLocal);
    await _SOAPService.postPhysicalExam(postPhysicalExamRequestModel);
    if (_SOAPService.hasError) {
      error = _SOAPService.error;
      setState(ViewState.ErrorLocal);
    } else
      setState(ViewState.Idle);
  }

  Future postProgressNote(PostProgressNoteRequestModel postProgressNoteRequestModel)  async {
    setState(ViewState.BusyLocal);
    await _SOAPService.postProgressNote(postProgressNoteRequestModel);
    if (_SOAPService.hasError) {
      error = _SOAPService.error;
      setState(ViewState.ErrorLocal);
    } else
      setState(ViewState.Idle);
  }

  Future postAssessment(PostAssessmentRequestModel postAssessmentRequestModel)  async {
    setState(ViewState.BusyLocal);
    await _SOAPService.postAssessment(postAssessmentRequestModel);
    if (_SOAPService.hasError) {
      error = _SOAPService.error;
      setState(ViewState.ErrorLocal);
    } else
      setState(ViewState.Idle);
  }


  Future patchAllergy(PostAllergyRequestModel patchAllergyRequestModel) async {
    setState(ViewState.BusyLocal);
    await _SOAPService.patchAllergy(patchAllergyRequestModel);
    if (_SOAPService.hasError) {
      error = _SOAPService.error;
      setState(ViewState.ErrorLocal);
    } else
      setState(ViewState.Idle);
  }

  Future patchHistories(PostHistoriesRequestModel patchHistoriesRequestModel) async {
    setState(ViewState.BusyLocal);
    await _SOAPService.patchHistories(patchHistoriesRequestModel);
    if (_SOAPService.hasError) {
      error = _SOAPService.error;
      setState(ViewState.ErrorLocal);
    } else
      setState(ViewState.Idle);
  }

  Future patchChiefComplaint(PostChiefComplaintRequestModel patchChiefComplaintRequestModel) async {
    setState(ViewState.BusyLocal);
    await _SOAPService.patchChiefComplaint(patchChiefComplaintRequestModel);
    if (_SOAPService.hasError) {
      error = _SOAPService.error;
      setState(ViewState.ErrorLocal);
    } else
      setState(ViewState.Idle);
  }

  Future patchPhysicalExam(PostPhysicalExamRequestModel patchPhysicalExamRequestModel) async {
    setState(ViewState.BusyLocal);
    await _SOAPService.patchPhysicalExam(patchPhysicalExamRequestModel);
    if (_SOAPService.hasError) {
      error = _SOAPService.error;
      setState(ViewState.ErrorLocal);
    } else
      setState(ViewState.Idle);
  }

  Future patchProgressNote(PostProgressNoteRequestModel patchProgressNoteRequestModel)  async {
    setState(ViewState.BusyLocal);
    await _SOAPService.patchProgressNote(patchProgressNoteRequestModel);
    if (_SOAPService.hasError) {
      error = _SOAPService.error;
      setState(ViewState.ErrorLocal);
    } else
      setState(ViewState.Idle);
  }

  Future patchAssessment(PatchAssessmentReqModel patchAssessmentRequestModel)  async {
    setState(ViewState.BusyLocal);
    await _SOAPService.patchAssessment(patchAssessmentRequestModel);
    if (_SOAPService.hasError) {
      error = _SOAPService.error;
      setState(ViewState.ErrorLocal);
    } else
      setState(ViewState.Idle);
  }

  Future getPatientAllergy(GeneralGetReqForSOAP generalGetReqForSOAP,
      {isLocalBusy = false}) async {
    if (isLocalBusy) {
      setState(ViewState.BusyLocal);
    } else
      setState(ViewState.Busy);
    await _SOAPService.getPatientAllergy(generalGetReqForSOAP);
    if (_SOAPService.hasError) {
      error = _SOAPService.error;
      if (isLocalBusy) {
        setState(ViewState.ErrorLocal);
      } else
      setState(ViewState.Error);
    } else
      setState(ViewState.Idle);
  }

  String getAllergicNames(isArabic) {
    String allergiesString = '';
    patientAllergiesList.forEach((element) {
      MasterKeyModel selectedAllergy = getOneMasterKey(
          masterKeys: MasterKeysService.Allergies,
          id: element.allergyDiseaseId,
          typeId: element.allergyDiseaseType);
      if(selectedAllergy != null && element.isChecked)
      allergiesString +=
          (isArabic ? selectedAllergy.nameAr : selectedAllergy.nameEn )+ ' , ';
    });
    return allergiesString;
  }

  Future getPatientHistories(GetHistoryReqModel getHistoryReqModel, {bool isFirst =  false}) async {
    setState(ViewState.Busy);
    await _SOAPService.getPatientHistories(getHistoryReqModel, isFirst: isFirst);
    if (_SOAPService.hasError) {
      error = _SOAPService.error;
      setState(ViewState.Error);
    } else
      setState(ViewState.Idle);
  }

  Future getPatientChiefComplaint(
      GetChiefComplaintReqModel getChiefComplaintReqModel) async {
    setState(ViewState.Busy);
    await _SOAPService.getPatientChiefComplaint(getChiefComplaintReqModel);
    if (_SOAPService.hasError) {
      error = _SOAPService.error;
      setState(ViewState.Error);
    } else
      setState(ViewState.Idle);
  }

  Future getPatientPhysicalExam(GetPhysicalExamReqModel getPhysicalExamReqModel) async {
    setState(ViewState.Busy);
    await _SOAPService.getPatientPhysicalExam(getPhysicalExamReqModel);
    if (_SOAPService.hasError) {
      error = _SOAPService.error;
      setState(ViewState.Error);
    } else
      setState(ViewState.Idle);
  }

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

  Future getPatientAssessment(
      GetAssessmentReqModel getAssessmentReqModel) async {
    setState(ViewState.Busy);
    await _SOAPService.getPatientAssessment(getAssessmentReqModel);
    if (_SOAPService.hasError) {
      error = _SOAPService.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);
  }
  // ignore: missing_return
  MasterKeyModel getOneMasterKey(
      {@required MasterKeysService masterKeys, dynamic id, int typeId}) {
    switch (masterKeys) {
      case MasterKeysService.Allergies:
        List<MasterKeyModel> result = allergiesList.where((element) {
          return element.id == id &&
              element.typeId == masterKeys.getMasterKeyService();
        }).toList();
        if (result.isNotEmpty) {
          return result.first;
        }
        break;

      case MasterKeysService.HistoryFamily:
        List<MasterKeyModel> result = historyFamilyList.where((element) {
          return element.id == id &&
              element.typeId == masterKeys.getMasterKeyService();
        }).toList();
        if (result.isNotEmpty) {
          return result.first;
        }
        break;
      case MasterKeysService.HistoryMedical:
        List<MasterKeyModel> result = historyMedicalList.where((element) {
          return element.id == id &&
              element.typeId == masterKeys.getMasterKeyService();
        }).toList();
        if (result.isNotEmpty) {
          return result.first;
        }
        break;
      case MasterKeysService.HistorySocial:
        List<MasterKeyModel> result = historySocialList.where((element) {
          return element.id == id &&
              element.typeId == masterKeys.getMasterKeyService();
        }).toList();
        if (result.isNotEmpty) {
          return result.first;
        }
        break;
      case MasterKeysService.HistorySports:
        List<MasterKeyModel> result = historySocialList.where((element) {
          return element.id == id &&
              element.typeId == masterKeys.getMasterKeyService();
        }).toList();
        if (result.isNotEmpty) {
          return result.first;
        }
        break;
      case MasterKeysService.HistorySurgical:
        List<MasterKeyModel> result = historySurgicalList.where((element) {
          return element.id == id;}).toList();
        if (result.isNotEmpty) {
          return result.first;
        }
        break;
      case MasterKeysService.PhysicalExamination:
        List<MasterKeyModel> result = physicalExaminationList.where((element) {
          return element.id == id &&
              element.typeId == masterKeys.getMasterKeyService();
        }).toList();
        if (result.isNotEmpty) {
          return result.first;
        }
        break;
      case MasterKeysService.AllergySeverity:
        List<MasterKeyModel> result = allergySeverityList.where((element) {
          return element.id == id &&
              element.typeId == masterKeys.getMasterKeyService();
        }).toList();
        if (result.isNotEmpty) {
          return result.first;
        }
        break;
    // case MasterKeysService.physiotherapyGoals:
    //   listOfPhysiotherapyGoals.clear();
    //   entryList.forEach((v) {
    //     listOfPhysiotherapyGoals.add(MasterKeyModel.fromJson(v));
    //   });
    //   break;

      case MasterKeysService.DiagnosisType:
        List<MasterKeyModel> result = listOfDiagnosisType.where((element) {
          return element.id == id &&
              element.typeId == masterKeys.getMasterKeyService();
        }).toList();
        if (result.isNotEmpty) {
          return result.first;
        }
        break;
      case MasterKeysService.DiagnosisCondition:
        List<MasterKeyModel> result = listOfDiagnosisCondition.where((element) {
          return element.id == id &&
              element.typeId == masterKeys.getMasterKeyService();
        }).toList();
        if (result.isNotEmpty) {
          return result.first;
        }
        break;
      case MasterKeysService.ICD10:
        List<MasterKeyModel> result = listOfICD10.where((element) {
          return element.code == id ;
        }).toList();
        if (result.isNotEmpty) {
          return result.first;
        }
        break;
        break;
    }
  }


}
