#ifndef RCT_NEW_ARCH_ENABLED

#import "BasePaperComponent.h"

#if __has_include(<CheckoutReactNativeComponents/CheckoutReactNativeComponents-Swift.h>)
  #import <CheckoutReactNativeComponents/CheckoutReactNativeComponents-Swift.h>
#else
  #import <CheckoutReactNativeComponents-Swift.h>
#endif

// MARK: - Base Component View Implementation

@implementation RCTBasePaperComponent

- (instancetype)initWithComponentType:(RCTCheckoutComponentType)componentType
{
    if (self = [super initWithFrame:CGRectZero]) {
        _componentType = componentType;
        _componentCreated = NO;
        _actionableComponent = nil;
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    return [self initWithComponentType:RCTCheckoutComponentTypeFlow];
}

- (void)setConfig:(NSDictionary *)config
{
    if (_config == nil || ![_config isEqualToDictionary:config]) {
        _config = [config copy];
        [self createComponent];
    }
}

- (void)createComponent
{
    if (_checkoutVC) {
        [_checkoutVC.view removeFromSuperview];
        _checkoutVC = nil;
        _componentCreated = NO;
    }
    
    NSDictionary *configToUse = _config ? _config : @{};
    
    CheckoutComponentType swiftComponentType = (CheckoutComponentType)_componentType;
    
    [[CheckoutManager sharedInstance] createComponentWithType:swiftComponentType
                                                       config:configToUse
                                          onDimensionsChanged:^(CGSize size) {
        [self handleDimensionChange:size];
    }
                                                   completion:^(UIViewController * _Nullable controller,
                                                               NSError * _Nullable error) {
        if (error || controller == nil) {
            self->_componentCreated = NO;
            return;
        }
        
        dispatch_async(dispatch_get_main_queue(), ^{
            self->_checkoutVC = controller;
            self->_componentCreated = YES;
                        
            [self addSubview:controller.view];
            
            controller.view.frame = self.bounds;
            controller.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        });
    }];
}

- (void)handleDimensionChange:(CGSize)size {
    if (self.onDimensionsChanged) {
        self.onDimensionsChanged(@{
            @"width": @(size.width),
            @"height": @(size.height)
        });
    }
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    if (_checkoutVC && _componentCreated) {
        _checkoutVC.view.frame = self.bounds;
    }
}

- (void)dealloc
{
    if (_checkoutVC) {
        [[CheckoutManager sharedInstance] cleanupComponentWithViewController:_checkoutVC];
        [_checkoutVC.view removeFromSuperview];
        _checkoutVC = nil;
    }
    _actionableComponent = nil;
}

- (void)submit
{
    if (_checkoutVC && _componentCreated) {
        [[CheckoutManager sharedInstance] submitComponentWithViewController:_checkoutVC];
    }
}

- (void)tokenize
{
    if (_checkoutVC && _componentCreated) {
        [[CheckoutManager sharedInstance] tokenizeComponentWithViewController:_checkoutVC];
    }
}

- (void)update:(NSInteger)amount currency:(NSString *)currency
{
  if (_checkoutVC && _componentCreated) {
    [[CheckoutManager sharedInstance] updateComponentWithViewController:_checkoutVC amount: amount currency: currency];
  }
}

@end

// MARK: - Base Manager Implementation

@implementation RCTBasePaperComponentManager

- (instancetype)initWithComponentType:(RCTCheckoutComponentType)componentType
{
    if (self = [super init]) {
        _componentType = componentType;
    }
    return self;
}

- (UIView *)view
{
    RCTBasePaperComponent *component = [[RCTBasePaperComponent alloc] initWithComponentType:_componentType];
    return component;
}

@end

#endif
