#if !defined(RCT_NEW_ARCH_ENABLED)

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

#import "RNScanbotCroppingLegacyView.h"

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

@interface RNScanbotCroppingViewManager : RCTViewManager

@end

@implementation RNScanbotCroppingViewManager

RCT_EXPORT_MODULE(ScanbotCroppingView)

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

// Events
RCT_EXPORT_VIEW_PROPERTY(onCroppedAreaResult, RCTDirectEventBlock)
RCT_EXPORT_VIEW_PROPERTY(onError, RCTDirectEventBlock)
// Sources
RCT_EXPORT_VIEW_PROPERTY(imageFileSrc, NSString)
RCT_EXPORT_VIEW_PROPERTY(imageRefSrc, NSString)
// Polygon Configuration
RCT_EXPORT_VIEW_PROPERTY(edgeColor, UIColor)
RCT_EXPORT_VIEW_PROPERTY(edgeLineWidth, NSNumber)
RCT_EXPORT_VIEW_PROPERTY(edgeColorOnLine, UIColor)
RCT_EXPORT_VIEW_PROPERTY(anchorPointsColor, UIColor)


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

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

RCT_EXPORT_METHOD(rotate:(nonnull NSNumber*) reactTag
                  clockwise: (BOOL) clockwise) {
    [self callNativeMethodWithReactTag: reactTag andBlock: ^void (RNScanbotCroppingLegacyView *nativeView) {
        [nativeView rotate: clockwise];
    }];
}

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

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

@end

#endif
