// Copyright © 2022 Olo Inc. All rights reserved.
// This software is made available under the Olo Pay SDK License (See LICENSE.md file)

#import "DigitalWalletButtonComponentView.h"
#import <React/RCTConversions.h>
#import <React/RCTFabricComponentsPlugins.h>
#import <react/renderer/components/OlopaysdkReactNativeSpec/ComponentDescriptors.h>
#import <react/renderer/components/OlopaysdkReactNativeSpec/EventEmitters.h>
#import <react/renderer/components/OlopaysdkReactNativeSpec/Props.h>
#import <react/renderer/components/OlopaysdkReactNativeSpec/RCTComponentViewHelpers.h>
#import "OloPaySDKForwardDeclarations.h"
#import "olopaysdk_react_native-Swift.h"

using namespace facebook::react;

@interface DigitalWalletButtonComponentView () <RCTDigitalWalletButtonViewProtocol>
@end

@implementation DigitalWalletButtonComponentView {
    DigitalWalletButton *_swiftView;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        static const auto defaultProps = std::make_shared<const DigitalWalletButtonProps>();
        _props = defaultProps;

        _swiftView = [[DigitalWalletButton alloc] initWithFrame:self.bounds];
        _swiftView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

        __weak __typeof__(self) weakSelf = self;
        _swiftView.onClicked = ^{
            [weakSelf sendClickedEvent];
        };

        self.contentView = _swiftView;
    }
    return self;
}

#pragma mark - RCTComponentViewProtocol

+ (ComponentDescriptorProvider)componentDescriptorProvider
{
    return concreteComponentDescriptorProvider<DigitalWalletButtonComponentDescriptor>();
}

- (void)updateProps:(Props::Shared const &)props oldProps:(Props::Shared const &)oldProps
{
    const auto &oldViewProps = *std::static_pointer_cast<DigitalWalletButtonProps const>(_props);
    const auto &newViewProps = *std::static_pointer_cast<DigitalWalletButtonProps const>(props);

    if (oldViewProps.isEnabled != newViewProps.isEnabled) {
        _swiftView.isEnabled = newViewProps.isEnabled;
    }

    // Update Apple Pay config - always update (struct comparison not supported)
    {
        NSMutableDictionary *config = [NSMutableDictionary dictionary];

        if (!newViewProps.applePayConfig.buttonType.empty()) {
            config[@"buttonType"] = RCTNSStringFromString(newViewProps.applePayConfig.buttonType);
        }
        if (!newViewProps.applePayConfig.buttonStyle.empty()) {
            config[@"buttonStyle"] = RCTNSStringFromString(newViewProps.applePayConfig.buttonStyle);
        }
        if (newViewProps.applePayConfig.cornerRadius > 0) {
            config[@"cornerRadius"] = @(newViewProps.applePayConfig.cornerRadius);
        }

        [_swiftView updateApplePayConfig:config];
    }

    [super updateProps:props oldProps:oldProps];
}

#pragma mark - Event Emitters

- (void)sendClickedEvent
{
    if (_eventEmitter) {
        auto viewEventEmitter = std::static_pointer_cast<DigitalWalletButtonEventEmitter const>(_eventEmitter);
        viewEventEmitter->onClickedEvent({});
    }
}

@end

Class<RCTComponentViewProtocol> DigitalWalletButtonCls(void)
{
    return DigitalWalletButtonComponentView.class;
}
