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

@interface RNScanbotBarcodeScannerLegacyView()
@property (strong, nonatomic) SBSDKBarcodeScannerViewController *scannerViewController;
@property (nonatomic) BOOL scanningEnabled;
@property (nonatomic) BOOL attached;
@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.attached = false;

        self.scannerViewController = [RNScanbotBarcodeScannerViewControllerWrapper initController];
        self.scannerViewController.delegate = self;
        self.scannerViewController.trackingOverlayController.delegate = self;
    }

    return self;
}

- (void)didMoveToWindow {
    [super didMoveToWindow];

    if(self.window && !self.attached){
        [[self reactViewController] sbsdk_attachViewController:self.scannerViewController inView:self];
        self.attached = true;
    }
}

- (void)removeFromSuperview {

    self.scannerViewController.delegate = nil;
    self.scannerViewController.trackingOverlayController.delegate = nil;

    [self.scannerViewController sbsdk_detachViewController:self.scannerViewController];
    self.scannerViewController = 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) setConfigFormatConfigurations: (NSArray<NSDictionary<NSString *, id> *> * _Nullable)containerConfigurations {
    SBSDKBarcodeScannerConfiguration *configuration = self.scannerViewController.copyCurrentConfiguration;
    NSMutableArray<SBSDKBarcodeFormatConfigurationBase *> *configs = [[NSMutableArray alloc] init];
    for(NSDictionary<NSString *, id> * container in containerConfigurations){
        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:container
                                                           options:NSJSONWritingSortedKeys
                                                             error:nil];
        if(jsonData != nil){
            NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
            [configs addObject:[SBSDKBarcodeFormatConfigurationBase fromJsonWithJson:jsonString]];
        }
    }

    configuration.barcodeFormatConfigurations = configs;
    self.scannerViewController.configuration = configuration;
}

- (void) setConfigExtractedDocumentFormats: (NSArray<NSString *> * _Nullable)formats {
    SBSDKBarcodeScannerConfiguration *configuration = self.scannerViewController.copyCurrentConfiguration;
    [RNScanbotBarcodeScannerViewControllerWrapper setConfigExtractedDocumentFormats:configuration formats:formats];
    self.scannerViewController.configuration = configuration;
}

- (void) setConfigOnlyAcceptDocuments: (BOOL)value {
    SBSDKBarcodeScannerConfiguration *configuration = self.scannerViewController.copyCurrentConfiguration;
    [RNScanbotBarcodeScannerViewControllerWrapper setConfigOnlyAcceptDocuments:configuration value:value];
    self.scannerViewController.configuration = configuration;
}

- (void) setConfigReturnBarcodeImage: (BOOL)value {
    SBSDKBarcodeScannerConfiguration *configuration = self.scannerViewController.copyCurrentConfiguration;
    [RNScanbotBarcodeScannerViewControllerWrapper setConfigReturnBarcodeImage:configuration value:value];
    self.scannerViewController.configuration = configuration;
}

- (void) setConfigEngineMode: (NSString * _Nullable)value {
    SBSDKBarcodeScannerConfiguration *configuration = self.scannerViewController.copyCurrentConfiguration;
    [RNScanbotBarcodeScannerViewControllerWrapper setConfigEngineMode:configuration value:value];
    self.scannerViewController.configuration = configuration;
}

- (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 {
    [RNScanbotBarcodeScannerViewControllerWrapper setOverlayTextFormat:self.scannerViewController value:value];
}

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

- (void) setOverlayBarcodeItemOverlayViewBinder:(BOOL) value {

    if(value){
        self.barcodeItemOverlayViewBinders = [[NSMutableDictionary alloc] init];
    }
}

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

- (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:(SBSDKBarcodeScannerViewController *)controller
                 didScanBarcodes:(NSArray<SBSDKBarcodeItem *> * _Nonnull)codes {

    if(!self.onBarcodeScannerResult) {
        return;
    }
    if(codes.count == 0) {
        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 *)controller didTapOnBarcode:(SBSDKBarcodeItem * _Nonnull)barcode; {

    if(!self.onSelectBarcodeResult){
        return;
    }

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

}

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

    NSString * _Nonnull barcodeItemUuid = [barcode uuid];
    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 *)controller textStyleFor:(SBSDKBarcodeItem *)barcode {

    NSString * _Nonnull barcodeItemUuid = [barcode uuid];
    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:(SBSDKBarcodeItem * _Nonnull)barcode proposedString:(NSString * _Nonnull)proposedString {

    NSString * _Nonnull barcodeItemUuid = [barcode uuid];
    NSDictionary * _Nullable barcodeItemOverlayViewBinding = nil;

    if (self.barcodeItemOverlayViewBinders != nil) {
        barcodeItemOverlayViewBinding = [self.barcodeItemOverlayViewBinders objectForKey:barcodeItemUuid];
    }

    return [RNScanbotBarcodeScannerViewControllerWrapper scannedBarcodeOverlayViewText:barcodeItemUuid
                                                                        proposedString:proposedString
                                                         barcodeItemOverlayViewBinding:barcodeItemOverlayViewBinding];
}

@end


#endif
