//
//  ExpressCheckoutLauncher.swift
//  Hyperswitch
//
//  Created by Harshit Srivastava on 21/02/24.
//

import Foundation
import React
import WebKit

@frozen public enum ExpressCheckoutResult {
    case completed(data: String)
    case canceled(data: String)
    case failed(error: Error)
}

public class ExpressCheckoutLauncher {

    init() {}

    static var configuration: PaymentSheet.Configuration?
    static var sdkAuthorization: String?
    static var completion: ((ExpressCheckoutResult) -> Void)?
    static var themes: String?

    public convenience init(
        sdkAuthorization: String,
        configuration: PaymentSheet.Configuration,
        themes: String? = nil,
        completion: @escaping ((ExpressCheckoutResult) -> Void)
    ) {

        self.init()

        ExpressCheckoutLauncher.configuration = configuration
        ExpressCheckoutLauncher.sdkAuthorization = sdkAuthorization
        ExpressCheckoutLauncher.themes = themes
        ExpressCheckoutLauncher.completion = completion

        let props: [String: Any] = [
            "publishableKey": APIClient.shared.publishableKey as Any,
            "profileId": APIClient.shared.profileId as Any,
            "sdkAuthorization": sdkAuthorization,
            "paymentMethodType": "expressCheckout",
            "paymentMethodData": "",
            "confirm": false,
        ]
        //        HyperModule.shared?.confirmEC(data: props) //MARK: WIP
    }

    public func launchPaymentSheet(paymentResult: NSMutableDictionary, callBack: @escaping RCTResponseSenderBlock) {

        DispatchQueue.main.async {

            RNViewManager.sharedInstance.responseHandler = self

            let hyperParams = SDKParams.getSDKParams()

            let props: [String: Any] = [
                "type": "widgetPayment",
                "sdkAuthorization": ExpressCheckoutLauncher.sdkAuthorization as Any,
                "publishableKey": APIClient.shared.publishableKey as Any,
                "profileId": APIClient.shared.profileId as Any,
                "hyperParams": hyperParams,
                "customBackendUrl": APIClient.shared.customBackendUrl as Any,
                "customLogUrl": APIClient.shared.customLogUrl as Any,
                "customParams": APIClient.shared.customParams as Any,
            ]

            let rootView = RNViewManager.sharedInstance.viewForModule("hyperSwitch", initialProperties: ["props": props])

            rootView.backgroundColor = UIColor.clear

            let paymentSheetViewController = UIViewController()
            paymentSheetViewController.modalPresentationStyle = .overFullScreen
            paymentSheetViewController.view = rootView

            RCTPresentedViewController()!.present(paymentSheetViewController, animated: false)
        }
    }

}

extension ExpressCheckoutLauncher: RNResponseHandler {
    func didReceiveResponse(response: String?, error: Error?) {

        if let completion = ExpressCheckoutLauncher.completion {
            if let error = error {
                completion(.failed(error: error))
            } else if response == "cancelled" {
                completion(.canceled(data: "cancelled"))
            } else {
                completion(.completed(data: response ?? "failed"))
            }
        }
    }
}

extension ExpressCheckoutLauncher {
    public func confirm() {

        ExpressCheckoutLauncher.completion = ExpressCheckoutLauncher.completion
        RNViewManager.sharedInstance.responseHandler = self

        var props: [String: Any] = [
            "publishableKey": APIClient.shared.publishableKey as Any,
            "profileId": APIClient.shared.profileId as Any,
            "sdkAuthorization": ExpressCheckoutLauncher.sdkAuthorization as Any,
            "paymentMethodType": "expressCheckout",
            "paymentMethodData": "",
            "confirm": true,
        ]
        //        HyperModule.shared?.confirmEC(data: props) //MARK: WIP
    }
}
