import 'package:amazon_payfort/amazon_payfort.dart';
import 'package:hmg_patient_app/config/config.dart';
import 'package:hmg_patient_app/core/enum/PayfortEnums.dart';
import 'package:hmg_patient_app/locator.dart';
import 'package:hmg_patient_app/services/payfort_services/payfort_project_details_resp_model.dart';
import 'package:hmg_patient_app/services/payfort_services/payfort_service.dart';
import 'package:hmg_patient_app/uitl/app_toast.dart';
import 'package:hmg_patient_app/uitl/gif_loader_dialog_utils.dart';
import 'package:hmg_patient_app/uitl/utils.dart';
import 'package:flutter/cupertino.dart';

class PayfortViewModel extends ChangeNotifier {
  PayfortService _payfortService = locator<PayfortService>();

  Future<void> initPayfort() async {
    await _payfortService.initPayfortSDK();
  }

  Future<PayfortProjectDetailsRespModel?> getProjectDetailsForPayfort({int? serviceId, int? projectId, int? languageID}) async {
    PayfortProjectDetailsRespModel payfortProjectDetailsRespModel = PayfortProjectDetailsRespModel();
    try {
      payfortProjectDetailsRespModel = await _payfortService.getPayfortConfigurations(serviceId: serviceId, projectId: projectId, languageID: languageID);

      String signature =
          await _payfortService.getPayfortSignature(payfortProjectDetailsRespModel.accessCode!, payfortProjectDetailsRespModel.merchantIdentifier!, payfortProjectDetailsRespModel.shaRequest!);
      payfortProjectDetailsRespModel.signature = signature;

      return payfortProjectDetailsRespModel;
    } on Exception catch (e, s) {
      print(s);
      return null;
    }
  }

  Future<void> addPayfortApplePayResponse(num patientID, {PayFortResult? result}) async {
    try {
      await _payfortService.addPayfortApplePayResponse(patientID, result: result);
    } on Exception catch (e, s) {
      print(s);
      return null;
    }
  }

  Future<void> initiateApplePayWithPayfort({
    String? customerName,
    String? customerEmail,
    String? orderDescription,
    String? merchantReference,
    num? orderAmount,
    PayfortProjectDetailsRespModel? payfortProjectDetailsRespModel,
    SucceededCallback? onSuccess,
    FailedCallback? onFailed,
    ServiceTypeEnum? serviceTypeEnum,
    int? projectId,
    String? currency,
  }) async {
    // GifLoaderDialogUtils.showMyDialog(AppGlobal.context);
    // PayfortProjectDetailsRespModel payfortProjectDetailsRespModel = await getProjectDetailsForPayfort(projectId: projectId, serviceId: serviceTypeEnum.getIdFromServiceEnum());
    //
    // if (payfortProjectDetailsRespModel == null) {
    //   GifLoaderDialogUtils.hideDialog(AppGlobal.context);
    //   return;
    // }

    try {
      await _payfortService.paymentWithApplePay(
        onSucceeded: onSuccess,
        onFailed: onFailed,
        customerName: customerName,
        customerEmail: customerEmail,
        orderDescription: orderDescription,
        orderAmount: orderAmount,
        merchantReference: merchantReference,
        merchantIdentifier: payfortProjectDetailsRespModel!.merchantIdentifier ?? "",
        applePayAccessCode: payfortProjectDetailsRespModel!.accessCode ?? "",
        applePayShaRequestPhrase: payfortProjectDetailsRespModel.shaRequest ?? "",
        currency: currency!,
      );
      GifLoaderDialogUtils.hideDialog(AppGlobal.context);
    } catch (e) {
      // Utils.showErrorToast(e.toString());
      AppToast.showErrorToast(message: e.toString(), localContext: AppGlobal.context);
      GifLoaderDialogUtils.hideDialog(AppGlobal.context);
      throw e;
    }
  }
}
