
#import <Foundation/Foundation.h>
#import <React/RCTLog.h>
#import <React/RCTUIManager.h>
#import <React/RCTViewManager.h>
#import <UIKit/UIKit.h>


#if !defined(RCT_NEW_ARCH_ENABLED)
#import "RNScanbotBarcodeScannerLegacyView.h"
typedef void (^NativeViewBlock)(RNScanbotBarcodeScannerLegacyView* _Nonnull);
#else
#import "RNScanbotBarcodeScannerView.h"
#endif


@interface RNScanbotBarcodeScannerViewManager: RCTViewManager
@end

@implementation RNScanbotBarcodeScannerViewManager

RCT_EXPORT_MODULE(ScanbotBarcodeScannerView)

- (UIView *)view {
#if !defined(RCT_NEW_ARCH_ENABLED)
    return [[RNScanbotBarcodeScannerLegacyView alloc] init];
#else
    return [[RNScanbotBarcodeScannerView alloc] init];
#endif
}

RCT_EXPORT_VIEW_PROPERTY(onBarcodeScannerResult, RCTDirectEventBlock)
RCT_EXPORT_VIEW_PROPERTY(onSelectBarcodeResult, RCTDirectEventBlock)

RCT_EXPORT_VIEW_PROPERTY(flashEnabled, BOOL)
RCT_EXPORT_VIEW_PROPERTY(finderEnabled, BOOL)
RCT_EXPORT_VIEW_PROPERTY(finderStrokeWidth, int)
RCT_EXPORT_VIEW_PROPERTY(finderStrokeColor, UIColor)
RCT_EXPORT_VIEW_PROPERTY(finderOverlayColor, UIColor)
//RCT_EXPORT_VIEW_PROPERTY(finderMinPadding, int)
RCT_EXPORT_VIEW_PROPERTY(finderInset, NSDictionary)
RCT_EXPORT_VIEW_PROPERTY(finderRequiredAspectRatios, NSDictionary)
RCT_EXPORT_VIEW_PROPERTY(cameraZoomFactor, float)
RCT_EXPORT_VIEW_PROPERTY(cameraZoomRange, NSDictionary)
RCT_EXPORT_VIEW_PROPERTY(cameraModule, NSString)
RCT_EXPORT_VIEW_PROPERTY(minFocusDistanceLock, BOOL)
RCT_EXPORT_VIEW_PROPERTY(detectorConfig, NSDictionary)
RCT_EXPORT_VIEW_PROPERTY(scanningEnabled, BOOL)
RCT_EXPORT_VIEW_PROPERTY(overlayEnabled, BOOL)
RCT_EXPORT_VIEW_PROPERTY(overlayPolygonColor, UIColor)
RCT_EXPORT_VIEW_PROPERTY(overlayStrokeColor, UIColor)
RCT_EXPORT_VIEW_PROPERTY(overlayTextColor, UIColor)
RCT_EXPORT_VIEW_PROPERTY(overlayTextContainerColor, UIColor)
RCT_EXPORT_VIEW_PROPERTY(overlayTextFormat, NSString)
RCT_EXPORT_VIEW_PROPERTY(overlayLoadingTextValue, NSString)
RCT_EXPORT_VIEW_PROPERTY(overlayBarcodeItemOverlayViewBinder, BOOL)

//Commands
#if !defined(RCT_NEW_ARCH_ENABLED)
RCT_EXPORT_METHOD(freezeCamera:(nonnull NSNumber*) reactTag) {
    [self callNativeMethodWithReactTag: reactTag andBlock: ^void (RNScanbotBarcodeScannerLegacyView *nativeView) {
        [nativeView freezeCamera];
    }];
}

RCT_EXPORT_METHOD(unfreezeCamera:(nonnull NSNumber*) reactTag) {
    [self callNativeMethodWithReactTag: reactTag andBlock: ^void (RNScanbotBarcodeScannerLegacyView *nativeView) {
        [nativeView unfreezeCamera];
    }];
}

RCT_EXPORT_METHOD(bindBarcodeItemOverlayView:(nonnull NSNumber*) reactTag
                  barcode:(nonnull NSString *)barcodeItemUuid binding:(nonnull NSString *)bindingConfigJson) {
    [self callNativeMethodWithReactTag: reactTag andBlock: ^void (RNScanbotBarcodeScannerLegacyView *nativeView) {
        [nativeView bindBarcodeItemOverlayView:barcodeItemUuid bindingConfig:bindingConfigJson];
    }];
}

- (void) callNativeMethodWithReactTag:(nonnull NSNumber*) reactTag andBlock:(NativeViewBlock)block {
    [self.bridge.uiManager addUIBlock:^(RCTUIManager *uiManager, NSDictionary<NSNumber *,UIView *> *viewRegistry) {
        RNScanbotBarcodeScannerLegacyView *view = (RNScanbotBarcodeScannerLegacyView *) viewRegistry[reactTag];
        if (!view || ![view isKindOfClass:[RNScanbotBarcodeScannerLegacyView class]]) {
            RCTLogError(@"Cannot find native view with tag #%@", reactTag);
            return;
        }
        block(view);
    }];
}
#endif


@end
