// Copyright © 2022 Olo Inc. All rights reserved.
// This software is made available under the Olo Pay SDK License (See LICENSE.md file)
//
//  DigitalWalletButtonController.swift
//  OlopaysdkReactNative
//
//  Created by Justin Anderson on 4/18/25.
//
import OloPaySDK
import PassKit

class DigitalWalletButtonController : UIViewController {
    private let _defaultCornerRadius = 8.0
    private let _applePayButton = OPApplePayButton()
    
    private var _applePayButtonStyle: PKPaymentButtonStyle? = nil
    private var _applePayButtonType: PKPaymentButtonType? = nil
    private var _applePayButtonCornerRadius: CGFloat? = nil
    
    var onPress: RCTDirectEventBlock? = nil
    
    public var isEnabled: Bool = true {
        didSet {
            _applePayButton.isEnabled = isEnabled
        }
    }
    
    var buttonConfig: NSDictionary = NSDictionary() {
        didSet {
            let style =
                buttonConfig[DataKeys.ApplePayButtonStyle] as? String ??
                DataKeys.ApplePayButtonStyleBlack
            
            let type =
                buttonConfig[DataKeys.ApplePayButtonType] as? String ??
                DataKeys.ApplePayButtonTypeCheckout
            
            let cornerRadius =
                buttonConfig[DataKeys.CornerRadiusKey] as? Double ??
                _defaultCornerRadius
            
            updateButton(
                type: PKPaymentButtonType.convert(from: type),
                style: PKPaymentButtonStyle.convert(from: style),
                cornerRadius: cornerRadius
            )
        }
    }
    
    override func viewDidLoad() {
        setupViews()
    }
    
    func setupViews() {
        view.addSubview(_applePayButton)
        _applePayButton.translatesAutoresizingMaskIntoConstraints = false
        let constraints = [
            _applePayButton.topAnchor.constraint(equalTo: self.view.topAnchor),
            _applePayButton.bottomAnchor.constraint(equalTo: self.view.bottomAnchor),
            _applePayButton.leftAnchor.constraint(equalTo: self.view.leftAnchor),
            _applePayButton.rightAnchor.constraint(equalTo: self.view.rightAnchor)
        ]
        
        NSLayoutConstraint.activate(constraints)
    }
    
    private func onButtonClicked() {
        onPress?(nil)
    }
    
    func updateButton(
        type: PKPaymentButtonType = .checkout,
        style: PKPaymentButtonStyle = .black,
        cornerRadius: CGFloat = 8.0
    ) {
        guard !buttonStylesMatch(type: type, style: style, cornerRadius: cornerRadius) else {
            return //Don't update if there's no change in styles
        }
        
        guard !onlyCornerRadiusChange(type: type, style: style, cornerRadius: cornerRadius) else {
            // If only the corner radius changes we can update the radius without having to
            // create a new button. This is much more efficient
            _applePayButton.updateCornerRadius(cornerRadius: cornerRadius)
            _applePayButtonCornerRadius = cornerRadius
            return
        }
        
        _applePayButtonType = type
        _applePayButtonStyle = style
        _applePayButtonCornerRadius = cornerRadius
        
        _applePayButton.updateButton(
            type: type,
            style: style,
            cornerRadius: cornerRadius
        )
        _applePayButton.onClick = onButtonClicked
    }
    
    private func buttonStylesMatch(
        type: PKPaymentButtonType,
        style: PKPaymentButtonStyle,
        cornerRadius: CGFloat
    ) -> Bool {
        return
            _applePayButtonType == type &&
            _applePayButtonStyle == style &&
            _applePayButtonCornerRadius == cornerRadius
    }
    
    private func onlyCornerRadiusChange(
        type: PKPaymentButtonType,
        style: PKPaymentButtonStyle,
        cornerRadius: CGFloat
    ) -> Bool {
        return
            _applePayButtonType == type &&
            _applePayButtonStyle == style &&
            _applePayButtonCornerRadius != cornerRadius
    }
}
