#if !defined(RCT_NEW_ARCH_ENABLED)

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

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

@interface RNScanbotDocumentScannerLegacyView() <RNScanbotDocumentScannerEventDelegate>
@property (strong, nonatomic) RNScanbotDocumentScannerViewController *scannerViewController;
@property (nonatomic) BOOL attached;
@end

@implementation RNScanbotDocumentScannerLegacyView

- (instancetype)initWithFrame:(CGRect)frame
{
    if(self = [super initWithFrame:frame]){
        self.attached = false;
        
        self.scannerViewController = [[RNScanbotDocumentScannerViewController 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) enabled {
    [self.scannerViewController flashEnabled:enabled];
}

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

- (void) setDetectDocumentAfterSnap:(BOOL) enabled {
    [self.scannerViewController detectDocumentAfterSnap:enabled];
}

// MARK: Document Configuration

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

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

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

- (void) setIgnoreOrientationMismatch:(BOOL) enabled {
    [self.scannerViewController ignoreOrientationMismatch:enabled];
}

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

- (void) setRequiredAspectRatios:(NSArray * _Nullable) value {
    [self.scannerViewController requiredAspectRatios:value];
}

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

- (void) setAutoSnappingEnabled:(BOOL) enabled {
    [self.scannerViewController autoSnappingEnabled:enabled];
}

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

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

// MARK: Camera Configuration

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

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

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

// MARK: Finder Configuration

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

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

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

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

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

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

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

// MARK: Polygon Configuration

- (void) setPolygonEnabled:(BOOL) value {
    [self.scannerViewController polygonEnabled:value];
}

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

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

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

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

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

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

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

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

- (void) setPolygonAutoSnapProgressEnabled:(BOOL) value {
    [self.scannerViewController polygonAutoSnapProgressEnabled:value];
}

// MARK: Commands

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

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

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

//MARK: Delegate Events

- (void)onScannedDocument:(SBSDKImageRef * _Nonnull)originalImageRef :(SBSDKImageRef * _Nullable)croppedImageRef :(NSString * _Nullable)detectionResult {
    if(!self.onDocumentScannerResult){
        return;
    }
    
    self.onDocumentScannerResult(@{
        @"originalImage": [originalImageRef serialize],
        @"documentImage": croppedImageRef ? [croppedImageRef serialize] : @"",
        @"detectionResult": detectionResult ? detectionResult : @""
    });
}

- (void)onDocumentDetectionStatus:(NSString * _Nonnull)status {
    if(!self.onDetectionResult){
        return;
    }
    
    self.onDetectionResult(@{@"result": status});
}

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


@end

#endif
