import 'package:doctor_app_flutter/config/shared_pref_kay.dart';
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/prescription_model.dart';
import 'package:doctor_app_flutter/core/service/patient-ucaf-service.dart';
import 'package:doctor_app_flutter/core/viewModel/base_view_model.dart';
import 'package:doctor_app_flutter/models/SOAP/ChiefComplaint/GetChiefComplaintResModel.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/SOAP/order-procedure.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';
import 'package:flutter/material.dart';

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

class UcafViewModel extends BaseViewModel {
  UcafService _ucafService = locator<UcafService>();

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

  VitalSignData get patientVitalSigns => _ucafService.patientVitalSigns;

  List<GetAssessmentResModel> get patientAssessmentList =>
      _ucafService.patientAssessmentList;

  List<MasterKeyModel> get diagnosisTypes => _ucafService.listOfDiagnosisType;

  List<MasterKeyModel> get diagnosisConditions =>
      _ucafService.listOfDiagnosisCondition;

  PrescriptionModel get prescriptionList => _ucafService.prescriptionList;

  List<OrderProcedure> get orderProcedures => _ucafService.orderProcedureList;

  String selectedLanguage;

  resetDataInFirst(){
    _ucafService.patientAssessmentList = [];
    _ucafService.orderProcedureList = [];
    _ucafService.prescriptionList = null;
  }

  Future getLanguage() async {
    selectedLanguage = await sharedPref.getString(APP_Language);
  }

  Future getUCAFData(PatiantInformtion patient) async {
    setState(ViewState.Busy);
    await _ucafService.getPatientVitalSign(patient);
    await _ucafService.getPatientChiefComplaint(patient);

    if (_ucafService.hasError) {
      error = _ucafService.error;
      setState(ViewState.Error);
    } else {
      setState(ViewState.Idle);
    }
  }

  Future getPatientAssessment(PatiantInformtion patient) async {
    if (patientAssessmentList.isEmpty) {
      setState(ViewState.Busy);
      await _ucafService.getPatientAssessment(patient);
      if (_ucafService.hasError) {
        error = _ucafService.error;
        setState(ViewState.Error);
      } else {
        if (patientAssessmentList.isNotEmpty) {
          if (diagnosisConditions.length == 0) {
            await _ucafService
                .getMasterLookup(MasterKeysService.DiagnosisCondition);
          }
          if (diagnosisTypes.length == 0) {
            await _ucafService.getMasterLookup(MasterKeysService.DiagnosisType);
          }
          if (_ucafService.hasError) {
            error = _ucafService.error;
            setState(ViewState.Error);
          } else
            setState(ViewState.Idle);
        } else
          setState(ViewState.Idle); // but with empty list
      }
    }
  }

  Future getOrderProcedures(PatiantInformtion patient) async {
    if (orderProcedures.isEmpty) {
      setState(ViewState.Busy);
      await _ucafService.getOrderProcedures(patient);
      if (_ucafService.hasError) {
        error = _ucafService.error;
        setState(ViewState.Error);
      } else {
        setState(ViewState.Idle);
      }
    }
  }

  Future getPrescription(PatiantInformtion patient) async {
    if (prescriptionList == null) {
      setState(ViewState.Busy);
      await _ucafService.getPrescription(patient);
      if (_ucafService.hasError) {
        error = _ucafService.error;
        setState(ViewState.Error);
      } else {
        setState(ViewState.Idle);
      }
    }
  }

  MasterKeyModel findMasterDataById(
      {@required MasterKeysService masterKeys, dynamic id}) {
    switch (masterKeys) {
      case MasterKeysService.DiagnosisCondition:
        List<MasterKeyModel> result = diagnosisConditions.where((element) {
          return element.id == id &&
              element.typeId == masterKeys.getMasterKeyService();
        }).toList();
        if (result.isNotEmpty) {
          return result.first;
        }
        return null;
      case MasterKeysService.DiagnosisType:
        List<MasterKeyModel> result = diagnosisTypes.where((element) {
          return element.id == id &&
              element.typeId == masterKeys.getMasterKeyService();
        }).toList();
        if (result.isNotEmpty) {
          return result.first;
        }
        return null;
      default:
        return null;
    }
  }

  Future postUCAF(PatiantInformtion patient) async {
    setState(ViewState.Busy);
    await _ucafService.postUCAF(patient);
    if (_ucafService.hasError) {
      error = _ucafService.error;
      setState(ViewState.ErrorLocal);
    } else {
      setState(ViewState.Idle); // but with empty list
    }
  }
}
