
#if !defined(RCT_NEW_ARCH_ENABLED)
#import "RNScanbotBarcodeScannerLegacyView.h"

@interface RNScanbotBarcodeScannerLegacyView()
@property (strong, nonatomic) SBSDKBarcodeScannerViewController *scannerViewController;
@property (strong, nonatomic) UIViewController *vc;
@property (nonatomic) BOOL scanningEnabled;
@property (nonatomic) NSString* _Nullable barcodeItemOverlayViewLoadingText;
@property (atomic) NSMutableDictionary* _Nullable barcodeItemOverlayViewBinders;
@end

@implementation RNScanbotBarcodeScannerLegacyView



- (instancetype)initWithFrame:(CGRect)frame
{

    if (self = [super initWithFrame:frame]) {
        _scanningEnabled = true;
        _barcodeItemOverlayViewLoadingText = nil;
        _barcodeItemOverlayViewBinders = nil;

        self.vc = [[UIViewController alloc] init];
        self.vc.view = self;
        self.scannerViewController = [RNScanbotBarcodeScannerViewControllerWrapper initController];
        self.scannerViewController.delegate = self;
        self.scannerViewController.trackingOverlayController.delegate = self;
    }

    return self;
}

- (void)didMoveToWindow {
    [super didMoveToWindow];
    if(self.window){
        [self.vc sbsdk_attachViewController:self.scannerViewController inView:self];
    } else {
        [self.vc sbsdk_detachViewController:self.scannerViewController];
    }
}

- (void)removeFromSuperview {
    self.scannerViewController = nil;
    self.vc = nil;
    self.barcodeItemOverlayViewLoadingText = nil;
    self.barcodeItemOverlayViewBinders = nil;
    
    [super removeFromSuperview];
}


- (void) setScanningEnabled:(BOOL)value {
    _scanningEnabled = value;
}

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

- (void) setFinderEnabled:(BOOL)value {
    SBSDKBaseScannerViewFinderConfiguration *finderConfig = self.scannerViewController.viewFinderConfiguration;
    [RNScanbotBarcodeScannerViewControllerWrapper setFinderEnabled:finderConfig value:value];
    self.scannerViewController.viewFinderConfiguration = finderConfig;
}

- (void) setFinderStrokeWidth:(int)value {
    SBSDKBaseScannerViewFinderConfiguration *finderConfig = self.scannerViewController.viewFinderConfiguration;
    [RNScanbotBarcodeScannerViewControllerWrapper setFinderStrokeWidth:finderConfig value:value];
    self.scannerViewController.viewFinderConfiguration = finderConfig;
}

- (void) setFinderStrokeColor:(UIColor * _Nullable)value {
    SBSDKBaseScannerViewFinderConfiguration *finderConfig = self.scannerViewController.viewFinderConfiguration;
    [RNScanbotBarcodeScannerViewControllerWrapper setFinderStrokeColor:finderConfig value:value];
    self.scannerViewController.viewFinderConfiguration = finderConfig;
}

- (void) setFinderOverlayColor:(UIColor * _Nullable)value {
    SBSDKBaseScannerViewFinderConfiguration *finderConfig = self.scannerViewController.viewFinderConfiguration;
    [RNScanbotBarcodeScannerViewControllerWrapper setFinderOverlayColor:finderConfig value:value];
    self.scannerViewController.viewFinderConfiguration = finderConfig;
}

- (void) setFinderInset:(NSDictionary * _Nullable)value {
    NSNumber *top = [value valueForKey:@"top"];
    NSNumber *left = [value valueForKey:@"left"];
    NSNumber *bottom = [value valueForKey:@"bottom"];
    NSNumber *right = [value valueForKey:@"right"];

    SBSDKBaseScannerViewFinderConfiguration *finderConfig = self.scannerViewController.viewFinderConfiguration;

    [RNScanbotBarcodeScannerViewControllerWrapper setFinderInset:finderConfig
                                                             top:top != nil ? [top floatValue] : 10
                                                            left:left != nil ? [left floatValue]: 10
                                                          bottom:bottom != nil ? [bottom floatValue] : 10
                                                           right:right != nil ? [right floatValue] : 10];

    self.scannerViewController.viewFinderConfiguration = finderConfig;
}

- (void) setFinderRequiredAspectRatios:(NSDictionary * _Nullable)value {
    NSNumber *width = [value valueForKey:@"width"];
    NSNumber *height = [value valueForKey:@"height"];

    SBSDKBaseScannerViewFinderConfiguration *finderConfig = self.scannerViewController.viewFinderConfiguration;

    [RNScanbotBarcodeScannerViewControllerWrapper setFinderRequiredAspectRatios:finderConfig
                                                                          width:width != nil ? [width doubleValue] : 4
                                                                         height:height != nil ? [height doubleValue] : 3];

    self.scannerViewController.viewFinderConfiguration = finderConfig;
}

- (void) setCameraZoomFactor:(float)value {
    SBSDKBaseScannerZoomConfiguration *zoomConfig = self.scannerViewController.zoomConfiguration;
    [RNScanbotBarcodeScannerViewControllerWrapper setCameraZoomFactor:zoomConfig value:value];
    self.scannerViewController.zoomConfiguration = zoomConfig;
}

- (void) setCameraZoomRange:(NSDictionary * _Nullable)value {
    NSNumber *minZoom = [value valueForKey:@"minZoom"];
    NSNumber *maxZoom = [value valueForKey:@"maxZoom"];

    SBSDKBaseScannerZoomConfiguration *zoomConfig = self.scannerViewController.zoomConfiguration;

    [RNScanbotBarcodeScannerViewControllerWrapper setCameraZoomRange:zoomConfig
                                                             minZoom: minZoom != nil ? [minZoom floatValue] : 1
                                                             maxZoom: maxZoom != nil ? [maxZoom floatValue] : 12];

    self.scannerViewController.zoomConfiguration = zoomConfig;
}

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

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

- (void) setDetectorConfig:(NSDictionary * _Nullable)configDict {
    SBBWrapperBarcodeCameraViewConfiguration* decodedConfig = [SBBWrapperBarcodeCameraViewConfiguration fromDicitonary: configDict];

    SBSDKBarcodeAdditionalParameters *additionalConfig = self.scannerViewController.additionalDetectionParameters;

    [RNScanbotBarcodeScannerViewControllerWrapper setBarcodeFormats:self.scannerViewController acceptedBarcodeTypes:decodedConfig.acceptedBarcodeTypes];

    [RNScanbotBarcodeScannerViewControllerWrapper setDocumentFormats:self.scannerViewController acceptedDocumentTypes:decodedConfig.acceptedDocumentTypes];

    [RNScanbotBarcodeScannerViewControllerWrapper setEngineMode:self.scannerViewController engineMode:decodedConfig.engineMode];

    [RNScanbotBarcodeScannerViewControllerWrapper setBarcodeExtensionFilter:self.scannerViewController
                                                                         filter:decodedConfig.barcodesExtensionFilter];

    [RNScanbotBarcodeScannerViewControllerWrapper setLowPowerMode:self.scannerViewController value:decodedConfig.lowPowerMode];

    [RNScanbotBarcodeScannerViewControllerWrapper setMinimumTextLenght:additionalConfig value:decodedConfig.minimumTextLength];

    [RNScanbotBarcodeScannerViewControllerWrapper setMaximumTextLenght:additionalConfig value:decodedConfig.maximumTextLength];

    [RNScanbotBarcodeScannerViewControllerWrapper setMinimum1DBarcodesQuietZone:additionalConfig value:decodedConfig.minimum1DBarcodesQuietZone];

    [RNScanbotBarcodeScannerViewControllerWrapper setStripCheckDigits:additionalConfig value:decodedConfig.stripCheckDigits];

    [RNScanbotBarcodeScannerViewControllerWrapper setGS1HandlingMode:additionalConfig gs1HandlingMode:decodedConfig.gs1Handling];

    [RNScanbotBarcodeScannerViewControllerWrapper setMsiPlesseyChecksumAlgorithm:additionalConfig msiPlesseyChecksumAlgorithm:decodedConfig.msiPlesseyChecksumAlgorithm];

    self.scannerViewController.additionalDetectionParameters = additionalConfig;
}

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

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

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


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

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

- (void) setOverlayTextFormat:(NSString * _Nullable) value {
    SBBWrapperBarcodeCameraViewConfiguration* decodedConfig = [SBBWrapperBarcodeCameraViewConfiguration fromDicitonary: @{
        @"textFormat": value
    }];

    [RNScanbotBarcodeScannerViewControllerWrapper setOverlayTextFormat:self.scannerViewController value: decodedConfig.textFormat];
}

- (void) setOverlayLoadingTextValue:(NSString * _Nullable) value {
    self.barcodeItemOverlayViewLoadingText = value;
}

- (void) setOverlayBarcodeItemOverlayViewBinder:(BOOL) value {
    
    if(value){
        self.barcodeItemOverlayViewBinders = [[NSMutableDictionary alloc] init];
    }
}

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

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

- (void)bindBarcodeItemOverlayView:(nonnull NSString *)barcodeItemUuid bindingConfig:(nonnull NSString *)bindingConfigJson {
    [RNScanbotBarcodeScannerViewControllerWrapper updateBarcodeItemOverlayViewBinderInDictionary:self.barcodeItemOverlayViewBinders
                                                                                 barcodeItemUuid:barcodeItemUuid
                                                                    barcodeItemOverlayViewConfigJson:bindingConfigJson];
}

- (void)barcodeScannerController:(nonnull SBSDKBarcodeScannerViewController *)controller
               didDetectBarcodes:(nonnull NSArray<SBSDKBarcodeScannerResult *> *)codes {

    if(!self.onBarcodeScannerResult) {
        return;
    }

    NSString *result = [RNScanbotBarcodeScannerViewControllerWrapper jsonFromResultOrNil:codes];
    if(result != nil){
        self.onBarcodeScannerResult(@{@"result":result});
        
        [RNScanbotBarcodeScannerViewControllerWrapper addBarcodeItemOverlayViewInitialBinderInDictionary:self.barcodeItemOverlayViewBinders
                                                                                             loadingText:self.barcodeItemOverlayViewLoadingText
                                                                                                barcodes:codes];
    }
}

- (BOOL)barcodeScannerControllerShouldDetectBarcodes:(SBSDKBarcodeScannerViewController * _Nonnull)controller{
    return _scanningEnabled;
}

- (void)barcodeTrackingOverlay:(SBSDKBarcodeTrackingOverlayController * _Nonnull)controller didTapOnBarcode:(SBSDKBarcodeScannerResult * _Nonnull)barcode {

    if(!self.onSelectBarcodeResult){
        return;
    }

    NSString *result = [RNScanbotBarcodeScannerViewControllerWrapper jsonFromResultOrNil:@[barcode]];
    if(result != nil){
        self.onSelectBarcodeResult(@{@"result":result});
    }

}

- (SBSDKBarcodeTrackedViewPolygonStyle *)barcodeTrackingOverlay:(SBSDKBarcodeTrackingOverlayController * _Nonnull)controller polygonStyleFor:(SBSDKBarcodeScannerResult * _Nonnull)barcode {

    NSString * _Nonnull barcodeItemUuid = [BarcodeItem uuidFor:barcode];
    NSDictionary * _Nullable barcodeItemOverlayViewBinding = nil;
    
    if (self.barcodeItemOverlayViewBinders != nil) {
        barcodeItemOverlayViewBinding = [self.barcodeItemOverlayViewBinders objectForKey:barcodeItemUuid];
    }
    
    return [RNScanbotBarcodeScannerViewControllerWrapper scannedBarcodePolygonStyle:barcode
                                                 barcodeItemOverlayViewBinding:barcodeItemOverlayViewBinding
                                                                        controller:self.scannerViewController];

}

- (SBSDKBarcodeTrackedViewTextStyle *)barcodeTrackingOverlay:(SBSDKBarcodeTrackingOverlayController * _Nonnull)controller textStyleFor:(SBSDKBarcodeScannerResult * _Nonnull)barcode {

    NSString * _Nonnull barcodeItemUuid = [BarcodeItem uuidFor:barcode];
    NSDictionary * _Nullable barcodeItemOverlayViewBinding = nil;
  
    if (self.barcodeItemOverlayViewBinders != nil) {
      barcodeItemOverlayViewBinding = [self.barcodeItemOverlayViewBinders objectForKey:barcodeItemUuid];
    }
    
    return [RNScanbotBarcodeScannerViewControllerWrapper scannedBarcodeOverlayViewTextStyle:barcode
                                               barcodeItemOverlayViewBinding:barcodeItemOverlayViewBinding
                                                                          controller:self.scannerViewController];
}

- (NSString * _Nonnull)barcodeTrackingOverlay:(SBSDKBarcodeTrackingOverlayController * _Nonnull)controller overrideTextFor:(SBSDKBarcodeScannerResult * _Nonnull)barcode proposedString:(NSString * _Nonnull)proposedString {
    
    NSString * _Nonnull barcodeItemUuid = [BarcodeItem uuidFor:barcode];
    NSDictionary * _Nullable barcodeItemOverlayViewBinding = nil;
  
    if (self.barcodeItemOverlayViewBinders != nil) {
      barcodeItemOverlayViewBinding = [self.barcodeItemOverlayViewBinders objectForKey:barcodeItemUuid];
    }
    
    return [RNScanbotBarcodeScannerViewControllerWrapper scannedBarcodeOverlayViewText:barcodeItemUuid
                                                                        proposedString:proposedString
                                                     barcodeItemOverlayViewBinding:barcodeItemOverlayViewBinding];
}

@end


#endif
