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/admissionRequest/admission-request.dart';
import 'package:doctor_app_flutter/core/service/patient-admission-request-service.dart';
import 'package:doctor_app_flutter/core/viewModel/base_view_model.dart';

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

class AdmissionRequestViewModel extends BaseViewModel{

  AdmissionRequestService _admissionRequestService = locator<AdmissionRequestService>();

  List<dynamic> get clinicList => _admissionRequestService.clinicList;

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

  List<dynamic>  get speciality => _admissionRequestService.specialityList;

  List<dynamic>  get floorList => _admissionRequestService.floorList;

  List<dynamic>  get wardList => _admissionRequestService.wardList;

  List<dynamic>  get roomCategoryList => _admissionRequestService.roomCategoryList;

  List<dynamic>  get admissionTypeList  => _admissionRequestService.listOfAdmissionType;

  List<dynamic>  get diagnosisTypesList => _admissionRequestService.diagnosisTypesList;

  List<dynamic>  get allergiesList => _admissionRequestService.allergiesLookupList;

  List<dynamic>  get dietTypesList => _admissionRequestService.dietTypesList;

  List<dynamic>  get icdCodes => _admissionRequestService.icdCodes;

  List<dynamic>  get listOfDiagnosisSelectionTypes => _admissionRequestService.listOfDiagnosisSelectionTypes;

  AdmissionRequest admissionRequestData;

  Future getSpecialityList() async {
    await getMasterLookup(MasterKeysService.Speciality);
    if (!_admissionRequestService.hasError) {
      _admissionRequestService.setSpecialityList();
      await getClinicDoctors(1);
    }
  }

  Future getClinics() async {
    setState(ViewState.BusyLocal);
    await _admissionRequestService.getClinics();
    if (_admissionRequestService.hasError) {
      error = _admissionRequestService.error;
      setState(ViewState.ErrorLocal);
    } else
      setState(ViewState.Idle);
  }

  Future getClinicDoctors(int clinicId) async {
    setState(ViewState.BusyLocal);
    await _admissionRequestService.getDoctorsList(clinicId);
    if (_admissionRequestService.hasError) {
      error = _admissionRequestService.error;
      setState(ViewState.ErrorLocal);
    } else
      setState(ViewState.Idle);
  }

  Future getFloors() async {
    setState(ViewState.BusyLocal);
    await _admissionRequestService.getFloors();
    if (_admissionRequestService.hasError) {
      error = _admissionRequestService.error;
      setState(ViewState.ErrorLocal);
    } else
      setState(ViewState.Idle);
  }

  Future getWards() async {
    setState(ViewState.BusyLocal);
    await _admissionRequestService.getWardList();
    if (_admissionRequestService.hasError) {
      error = _admissionRequestService.error;
      setState(ViewState.ErrorLocal);
    } else
      setState(ViewState.Idle);
  }

  Future getRoomCategories() async {
    setState(ViewState.BusyLocal);
    await _admissionRequestService.getRoomCategories();
    if (_admissionRequestService.hasError) {
      error = _admissionRequestService.error;
      setState(ViewState.ErrorLocal);
    } else
      setState(ViewState.Idle);
  }

  Future getDiagnosis() async {
    setState(ViewState.BusyLocal);
    await _admissionRequestService.getDiagnosisTypesList();
    if (_admissionRequestService.hasError) {
      error = _admissionRequestService.error;
      setState(ViewState.ErrorLocal);
    } else
      setState(ViewState.Idle);
  }

  Future getAllergies() async {
    await getMasterLookup(MasterKeysService.Allergies);
    if (!_admissionRequestService.hasError) {
      _admissionRequestService.setAllergies();
    }
  }

  Future getDietTypes(int patientMrn) async {
    setState(ViewState.BusyLocal);
    await _admissionRequestService.getDietTypesList(patientMrn);
    if (_admissionRequestService.hasError) {
      error = _admissionRequestService.error;
      setState(ViewState.ErrorLocal);
    } else
      setState(ViewState.Idle);
  }

  Future getICDCodes(int patientMrn) async {
    setState(ViewState.BusyLocal);
    await _admissionRequestService.getICDCodes(patientMrn);
    if (_admissionRequestService.hasError) {
      error = _admissionRequestService.error;
      setState(ViewState.ErrorLocal);
    } else
      setState(ViewState.Idle);
  }

  Future makeAdmissionRequest() async{
    setState(ViewState.Busy);
    await _admissionRequestService.makeAdmissionRequest(admissionRequestData);
    if (_admissionRequestService.hasError) {
      error = _admissionRequestService.error;
      setState(ViewState.ErrorLocal);
    } else
      setState(ViewState.Idle);
  }

  Future getMasterLookup(MasterKeysService keysService) async {
    setState(ViewState.BusyLocal);
    await _admissionRequestService.getMasterLookup(keysService);
    if (_admissionRequestService.hasError) {
      error = _admissionRequestService.error;
      setState(ViewState.ErrorLocal);
    } else {
      setState(ViewState.Idle);
    }
  }
}