import 'package:doctor_app_flutter/config/shared_pref_kay.dart';
import 'package:doctor_app_flutter/core/enum/view_state.dart';
import 'package:doctor_app_flutter/core/model/doctor/doctor_profile_model.dart';
import 'package:doctor_app_flutter/core/model/pharmacy-intervention-model/intervention_history.dart';
import 'package:doctor_app_flutter/core/model/pharmacy-intervention-model/nursing_station.dart';
import 'package:doctor_app_flutter/core/model/pharmacy-intervention-model/pharmacy_intervention_item.dart';
import 'package:doctor_app_flutter/core/viewModel/base_view_model.dart';
import 'package:doctor_app_flutter/locator.dart';
import 'package:doctor_app_flutter/screens/pharmacy_intervention/viewmodel/pharmacy_intervention_service.dart';
import 'package:doctor_app_flutter/utils/dr_app_toast_msg.dart';
import 'package:intl/intl.dart';

class PharmacyInterventionViewModel extends BaseViewModel {
  PharmacyInterventionService _service = locator<PharmacyInterventionService>();
  MedicationList? medicationList;
  bool isBottomSheetShown = false;

  // MedicationList? medicationList = MedicationList.fromJson({
  //   "entityList": [
  //     {
  //       "accessLevel": 4,
  //       "admissionNo": 2020000098,
  //       "authorizedby": null,
  //       "authorizedbyName": null,
  //       "createdBy": "1485",
  //       "doctorComments": "asdf",
  //       "doctorName": null,
  //       "dosageDetail": "0/VIALS/BID/Parenteral",
  //       "itemID": 107476,
  //       "lineItemNo": 1,
  //       "medication": "TARGOPLANIN 200 MG INJ VIAL 1'S",
  //       "nursingStation": "New Pediatric Building 1st  Floor ",
  //       "orderNo": 1448172,
  //       "patientID": 3120412,
  //       "patientName": "HABAB AABER CHINAN",
  //       "pharmacyRemarks": null,
  //       "prescriptionNo": 2686564,
  //       "startDatetime": "2024-12-24T10:33:18",
  //       "status": 62,
  //       "statusName": "RejectWithIntervention",
  //       "stopDatetime": "2024-12-24T20:00:00"
  //     },{
  //       "accessLevel": 4,
  //       "patientID": 3648880,
  //       "patientName": "JEHAD BADEI ALI",
  //       "nursingStation": "New Pediatric Building 3rd  Floor ",
  //       "admissionNo": 2022000100,
  //       "orderNo": 1447979,
  //       "prescriptionNo": 2686333,
  //       "lineItemNo": 3,
  //       "itemID": 67507,
  //       "medication": "DIPEPTIVEN 20% CONCENTRATED -100ML",
  //       "dosageDetail": "1 MG/BOTTLE/As Directed/Oral",
  //       "doctorComments": "Test PHR:  (Pharmacy Intervention Accepted)Test",
  //       "startDatetime": "2024-05-08T12:56:00",
  //       "stopDatetime": "2024-05-08T12:57:00",
  //       "status": 52,
  //       "statusName": "Discontinue",
  //       "createdBy": "2804",
  //       "authorizedby": null,
  //       "doctorName": null,
  //       "authorizedbyName": null,
  //       "pharmacyRemarks": null
  //     },
  //     {
  //       "accessLevel": 4,
  //       "patientID": 3648880,
  //       "patientName": "JEHAD BADEI ALI",
  //       "nursingStation": "New Pediatric Building 3rd  Floor ",
  //       "admissionNo": 2022000100,
  //       "orderNo": 1447979,
  //       "prescriptionNo": 2686333,
  //       "lineItemNo": 3,
  //       "itemID": 67507,
  //       "medication": "DIPEPTIVEN 20% CONCENTRATED -100ML",
  //       "dosageDetail": "1 MG/BOTTLE/As Directed/Oral",
  //       "doctorComments": "Test PHR:  (Pharmacy Intervention Accepted)Test",
  //       "startDatetime": "2024-05-08T12:56:00",
  //       "stopDatetime": "2024-05-08T12:57:00",
  //       "status": 52,
  //       "statusName": "Discontinue",
  //       "createdBy": "2804",
  //       "authorizedby": null,
  //       "doctorName": null,
  //       "authorizedbyName": null,
  //       "pharmacyRemarks": null
  //     }
  //   ],
  //   "rowcount": 1,
  //   "statusMessage": null,
  //   "success": null
  // });

  Medication? currentlySelectedMedication;

  InterventionHistoryList? interventionHistoryList;

  NursingStation? nursingStations;

  PharmacyInterventionViewModel(){
    getDoctorProfile();
  }

  // InterventionHistoryList? interventionHistoryList = InterventionHistoryList.fromJson({
  //   "entityList": [
  //     {
  //       "authizationFormText": "Intervention Approval Level 3",
  //       "authorizedAction": 51,
  //       "commentedBy": 1005,
  //       "commentedByName": "TEMP - PHARMACIST ",
  //       "interventionDesc": "Drug information :administration related",
  //       "interventionId": 29,
  //       "remark": "asdfasdf"
  //     },
  //     {
  //       "authizationFormText": "Intervention Approval Level 3",
  //       "authorizedAction": 51,
  //       "commentedBy": 0,
  //       "commentedByName": "lorem ipsum1 ",
  //       "interventionDesc": "",
  //       "interventionId": 1485,
  //       "remark": "loremIpsum"
  //     }, {
  //       "authizationFormText": "Intervention Approval Level 3",
  //       "authorizedAction": 51,
  //       "commentedBy": 1005,
  //       "commentedByName": "TEMP - PHARMACIST ",
  //       "interventionDesc": "Drug information :administration related",
  //       "interventionId": 29,
  //       "remark": "asdfasdf"
  //     },
  //     {
  //       "authizationFormText": "Intervention Approval Level 3",
  //       "authorizedAction": 51,
  //       "commentedBy": 0,
  //       "commentedByName": "lorem ipsum1 ",
  //       "interventionDesc": "",
  //       "interventionId": 1485,
  //       "remark": "loremIpsum"
  //     }, {
  //       "authizationFormText": "Intervention Approval Level 3",
  //       "authorizedAction": 51,
  //       "commentedBy": 1005,
  //       "commentedByName": "TEMP - PHARMACIST ",
  //       "interventionDesc": "Drug information :administration related",
  //       "interventionId": 29,
  //       "remark": "asdfasdf"
  //     },
  //     {
  //       "authizationFormText": "Intervention Approval Level 3",
  //       "authorizedAction": 51,
  //       "commentedBy": 0,
  //       "commentedByName": "lorem ipsum1 ",
  //       "interventionDesc": "",
  //       "interventionId": 1485,
  //       "remark": "loremIpsum"
  //     }, {
  //       "authizationFormText": "Intervention Approval Level 3",
  //       "authorizedAction": 51,
  //       "commentedBy": 1005,
  //       "commentedByName": "TEMP - PHARMACIST ",
  //       "interventionDesc": "Drug information :administration related",
  //       "interventionId": 29,
  //       "remark": "asdfasdf"
  //     },
  //     {
  //       "authizationFormText": "Intervention Approval Level 3",
  //       "authorizedAction": 51,
  //       "commentedBy": 0,
  //       "commentedByName": "lorem ipsum1 ",
  //       "interventionDesc": "",
  //       "interventionId": 1485,
  //       "remark": "loremIpsum"
  //     }, {
  //       "authizationFormText": "Intervention Approval Level 3",
  //       "authorizedAction": 51,
  //       "commentedBy": 1005,
  //       "commentedByName": "TEMP - PHARMACIST ",
  //       "interventionDesc": "Drug information :administration related",
  //       "interventionId": 29,
  //       "remark": "asdfasdf"
  //     },
  //     {
  //       "authizationFormText": "Intervention Approval Level 3",
  //       "authorizedAction": 51,
  //       "commentedBy": 0,
  //       "commentedByName": "lorem ipsum1 ",
  //       "interventionDesc": "",
  //       "interventionId": 1485,
  //       "remark": "loremIpsum"
  //     }, {
  //       "authizationFormText": "Intervention Approval Level 3",
  //       "authorizedAction": 51,
  //       "commentedBy": 1005,
  //       "commentedByName": "TEMP - PHARMACIST ",
  //       "interventionDesc": "Drug information :administration related",
  //       "interventionId": 29,
  //       "remark": "asdfasdf"
  //     },
  //     {
  //       "authizationFormText": "Intervention Approval Level 3",
  //       "authorizedAction": 51,
  //       "commentedBy": 0,
  //       "commentedByName": "lorem ipsum1 ",
  //       "interventionDesc": "",
  //       "interventionId": 1485,
  //       "remark": "loremIpsum"
  //     }, {
  //       "authizationFormText": "Intervention Approval Level 3",
  //       "authorizedAction": 51,
  //       "commentedBy": 1005,
  //       "commentedByName": "TEMP - PHARMACIST ",
  //       "interventionDesc": "Drug information :administration related",
  //       "interventionId": 29,
  //       "remark": "asdfasdf"
  //     },
  //     {
  //       "authizationFormText": "Intervention Approval Level 3",
  //       "authorizedAction": 51,
  //       "commentedBy": 0,
  //       "commentedByName": "lorem ipsum1 ",
  //       "interventionDesc": "",
  //       "interventionId": 1485,
  //       "remark": "loremIpsum"
  //     }, {
  //       "authizationFormText": "Intervention Approval Level 3",
  //       "authorizedAction": 51,
  //       "commentedBy": 1005,
  //       "commentedByName": "TEMP - PHARMACIST ",
  //       "interventionDesc": "Drug information :administration related",
  //       "interventionId": 29,
  //       "remark": "asdfasdf"
  //     },
  //     {
  //       "authizationFormText": "Intervention Approval Level 3",
  //       "authorizedAction": 51,
  //       "commentedBy": 0,
  //       "commentedByName": "lorem ipsum1 ",
  //       "interventionDesc": "",
  //       "interventionId": 1485,
  //       "remark": "loremIpsum"
  //     }, {
  //       "authizationFormText": "Intervention Approval Level 3",
  //       "authorizedAction": 51,
  //       "commentedBy": 1005,
  //       "commentedByName": "TEMP - PHARMACIST ",
  //       "interventionDesc": "Drug information :administration related",
  //       "interventionId": 29,
  //       "remark": "asdfasdf"
  //     },
  //     {
  //       "authizationFormText": "Intervention Approval Level 3",
  //       "authorizedAction": 51,
  //       "commentedBy": 0,
  //       "commentedByName": "lorem ipsum1 ",
  //       "interventionDesc": "",
  //       "interventionId": 1485,
  //       "remark": "loremIpsum"
  //     }, {
  //       "authizationFormText": "Intervention Approval Level 3",
  //       "authorizedAction": 51,
  //       "commentedBy": 1005,
  //       "commentedByName": "TEMP - PHARMACIST ",
  //       "interventionDesc": "Drug information :administration related",
  //       "interventionId": 29,
  //       "remark": "asdfasdf"
  //     },
  //     {
  //       "authizationFormText": "Intervention Approval Level 3",
  //       "authorizedAction": 51,
  //       "commentedBy": 0,
  //       "commentedByName": "lorem ipsum1 ",
  //       "interventionDesc": "",
  //       "interventionId": 1485,
  //       "remark": "loremIpsum"
  //     }, {
  //       "authizationFormText": "Intervention Approval Level 3",
  //       "authorizedAction": 51,
  //       "commentedBy": 1005,
  //       "commentedByName": "TEMP - PHARMACIST ",
  //       "interventionDesc": "Drug information :administration related",
  //       "interventionId": 29,
  //       "remark": "asdfasdf"
  //     },
  //     {
  //       "authizationFormText": "Intervention Approval Level 3",
  //       "authorizedAction": 51,
  //       "commentedBy": 0,
  //       "commentedByName": "lorem ipsum1 ",
  //       "interventionDesc": "",
  //       "interventionId": 1485,
  //       "remark": "loremIpsum"
  //     }, {
  //       "authizationFormText": "Intervention Approval Level 3",
  //       "authorizedAction": 51,
  //       "commentedBy": 1005,
  //       "commentedByName": "TEMP - PHARMACIST ",
  //       "interventionDesc": "Drug information :administration related",
  //       "interventionId": 29,
  //       "remark": "asdfasdf"
  //     },
  //     {
  //       "authizationFormText": "Intervention Approval Level 3",
  //       "authorizedAction": 51,
  //       "commentedBy": 0,
  //       "commentedByName": "lorem ipsum1 ",
  //       "interventionDesc": "",
  //       "interventionId": 1485,
  //       "remark": "loremIpsum"
  //     }, {
  //       "authizationFormText": "Intervention Approval Level 3",
  //       "authorizedAction": 51,
  //       "commentedBy": 1005,
  //       "commentedByName": "TEMP - PHARMACIST ",
  //       "interventionDesc": "Drug information :administration related",
  //       "interventionId": 29,
  //       "remark": "asdfasdf"
  //     },
  //     {
  //       "authizationFormText": "Intervention Approval Level 3",
  //       "authorizedAction": 51,
  //       "commentedBy": 0,
  //       "commentedByName": "lorem ipsum1 ",
  //       "interventionDesc": "",
  //       "interventionId": 1485,
  //       "remark": "loremIpsum"
  //     }, {
  //       "authizationFormText": "Intervention Approval Level 3",
  //       "authorizedAction": 51,
  //       "commentedBy": 1005,
  //       "commentedByName": "TEMP - PHARMACIST ",
  //       "interventionDesc": "Drug information :administration related",
  //       "interventionId": 29,
  //       "remark": "asdfasdf"
  //     },
  //     {
  //       "authizationFormText": "Intervention Approval Level 3",
  //       "authorizedAction": 51,
  //       "commentedBy": 0,
  //       "commentedByName": "lorem ipsum1 ",
  //       "interventionDesc": "",
  //       "interventionId": 1485,
  //       "remark": "loremIpsum"
  //     }, {
  //       "authizationFormText": "Intervention Approval Level 3",
  //       "authorizedAction": 51,
  //       "commentedBy": 1005,
  //       "commentedByName": "TEMP - PHARMACIST ",
  //       "interventionDesc": "Drug information :administration related",
  //       "interventionId": 29,
  //       "remark": "asdfasdf"
  //     },
  //     {
  //       "authizationFormText": "Intervention Approval Level 3",
  //       "authorizedAction": 51,
  //       "commentedBy": 0,
  //       "commentedByName": "lorem ipsum1 ",
  //       "interventionDesc": "",
  //       "interventionId": 1485,
  //       "remark": "loremIpsum"
  //     }, {
  //       "authizationFormText": "Intervention Approval Level 3",
  //       "authorizedAction": 51,
  //       "commentedBy": 1005,
  //       "commentedByName": "TEMP - PHARMACIST ",
  //       "interventionDesc": "Drug information :administration related",
  //       "interventionId": 29,
  //       "remark": "asdfasdf"
  //     },
  //     {
  //       "authizationFormText": "Intervention Approval Level 3",
  //       "authorizedAction": 51,
  //       "commentedBy": 0,
  //       "commentedByName": "lorem ipsum1 ",
  //       "interventionDesc": "",
  //       "interventionId": 1485,
  //       "remark": "loremIpsum"
  //     }, {
  //       "authizationFormText": "Intervention Approval Level 3",
  //       "authorizedAction": 51,
  //       "commentedBy": 1005,
  //       "commentedByName": "TEMP - PHARMACIST ",
  //       "interventionDesc": "Drug information :administration related",
  //       "interventionId": 29,
  //       "remark": "asdfasdf"
  //     },
  //     {
  //       "authizationFormText": "Intervention Approval Level 3",
  //       "authorizedAction": 51,
  //       "commentedBy": 0,
  //       "commentedByName": "lorem ipsum1 ",
  //       "interventionDesc": "",
  //       "interventionId": 1485,
  //       "remark": "loremIpsum"
  //     }, {
  //       "authizationFormText": "Intervention Approval Level 3",
  //       "authorizedAction": 51,
  //       "commentedBy": 1005,
  //       "commentedByName": "TEMP - PHARMACIST ",
  //       "interventionDesc": "Drug information :administration related",
  //       "interventionId": 29,
  //       "remark": "asdfasdf"
  //     },
  //     {
  //       "authizationFormText": "Intervention Approval Level 3",
  //       "authorizedAction": 51,
  //       "commentedBy": 0,
  //       "commentedByName": "lorem ipsum1 ",
  //       "interventionDesc": "",
  //       "interventionId": 1485,
  //       "remark": "loremIpsum"
  //     }, {
  //       "authizationFormText": "Intervention Approval Level 3",
  //       "authorizedAction": 51,
  //       "commentedBy": 1005,
  //       "commentedByName": "TEMP - PHARMACIST ",
  //       "interventionDesc": "Drug information :administration related",
  //       "interventionId": 29,
  //       "remark": "asdfasdf"
  //     },
  //     {
  //       "authizationFormText": "Intervention Approval Level 3",
  //       "authorizedAction": 51,
  //       "commentedBy": 0,
  //       "commentedByName": "lorem ipsum1 ",
  //       "interventionDesc": "",
  //       "interventionId": 1485,
  //       "remark": "loremIpsum"
  //     }, {
  //       "authizationFormText": "Intervention Approval Level 3",
  //       "authorizedAction": 51,
  //       "commentedBy": 1005,
  //       "commentedByName": "TEMP - PHARMACIST ",
  //       "interventionDesc": "Drug information :administration related",
  //       "interventionId": 29,
  //       "remark": "asdfasdf"
  //     },
  //     {
  //       "authizationFormText": "Intervention Approval Level 3",
  //       "authorizedAction": 51,
  //       "commentedBy": 0,
  //       "commentedByName": "lorem ipsum1 ",
  //       "interventionDesc": "",
  //       "interventionId": 1485,
  //       "remark": "loremIpsum"
  //     }, {
  //       "authizationFormText": "Intervention Approval Level 3",
  //       "authorizedAction": 51,
  //       "commentedBy": 1005,
  //       "commentedByName": "TEMP - PHARMACIST ",
  //       "interventionDesc": "Drug information :administration related",
  //       "interventionId": 29,
  //       "remark": "asdfasdf"
  //     },
  //     {
  //       "authizationFormText": "Intervention Approval Level 3",
  //       "authorizedAction": 51,
  //       "commentedBy": 0,
  //       "commentedByName": "lorem ipsum1 ",
  //       "interventionDesc": "",
  //       "interventionId": 1485,
  //       "remark": "loremIpsum"
  //     }, {
  //       "authizationFormText": "Intervention Approval Level 3",
  //       "authorizedAction": 51,
  //       "commentedBy": 1005,
  //       "commentedByName": "TEMP - PHARMACIST ",
  //       "interventionDesc": "Drug information :administration related",
  //       "interventionId": 29,
  //       "remark": "asdfasdf"
  //     },
  //     {
  //       "authizationFormText": "Intervention Approval Level 3",
  //       "authorizedAction": 51,
  //       "commentedBy": 0,
  //       "commentedByName": "lorem ipsum1 ",
  //       "interventionDesc": "",
  //       "interventionId": 1485,
  //       "remark": "loremIpsum"
  //     }, {
  //       "authizationFormText": "Intervention Approval Level 3",
  //       "authorizedAction": 51,
  //       "commentedBy": 1005,
  //       "commentedByName": "TEMP - PHARMACIST ",
  //       "interventionDesc": "Drug information :administration related",
  //       "interventionId": 29,
  //       "remark": "asdfasdf"
  //     },
  //     {
  //       "authizationFormText": "Intervention Approval Level 3",
  //       "authorizedAction": 51,
  //       "commentedBy": 0,
  //       "commentedByName": "lorem ipsum1 ",
  //       "interventionDesc": "",
  //       "interventionId": 1485,
  //       "remark": "loremIpsum"
  //     },
  //   ],
  //   "rowcount": 2,
  //   "statusMessage": null,
  //   "success": null
  // });

  bool isInfectiousDiseaseConsultant = false;

  String nursingStationId = '';
  String admissionId = '';
  String patientID = '';
  String fromDate = '';
  String toDate = '';
  NursingStationEntity? entity;

  String getDate(String dateTime) {
    if (dateTime.isEmpty) return '';
    DateTime now = DateTime.now();
    return DateFormat('dd MMM yyyy').format(now);
  }

  Future getNursingStations() async {
    nursingStations = await _service.getNursingStation();
  }

  Future getPharmacyIntervention(
      {String nursingStationId = '0',
      String admissionId = '0',
      String patientID = '0',
      String fromDate = '',
      String toDate = '',
        NursingStationEntity? entity,
      }) async {
    setState(ViewState.BusyLocal);

    MedicationList? result = await _service.getMedicationList(
        nursingStationId: nursingStationId,
        admissionId: admissionId,
        patientID: patientID,
        fromDate: fromDate,
        toDate: toDate);

    this.nursingStationId = nursingStationId;
    this.admissionId = admissionId;
    this.patientID = patientID;
    this.fromDate = fromDate;
    this.toDate = toDate;
    this.entity = entity;

    if (_service.hasError || result == null) {
      error = _service.error;
      medicationList = null;
      setState(ViewState.ErrorLocal);
      return;
    }
    medicationList = result;
    setState(ViewState.Idle);
  }

  Future getInfectiousDiseaseConsultantStatus() async {
    setState(ViewState.BusyLocal);
    bool result = await _service.getInfectiousDiseaseConsultantStatus();
    if (_service.hasError) {
      error = _service.error;
      setState(ViewState.ErrorLocal);
      return;
    }
    isInfectiousDiseaseConsultant = result;
    setState(ViewState.Idle);
  }

  Future updateInterventionDiseaseStatus({
    bool isAccepted = false,
    String remarks = '',
    String interventionID = '',
    String errorMessage = '',
    String successMessage = ''
  }) async {
    Map<String, dynamic>? user = await sharedPref.getObj(LOGGED_IN_USER);
    var userId = user?['List_MemberInformation'][0]['MemberID'] ?? doctorProfile?.doctorID;
    if(userId == null) {
      return;
    }
    var requestJson = {
      "PatientID":currentlySelectedMedication?.patientID ?? '',
      'AdmissionNo': currentlySelectedMedication?.admissionNo.toString() ?? '',
      "PrescriptionNo":
          currentlySelectedMedication?.prescriptionNo.toString() ?? '',
      "OrderNo": currentlySelectedMedication?.orderNo.toString() ?? '',
      "ItemID": currentlySelectedMedication?.itemID.toString() ?? '',
      "LineItemNo": currentlySelectedMedication?.lineItemNo.toString() ?? '',
      "Remarks": remarks ?? '',
      "InterventionID": interventionID,
      "AuthorizeID": userId,
      "Status": currentlySelectedMedication?.status.toString() ?? '',
      "IsAccepted": isAccepted
    };
    setState(ViewState.BusyLocal);
    bool result = await _service.updateInterventionStatus(requestJson);

    if (_service.hasError) {
      error = _service.error;
      setState(ViewState.ErrorLocal);
      DrAppToastMsg.showErrorToast(errorMessage);
      return;
    }
    if(result) {
      DrAppToastMsg.showSuccesToast(successMessage);
      await getPharmacyIntervention();
    }
    setState(ViewState.Idle);
  }

  Future getInterventionHistory({
    Medication? medication,
  }) async {
    currentlySelectedMedication = null;
    setState(ViewState.BusyLocal);
    DoctorProfileModel doctorProfile = DoctorProfileModel.fromJson(await sharedPref.getObj(DOCTOR_PROFILE));
    InterventionHistoryList? result = await _service.getInfectiousDiseaseHistory(
      admissionNumber: medication?.admissionNo ?? 0,
      prescriptionNumber: medication?.prescriptionNo ?? 0,
      orderNumber: medication?.orderNo ?? 0,
      itemID: medication?.itemID ?? 0,
      memberID: doctorProfile.doctorID ?? 0
    );

    // await _service.getInfectiousDiseaseHistory(
    //     admissionNumber: medication?.admissionNo?.toString() ?? '',
    //     prescriptionNumber: medication?.prescriptionNo?.toString() ?? '',
    //     orderNumber: medication?.orderNo?.toString() ?? '',
    //     itemID: medication?.itemID?.toString() ?? '');
    if (_service.hasError) {
      error = _service.error;
      setState(ViewState.ErrorLocal);
      return;
    }
    currentlySelectedMedication = medication;
    interventionHistoryList = result;
    setState(ViewState.Idle);
  }

  toggleShowBottomSheetValue() {
    interventionHistoryList = null;
  }

  toggleBottomOpenValue(bool bottomSheetValue){
    isBottomSheetShown = bottomSheetValue;
  }
}
