import 'package:doctor_app_flutter/core/enum/view_state.dart';
import 'package:doctor_app_flutter/core/model/dashboard/get_special_clinical_care_mapping_List_Respose_Model.dart';
import 'package:doctor_app_flutter/core/viewModel/base_view_model.dart';
import 'package:doctor_app_flutter/locator.dart';
import 'package:doctor_app_flutter/core/model/patient/patiant_info_model.dart';
import 'package:doctor_app_flutter/core/model/pharmacy-intervention-model/accept_or_reject_req_model.dart';
import 'package:doctor_app_flutter/core/model/pharmacy-intervention-model/intervention_medication_history_req_model.dart';
import 'package:doctor_app_flutter/core/model/pharmacy-intervention-model/intervention_medication_history_res_model.dart';
import 'package:doctor_app_flutter/core/model/pharmacy-intervention-model/new_medication_req_model.dart';
import 'package:doctor_app_flutter/core/model/pharmacy-intervention-model/new_medication_res_model.dart';

import '../../../utils/date-utils.dart';
import '../../service/patient/profile/intervention_medication_service.dart';
import '../../service/special_clinics/special_clinic_service.dart';

class InterventionMedicationViewModel extends BaseViewModel {
  SpecialClinicsService _specialClinicsService = locator<SpecialClinicsService>();

  List<GetSpecialClinicalCareMappingListResponseModel> get specialClinicalCareMappingList => _specialClinicsService.specialClinicalCareMappingList;

  List<PatiantInformtion> filterData = [];

  DateTime selectedFromDate = DateTime.now();
  DateTime selectedToDate = DateTime.now().add(Duration(days: 1));

  PatiantInformtion? patient;
  bool hasError = false;
  InterventionMedicationService _interventionMedicationService = locator<InterventionMedicationService>();

  List<InterventionMedicationResModel> get allInterventionList => _interventionMedicationService.allInterventionList;

  List<InterventionMedicationHistoryResModel> get allInterventionHistoryList => _interventionMedicationService.allInterventionHistoryList;

  Future getInterventionMedication({
    int? projectId,
    int? patientId,
    DateTime? fromDate,
    DateTime? toDate,
  }) async {
    InterventionMedicationReqModel interventionMedicationReqModel = InterventionMedicationReqModel(
      projectID: projectId,
      patientID: patientId,
      fromDate: AppDateUtils.convertDateToServerFormat(fromDate!),
      toDate: AppDateUtils.convertDateToServerFormat(toDate!),
    );
    hasError = false;
    setState(ViewState.Busy);
    await _interventionMedicationService.getInterventionMedication(interventionMedicationReqModel: interventionMedicationReqModel);
    if (_interventionMedicationService.hasError) {
      error = _interventionMedicationService.error;
      setState(ViewState.Error);
    } else {
      setState(ViewState.Idle);
    }
  }

  Future getInterventionMedicationHistory({int? projectId, int? patientId, int? admissionNo, int? prescriptionNo, int? orderNo, bool isBusyLocal = false}) async {
    InterventionMedicationHistoryReqModel interventionMedicationHistoryReqModel = InterventionMedicationHistoryReqModel(
      projectID: projectId,
      patientID: patientId,
      admissionNo: admissionNo,
      prescriptionNo: prescriptionNo,
      orderNo: orderNo,
    );
    hasError = false;
    if (isBusyLocal)
      setState(ViewState.Busy);
    else
      setState(ViewState.Busy);
    await _interventionMedicationService.getInterventionMedicationHistory(interventionMedicationHistoryReqModel: interventionMedicationHistoryReqModel);
    if (_interventionMedicationService.hasError) {
      error = _interventionMedicationService.error;
      if (isBusyLocal)
        setState(ViewState.ErrorLocal);
      else
        setState(ViewState.Error);
    } else {
      setState(ViewState.Idle);
    }
  }

  Future setAcceptedOrRejected({
    String? remarks,
    int? memberId,
    int? projectId,
    int? patientID,
    int? admissionNo,
    int? prescriptionNo,
    int? orderNo,
    int? interventionStatus,
    int? status,
  }) async {
    AcceptOrRejectReqModel acceptOrRejectReqModel = AcceptOrRejectReqModel(
        projectID: projectId,
        patientID: patientID,
        admissionNo: admissionNo,
        prescriptionNo: prescriptionNo,
        orderNo: orderNo,
        accessLevel: 4,
        lineItemNo: 1,
        remarks: remarks,
        memberID: memberId,
        interventionStatus: interventionStatus,
        status: status);
    hasError = false;
    setState(ViewState.BusyLocal);
    await _interventionMedicationService.setAcceptedOrRejected(acceptOrRejectReqModel: acceptOrRejectReqModel);
    if (_interventionMedicationService.hasError) {
      error = _interventionMedicationService.error;
      setState(ViewState.ErrorLocal);
    } else {
      setState(ViewState.Idle);
    }
  }
}
