import 'package:flutter/material.dart';
import 'package:hmg_patient_app_new/features/authentication/models/resp_models/authenticated_user_resp_model.dart';
import 'package:hmg_patient_app_new/features/radiology/radiology_repo.dart';
import 'package:hmg_patient_app_new/services/error_handler_service.dart';

import 'models/resp_models/patient_radiology_response_model.dart';

class RadiologyViewModel extends ChangeNotifier {
  bool isRadiologyOrdersLoading = false;
  bool isRadiologyPDFReportLoading = false;

  RadiologyRepo radiologyRepo;
  ErrorHandlerService errorHandlerService;

  List<PatientRadiologyResponseModel> patientRadiologyOrders = [];

  String radiologyImageURL = "";
  String patientRadiologyReportPDFBase64 = "";

  RadiologyViewModel({required this.radiologyRepo, required this.errorHandlerService});

  initRadiologyProvider() {
    patientRadiologyOrders.clear();
    isRadiologyOrdersLoading = true;
    isRadiologyPDFReportLoading = true;
    radiologyImageURL = "";
    getPatientRadiologyOrders();
    notifyListeners();
  }

  Future<void> getPatientRadiologyOrders({Function(dynamic)? onSuccess, Function(String)? onError}) async {
    final result = await radiologyRepo.getPatientRadiologyOrders(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) {
          patientRadiologyOrders = apiResponse.data!;
          isRadiologyOrdersLoading = false;
          notifyListeners();
          if (onSuccess != null) {
            onSuccess(apiResponse);
          }
        }
      },
    );
  }

  Future<void> getRadiologyImage({required PatientRadiologyResponseModel patientRadiologyResponseModel, Function(dynamic)? onSuccess, Function(String)? onError}) async {
    final result = await radiologyRepo.getRadiologyImage(patientRadiologyResponseModel: patientRadiologyResponseModel);

    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) {
          radiologyImageURL = apiResponse.data!;
          notifyListeners();
          if (onSuccess != null) {
            onSuccess(apiResponse);
          }
        }
      },
    );
  }

  Future<void> getRadiologyPDF(
      {required PatientRadiologyResponseModel patientRadiologyResponseModel, required AuthenticatedUser authenticatedUser, Function(dynamic)? onSuccess, Function(String)? onError}) async {
    final result = await radiologyRepo.getRadiologyReportPDF(patientRadiologyResponseModel: patientRadiologyResponseModel, authenticatedUser: authenticatedUser);

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