import 'package:flutter/material.dart';
import 'package:hmg_patient_app_new/features/prescriptions/models/resp_models/patient_prescriptions_response_model.dart';
import 'package:hmg_patient_app_new/features/prescriptions/models/resp_models/prescription_detail_response_model.dart';
import 'package:hmg_patient_app_new/features/prescriptions/prescriptions_repo.dart';
import 'package:hmg_patient_app_new/services/error_handler_service.dart';

class PrescriptionsViewModel extends ChangeNotifier {
  bool isPrescriptionsOrdersLoading = false;
  bool isPrescriptionsDetailsLoading = false;

  PrescriptionsRepo prescriptionsRepo;
  ErrorHandlerService errorHandlerService;

  // Prescription Orders Lists
  List<PatientPrescriptionsResponseModel> patientPrescriptionOrders = [];

  List<PrescriptionsList> patientPrescriptionOrdersByClinic = [];
  List<PrescriptionsList> patientPrescriptionOrdersByHospital = [];
  List<PrescriptionsList> patientPrescriptionOrdersViewList = [];

  // Prescription Details List
  List<PrescriptionDetailResponseModel> prescriptionDetailsList = [];

  bool isSortByClinic = true;

  PrescriptionsViewModel({required this.prescriptionsRepo, required this.errorHandlerService});

  initPrescriptionsViewModel() {
    patientPrescriptionOrders.clear();
    patientPrescriptionOrdersByClinic.clear();
    patientPrescriptionOrdersByHospital.clear();
    patientPrescriptionOrdersViewList.clear();
    isPrescriptionsOrdersLoading = true;
    isPrescriptionsDetailsLoading = true;
    isSortByClinic = true;
    getPatientPrescriptionOrders();
    notifyListeners();
  }

  setPrescriptionsDetailsLoading() {
    isPrescriptionsDetailsLoading = true;
    prescriptionDetailsList.clear();
    notifyListeners();
  }

  setPrescriptionItemReminder(bool value, PrescriptionDetailResponseModel item) {
    int index = prescriptionDetailsList.indexOf(item);
    if (index != -1) {
      prescriptionDetailsList[index].hasReminder = value;
      notifyListeners();
    }
  }

  setIsSortByClinic(bool value) {
    isSortByClinic = value;
    if (isSortByClinic) {
      patientPrescriptionOrdersViewList = patientPrescriptionOrdersByClinic;
    } else {
      patientPrescriptionOrdersViewList = patientPrescriptionOrdersByHospital;
    }
    notifyListeners();
  }

  Future<void> getPatientPrescriptionOrders({Function(dynamic)? onSuccess, Function(String)? onError}) async {
    final result = await prescriptionsRepo.getPatientPrescriptionOrders(patientId: "1231755");

    result.fold(
      (failure) async => await errorHandlerService.handleError(failure: failure),
      (apiResponse) {
        if (apiResponse.messageStatus == 2) {
          // dialogService.showErrorDialog(message: apiResponse.errorMessage!, onOkPressed: () {});
        } else if (apiResponse.messageStatus == 1) {
          patientPrescriptionOrders = apiResponse.data!;
          isPrescriptionsOrdersLoading = false;

          for (var element in patientPrescriptionOrders) {
            List<PrescriptionsList> prescriptionsByClinic = patientPrescriptionOrdersByClinic.where((elementClinic) => elementClinic.filterName == element.clinicDescription).toList();

            if (prescriptionsByClinic.isNotEmpty) {
              patientPrescriptionOrdersByClinic[patientPrescriptionOrdersByClinic.indexOf(prescriptionsByClinic[0])].prescriptionsList!.add(element);
            } else {
              patientPrescriptionOrdersByClinic.add(PrescriptionsList(filterName: element.clinicDescription, prescriptions: element));
            }

            List<PrescriptionsList> prescriptionsByHospital = patientPrescriptionOrdersByHospital.where((elementClinic) => elementClinic.filterName == element.name).toList();

            if (prescriptionsByHospital.isNotEmpty) {
              patientPrescriptionOrdersByHospital[patientPrescriptionOrdersByHospital.indexOf(prescriptionsByHospital[0])].prescriptionsList!.add(element);
            } else {
              patientPrescriptionOrdersByHospital.add(PrescriptionsList(filterName: element.name, prescriptions: element));
            }
          }
          patientPrescriptionOrdersViewList = patientPrescriptionOrdersByClinic;
          notifyListeners();
          if (onSuccess != null) {
            onSuccess(apiResponse);
          }
        }
      },
    );
  }

  Future<void> getPrescriptionDetails(PatientPrescriptionsResponseModel prescriptionsResponseModel, {Function(dynamic)? onSuccess, Function(String)? onError}) async {
    final result = await prescriptionsRepo.getPatientPrescriptionDetails(prescriptionsResponseModel: prescriptionsResponseModel);

    result.fold(
      (failure) async => await errorHandlerService.handleError(failure: failure),
      (apiResponse) {
        if (apiResponse.messageStatus == 2) {
          // dialogService.showErrorDialog(message: apiResponse.errorMessage!, onOkPressed: () {});
        } else if (apiResponse.messageStatus == 1) {
          prescriptionDetailsList = apiResponse.data!;
          isPrescriptionsDetailsLoading = false;
          notifyListeners();
          if (onSuccess != null) {
            onSuccess(apiResponse);
          }
        }
      },
    );
  }
}
