import 'package:flutter/material.dart';
import 'package:hmg_patient_app_new/features/my_appointments/models/resp_models/patient_appointment_history_response_model.dart';
import 'package:hmg_patient_app_new/features/my_appointments/models/resp_models/patient_appointment_share_response_model.dart';
import 'package:hmg_patient_app_new/features/my_appointments/my_appointments_repo.dart';
import 'package:hmg_patient_app_new/services/error_handler_service.dart';

class MyAppointmentsViewModel extends ChangeNotifier {
  int selectedTabIndex = 0;

  MyAppointmentsRepo myAppointmentsRepo;
  ErrorHandlerService errorHandlerService;

  bool isMyAppointmentsLoading = false;
  bool isAppointmentPatientShareLoading = false;

  List<PatientAppointmentHistoryResponseModel> patientAppointmentsHistoryList = [];
  PatientAppointmentShareResponseModel? patientAppointmentShareResponseModel;

  MyAppointmentsViewModel({required this.myAppointmentsRepo, required this.errorHandlerService});

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

  initAppointmentsViewModel() {
    patientAppointmentsHistoryList.clear();
    isMyAppointmentsLoading = true;
    isAppointmentPatientShareLoading = true;
    notifyListeners();
  }

  setIsAppointmentsHistoryLoading(bool val) {
    isMyAppointmentsLoading = val;
    notifyListeners();
  }

  setIsPatientAppointmentShareLoading(bool val) {
    isAppointmentPatientShareLoading = val;
    notifyListeners();
  }

  setAppointmentReminder(bool value, PatientAppointmentHistoryResponseModel item) {
    int index = patientAppointmentsHistoryList.indexOf(item);
    if (index != -1) {
      patientAppointmentsHistoryList[index].hasReminder = value;
      notifyListeners();
    }
  }

  Future<void> getPatientAppointments(bool isActiveAppointment, bool isArrivedAppointments, {Function(dynamic)? onSuccess, Function(String)? onError}) async {
    final result = await myAppointmentsRepo.getPatientAppointments(patientId: "3628599", isActiveAppointment: isActiveAppointment, isArrivedAppointments: isArrivedAppointments);

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

  Future<void> getPatientShareAppointment(int projectID, int clinicID, String appointmentNo, {Function(dynamic)? onSuccess, Function(String)? onError}) async {
    final result = await myAppointmentsRepo.getPatientShareAppointment(patientId: "3628599", projectID: projectID, clinicID: clinicID, appointmentNo: appointmentNo);

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

  Future<void> addAdvanceNumberRequest(
      {required String advanceNumber, required String paymentReference, required String appointmentNo, Function(dynamic)? onSuccess, Function(String)? onError}) async {
    final result = await myAppointmentsRepo.addAdvanceNumberRequest(advanceNumber: advanceNumber, paymentReference: paymentReference, appointmentNo: appointmentNo);

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

  Future<void> generateAppointmentQR(
      {required String clinicID, required String projectID, required String appointmentNo, required int isFollowUp, Function(dynamic)? onSuccess, Function(String)? onError}) async {
    final result = await myAppointmentsRepo.generateAppointmentQR(clinicID: clinicID, projectID: projectID, appointmentNo: appointmentNo, isFollowUp: isFollowUp);

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

  Future<void> createAdvancePayment(
      {required String paymentMethodName,
      required int projectID,
      required int clinicID,
      required String appointmentNo,
      required num payedAmount,
      required String paymentReference,
      required String patientID,
      required int patientType,
      Function(dynamic)? onSuccess,
      Function(String)? onError}) async {
    final result = await myAppointmentsRepo.createAdvancePayment(
        paymentMethodName: paymentMethodName,
        projectID: projectID,
        clinicID: clinicID,
        appointmentNo: appointmentNo,
        payedAmount: payedAmount,
        paymentReference: paymentReference,
        patientID: patientID,
        patientType: patientType);

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