#if !defined(RCT_NEW_ARCH_ENABLED)

#import <React/RCTViewManager.h>
#import <React/RCTUIManager.h>

#import "RNScanbotDocumentScannerLegacyView.h"

typedef void (^NativeViewBlock)(RNScanbotDocumentScannerLegacyView* _Nonnull);

@interface RNScanbotDocumentScannerViewManager : RCTViewManager

@end

@implementation RNScanbotDocumentScannerViewManager

RCT_EXPORT_MODULE(ScanbotDocumentScannerView)

-(UIView *)view {
    return [[RNScanbotDocumentScannerLegacyView alloc] init];
}

// Events
RCT_EXPORT_VIEW_PROPERTY(onDocumentScannerResult, RCTDirectEventBlock)
RCT_EXPORT_VIEW_PROPERTY(onDetectionResult, RCTDirectEventBlock)
RCT_EXPORT_VIEW_PROPERTY(onError, RCTDirectEventBlock)
// General Configuration
RCT_EXPORT_VIEW_PROPERTY(flashEnabled, BOOL)
RCT_EXPORT_VIEW_PROPERTY(hardwareButtonsEnabled, BOOL)
RCT_EXPORT_VIEW_PROPERTY(detectDocumentAfterSnap, BOOL)
// Document Configuration
RCT_EXPORT_VIEW_PROPERTY(acceptedAngleScore, NSNumber)
RCT_EXPORT_VIEW_PROPERTY(acceptedBrightnessThreshold, NSNumber)
RCT_EXPORT_VIEW_PROPERTY(acceptedSizeScore, NSNumber)
RCT_EXPORT_VIEW_PROPERTY(ignoreOrientationMismatch, BOOL)
RCT_EXPORT_VIEW_PROPERTY(acceptedAspectRatioScore, NSNumber)
RCT_EXPORT_VIEW_PROPERTY(requiredAspectRatios, NSArray)
RCT_EXPORT_VIEW_PROPERTY(partiallyVisibleDocumentConfiguration, NSDictionary)
RCT_EXPORT_VIEW_PROPERTY(autoSnappingEnabled, BOOL)
RCT_EXPORT_VIEW_PROPERTY(autoSnappingSensitivity, NSNumber)
RCT_EXPORT_VIEW_PROPERTY(autoSnappingDelay, NSNumber)
// Camera Configuration
RCT_EXPORT_VIEW_PROPERTY(cameraModule, NSString)
RCT_EXPORT_VIEW_PROPERTY(cameraPreviewMode, NSString)
RCT_EXPORT_VIEW_PROPERTY(photoQualityPrioritization, NSString)
// Finder Configuration
RCT_EXPORT_VIEW_PROPERTY(finderEnabled, BOOL)
RCT_EXPORT_VIEW_PROPERTY(finderLineColor, UIColor)
RCT_EXPORT_VIEW_PROPERTY(finderLineWidth, NSNumber)
RCT_EXPORT_VIEW_PROPERTY(finderOverlayColor, UIColor)
RCT_EXPORT_VIEW_PROPERTY(finderCornerRadius, NSNumber)
RCT_EXPORT_VIEW_PROPERTY(finderMinimumPadding, NSNumber)
RCT_EXPORT_VIEW_PROPERTY(finderAspectRatio, NSDictionary)
// Polygon Configuration
RCT_EXPORT_VIEW_PROPERTY(polygonEnabled, BOOL)
RCT_EXPORT_VIEW_PROPERTY(polygonBackgroundColor, UIColor)
RCT_EXPORT_VIEW_PROPERTY(polygonBackgroundColorOK, UIColor)
RCT_EXPORT_VIEW_PROPERTY(polygonColor, UIColor)
RCT_EXPORT_VIEW_PROPERTY(polygonColorOK, UIColor)
RCT_EXPORT_VIEW_PROPERTY(polygonLineWidth, NSNumber)
RCT_EXPORT_VIEW_PROPERTY(polygonCornerRadius, NSNumber)
RCT_EXPORT_VIEW_PROPERTY(polygonAutoSnapProgressColor, UIColor)
RCT_EXPORT_VIEW_PROPERTY(polygonAutoSnapProgressLineWidth, NSNumber)
RCT_EXPORT_VIEW_PROPERTY(polygonAutoSnapProgressEnabled, BOOL)


//Commands
RCT_EXPORT_METHOD(freezeCamera:(nonnull NSNumber*) reactTag) {
    [self callNativeMethodWithReactTag: reactTag andBlock: ^void (RNScanbotDocumentScannerLegacyView *nativeView) {
        [nativeView freezeCamera];
    }];
}

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

RCT_EXPORT_METHOD(snapDocument:(nonnull NSNumber*) reactTag aquireFocus:(BOOL)aquireFocus) {
    [self callNativeMethodWithReactTag: reactTag andBlock: ^void (RNScanbotDocumentScannerLegacyView *nativeView) {
        [nativeView snapDocument];
    }];
}

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

@end

#endif
