#if !defined(RCT_NEW_ARCH_ENABLED)

#import <ScanbotSDK/ScanbotSDK-Swift.h>
#import "RNScanbotBarcodeScannerLegacyView.h"

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

@interface RNScanbotBarcodeScannerLegacyView() <RNScanbotBarcodeScannerEventDelegate>
@property (strong, nonatomic) RNScanbotBarcodeScannerViewController *scannerViewController;
@property (nonatomic) BOOL attached;
@end

@implementation RNScanbotBarcodeScannerLegacyView

- (instancetype)initWithFrame:(CGRect)frame
{
    
    if (self = [super initWithFrame:frame]) {
        self.attached = false;
        
        self.scannerViewController = [[RNScanbotBarcodeScannerViewController alloc] init];
        [self.scannerViewController setDefaults];
        self.scannerViewController.delegate = self;
    }
    
    return self;
}

- (void)didMoveToWindow {
    [super didMoveToWindow];
    
    if(self.window && !self.attached){
        [self.scannerViewController attachControllerWithParentViewController:[self reactViewController] inView:self];
        self.attached = true;
    }
}

- (void)removeFromSuperview {
    
    self.scannerViewController.delegate = nil;
    [self.scannerViewController detachController];
    self.scannerViewController = nil;
    
    [super removeFromSuperview];
}

//MARK: General Configuration

- (void) setFlashEnabled:(BOOL)value {
    [self.scannerViewController flashEnabled:value];
}

- (void) setScanningEnabled:(BOOL)value {
    [self.scannerViewController scanningEnabled:value];
}

- (void) setHardwareButtonsEnabled:(BOOL) value {
    [self.scannerViewController hardwareButtonsEnabled:value];
}

// MARK: Finder Configuration

- (void) setFinderEnabled:(BOOL)value {
    [self.scannerViewController finderEnabled:value];
}

- (void) setFinderStrokeWidth:(NSNumber * _Nullable)value {
    [self.scannerViewController finderStrokeWidth:value];
}

- (void) setFinderStrokeColor:(UIColor * _Nullable)value {
    [self.scannerViewController finderStrokeColor:value];
}

- (void) setFinderOverlayColor:(UIColor * _Nullable)value {
    [self.scannerViewController finderOverlayColor:value];
}

- (void) setFinderInset:(NSDictionary * _Nullable)value {
    [self.scannerViewController finderInsets:value];
}

- (void) setFinderRequiredAspectRatios:(NSDictionary * _Nullable)value {
    [self.scannerViewController finderRequiredAspectRatios:value];
}

// MARK: Camera Configuration

- (void) setCameraZoomFactor:(NSNumber * _Nullable)value {
    [self.scannerViewController cameraZoomFactor:value];
}

- (void) setCameraZoomRange:(NSDictionary * _Nullable)value {
    [self.scannerViewController cameraZoomRange:value];
}

- (void) setMinFocusDistanceLock:(BOOL)value {
    [self.scannerViewController minFocusDistanceLock:value];
}

- (void) setCameraModule:(NSString * _Nullable)value {
    [self.scannerViewController cameraDevice:value];
}

// MARK: Overlay Configuration

- (void) setOverlayEnabled:(BOOL) value {
    [self.scannerViewController overlayEnabled:value];
}

- (void) setOverlayPolygonColor:(UIColor * _Nullable) value {
    [self.scannerViewController overlayPolygonColor:value];
}

- (void) setOverlayStrokeColor:(UIColor * _Nullable) value {
    [self.scannerViewController overlayStrokeColor:value];
}

- (void) setOverlayTextColor:(UIColor * _Nullable) value {
    [self.scannerViewController overlayTextColor:value];
}

- (void) setOverlayTextContainerColor:(UIColor * _Nullable) value {
    [self.scannerViewController overlayTextContainerColor:value];
}

- (void) setOverlayTextFormat:(NSString * _Nullable) value {
    [self.scannerViewController overlayTextFormat:value];
}

- (void) setOverlayLoadingTextValue:(NSString * _Nullable) value {
    [self.scannerViewController overlayLoadingTextValue:value];
}

- (void) setOverlayBarcodeItemOverlayViewBinder:(BOOL) value {
    [self.scannerViewController overlayViewBinder:value];
}

// MARK: Barcode Configuration

- (void) setConfigFormatConfigurations: (NSArray<NSDictionary<NSString *, id> *> * _Nullable)containerConfigurations {
    [self.scannerViewController configFormatConfigurations:containerConfigurations];
}

- (void) setConfigExtractedDocumentFormats: (NSArray<NSString *> * _Nullable)formats {
    [self.scannerViewController configExtractedDocumentFormats:formats];
}

- (void) setConfigOnlyAcceptDocuments: (BOOL)value {
    [self.scannerViewController configOnlyAcceptDocuments:value];
}

- (void) setConfigReturnBarcodeImage: (BOOL)value {
    [self.scannerViewController configReturnBarcodeImage:value];
}

- (void) setConfigEngineMode: (NSString * _Nullable)value {
    [self.scannerViewController configEngineMode:value];
}

- (void) setConfigOptimizedForOverlays: (BOOL)value {
    [self.scannerViewController configOptimizedForOverlays:value];
}

- (void) setConfigAccumulation: (NSDictionary *)value {
    [self.scannerViewController configAccumulationConfiguration:value];
}

// MARK: Commands

- (void) freezeCamera {
    [self.scannerViewController freezeCamera];
}

- (void) unfreezeCamera {
    [self.scannerViewController unfreezeCamera];
}

- (void)bindBarcodeItemOverlayView:(nonnull NSString *)barcodeItemUuid bindingConfig:(nonnull NSString *)bindingConfigJson {
    [self.scannerViewController bindBarcodeItemOverlayViewWithBarcodeID:barcodeItemUuid bindingConfigJson:bindingConfigJson];
}

//MARK: Delegate Events

- (void)onError:(NSString * _Nonnull)errorMessage errorCode:(NSInteger)errorCode {
    if(!self.onError){
        return;
    }
    
    self.onError(@{@"message": errorMessage, @"code": @(errorCode)});
}

- (void)onScannedBarcode:(NSString * _Nonnull)barcode {
    if(!self.onBarcodeScannerResult){
        return;
    }
    
    self.onBarcodeScannerResult(@{@"result":barcode});
}

- (void)onSelectedBarcode:(NSString * _Nonnull)barcode {
    if(!self.onSelectBarcodeResult){
        return;
    }
    
    self.onSelectBarcodeResult(@{@"result":barcode});
}

@end

#endif
