import 'package:flutter/material.dart';
import 'package:hmg_patient_app_new/core/dependencies.dart';
import 'package:hmg_patient_app_new/features/authentication/models/resp_models/authenticated_user_resp_model.dart';
import 'package:hmg_patient_app_new/features/medical_file/medical_file_repo.dart';
import 'package:hmg_patient_app_new/features/medical_file/models/family_file_response_model.dart';
import 'package:hmg_patient_app_new/features/medical_file/models/patient_medical_response_model.dart';
import 'package:hmg_patient_app_new/features/medical_file/models/patient_sickleave_response_model.dart';
import 'package:hmg_patient_app_new/features/medical_file/models/patient_vaccine_response_model.dart';
import 'package:hmg_patient_app_new/services/dialog_service.dart';
import 'package:hmg_patient_app_new/services/error_handler_service.dart';

class MedicalFileViewModel extends ChangeNotifier {
  int selectedTabIndex = 0;
  bool isPatientVaccineListLoading = false;
  bool isPatientSickLeaveListLoading = false;
  bool isPatientSickLeavePDFLoading = false;
  bool isPatientMedicalReportsListLoading = false;

  MedicalFileRepo medicalFileRepo;
  ErrorHandlerService errorHandlerService;

  List<PatientVaccineResponseModel> patientVaccineList = [];
  List<PatientSickLeavesResponseModel> patientSickLeaveList = [];

  List<PatientMedicalReportResponseModel> patientMedicalReportList = [];

  List<PatientMedicalReportResponseModel> patientMedicalReportRequestedList = [];
  List<PatientMedicalReportResponseModel> patientMedicalReportReadyList = [];
  List<PatientMedicalReportResponseModel> patientMedicalReportCancelledList = [];

  List<FamilyFileResponseModelLists> patientFamilyFiles =[];

  String patientSickLeavePDFBase64 = "";
  String patientMedicalReportPDFBase64 = "";

  int selectedMedicalReportsTabIndex = 0;
  static final DialogService _dialogService = getIt.get<DialogService>();

  MedicalFileViewModel({required this.medicalFileRepo, required this.errorHandlerService});

  initMedicalFileProvider() {
    isPatientVaccineListLoading = true;
    isPatientMedicalReportsListLoading = true;
    notifyListeners();
  }

  void onMedicalReportTabChange(int index) {
    selectedMedicalReportsTabIndex = index;
    if (index == 0) {
      patientMedicalReportList = patientMedicalReportRequestedList;
    } else if (index == 1) {
      patientMedicalReportList = patientMedicalReportReadyList;
    } else if (index == 2) {
      patientMedicalReportList = patientMedicalReportCancelledList;
    }
    notifyListeners();
  }

  setIsPatientVaccineListLoading(bool isLoading) {
    isPatientVaccineListLoading = isLoading;
    notifyListeners();
  }

  setIsPatientSickLeavePDFLoading(bool isLoading) {
    isPatientSickLeavePDFLoading = isLoading;
    notifyListeners();
  }

  setIsPatientSickLeaveListLoading(bool val) {
    if (val) {
      patientSickLeaveList.clear();
      patientSickLeavePDFBase64 = "";
    }
    isPatientSickLeaveListLoading = val;
    notifyListeners();
  }

  setIsPatientMedicalReportsLoading(bool val) {
    if (val) {
      patientMedicalReportList.clear();
      patientMedicalReportPDFBase64 = "";
    }
    isPatientMedicalReportsListLoading = val;
    notifyListeners();
  }

  void onTabChanged(int index) {
    selectedTabIndex = index;
    notifyListeners();
  }

  Future<void> getPatientVaccinesList({Function(dynamic)? onSuccess, Function(String)? onError}) async {
    patientVaccineList.clear();
    final result = await medicalFileRepo.getPatientVaccinesList();

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

  Future<void> getPatientSickLeaveList({Function(dynamic)? onSuccess, Function(String)? onError}) async {
    patientSickLeaveList.clear();
    final result = await medicalFileRepo.getPatientSickLeavesList();

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

  Future<void> getPatientSickLeavePDF(PatientSickLeavesResponseModel patientSickLeavesResponseModel, AuthenticatedUser authenticatedUser,
      {Function(dynamic)? onSuccess, Function(String)? onError}) async {
    final result = await medicalFileRepo.getPatientSickLeavePDF(patientSickLeavesResponseModel, authenticatedUser);

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

  Future<void> getPatientMedicalReportList({Function(dynamic)? onSuccess, Function(String)? onError}) async {
    patientMedicalReportList.clear();
    final result = await medicalFileRepo.getPatientMedicalReportsList();

    result.fold(
      (failure) async => await errorHandlerService.handleError(
        failure: failure,
        onOkPressed: () {
          onError!(failure.message);
        },
      ),
      (apiResponse) {
        if (apiResponse.messageStatus == 2) {
          // dialogService.showErrorDialog(message: apiResponse.errorMessage!, onOkPressed: () {});
        } else if (apiResponse.messageStatus == 1) {
          patientMedicalReportList = apiResponse.data!;
          if (patientMedicalReportList.isNotEmpty) {
            patientMedicalReportRequestedList = patientMedicalReportList.where((element) => element.status == 1).toList();
            patientMedicalReportReadyList = patientMedicalReportList.where((element) => element.status == 2).toList();
            patientMedicalReportCancelledList = patientMedicalReportList.where((element) => element.status == 4).toList();
          }
          onMedicalReportTabChange(0);
          isPatientMedicalReportsListLoading = false;
          notifyListeners();
          if (onSuccess != null) {
            onSuccess(apiResponse);
          }
        }
      },
    );
  }

  Future<void> getPatientMedicalReportPDF(PatientMedicalReportResponseModel patientMedicalReportResponseModel, AuthenticatedUser authenticatedUser,
      {Function(dynamic)? onSuccess, Function(String)? onError}) async {
    final result = await medicalFileRepo.getPatientMedicalReportPDF(patientMedicalReportResponseModel, authenticatedUser);

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


  Future<void> getFamilyFiles({Function(dynamic)? onSuccess, Function(String)? onError}) async {
    final result = await medicalFileRepo.getPatientFamilyFiles();

    result.fold(
          (failure) async => await errorHandlerService.handleError(
        failure: failure,
        onOkPressed: () {
          onError!(failure.message);
        },
      ),
          (apiResponse) {
        if (apiResponse.messageStatus == 2) {
          _dialogService.showErrorBottomSheet(message: apiResponse.errorMessage!, onOkPressed: () {});
        } else if (apiResponse.messageStatus == 1) {
          patientFamilyFiles = apiResponse.data!;
          notifyListeners();
          if (onSuccess != null) {
            onSuccess(apiResponse);
          }
        }
      },
    );
  }

}
