#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)
#if __has_include(<RNScanbotSDK/RNScanbotSDK-Swift.h>)
#import <RNScanbotSDK/RNScanbotSDK-Swift.h>
#else
#import "RNScanbotSDK-Swift.h"
#endif
#import <AVFoundation/AVFoundation.h>
#import <ScanbotSDK/ScanbotSDK-Swift.h>
#import <ScanbotSDKNativeWrapper/ScanbotSDKNativeWrapper-Swift.h>
typedef void (^NativeViewBlock)(RNScanbotDocumentScannerLegacyView* _Nonnull);
#else
#import "RNScanbotDocumentScannerView.h"
#endif

@interface RNScanbotDocumentScannerViewManager : RCTViewManager

@end

@implementation RNScanbotDocumentScannerViewManager

RCT_EXPORT_MODULE(ScanbotDocumentScannerView)

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

RCT_EXPORT_VIEW_PROPERTY(onDocumentScannerResult, RCTDirectEventBlock)
RCT_EXPORT_VIEW_PROPERTY(onDetectionResult, RCTDirectEventBlock)

RCT_EXPORT_VIEW_PROPERTY(acceptedAngleScore, NSNumber)
RCT_EXPORT_VIEW_PROPERTY(acceptedBrightnessThreshold, NSNumber)
RCT_EXPORT_VIEW_PROPERTY(acceptedSizeScore, NSNumber)
RCT_EXPORT_VIEW_PROPERTY(autoSnappingEnabled, BOOL)
RCT_EXPORT_VIEW_PROPERTY(autoSnappingSensitivity, NSNumber)
RCT_EXPORT_VIEW_PROPERTY(autoSnappingDelay, NSNumber)
RCT_EXPORT_VIEW_PROPERTY(cameraModule, NSString)
RCT_EXPORT_VIEW_PROPERTY(cameraPreviewMode, NSString)
RCT_EXPORT_VIEW_PROPERTY(flashEnabled, BOOL)
RCT_EXPORT_VIEW_PROPERTY(ignoreBadAspectRatio, BOOL)
RCT_EXPORT_VIEW_PROPERTY(photoQualityPrioritization, NSString)
//RCT_EXPORT_VIEW_PROPERTY(touchToFocusEnabled, BOOL)
RCT_EXPORT_VIEW_PROPERTY(requiredAspectRatios, NSArray)
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)
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
#if !defined(RCT_NEW_ARCH_ENABLED)
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) {
  [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);
  }];
}
#endif

@end
