/*
 * Copyright © Dynamsoft Corporation. All rights reserved.
 */

#import "RNDynamsoftMultiCrossFilter.h"
#import <DynamsoftCaptureVisionBundle/DynamsoftCaptureVisionBundle.h>
#import "RNDynamsoftCaptureVisionRouter.h"

@implementation RNDynamsoftMultiCrossFilter

RCT_EXPORT_MODULE(DynamsoftMultiCrossFilterModule)

RCT_EXPORT_BLOCKING_SYNCHRONOUS_METHOD(createInstance) {
    DSMultiFrameResultCrossFilter *filter = [[DSMultiFrameResultCrossFilter alloc] init];
    NSString *key = [NSNumber numberWithUnsignedInteger:filter.hash].stringValue;
    RNDynamsoftCaptureVisionRouter *router = [self.bridge moduleForClass:[RNDynamsoftCaptureVisionRouter class]];
    [router.filterDictionary setValue:filter forKey:key];
    return key;
}

RCT_EXPORT_METHOD(destroy:(NSString *)key) {
    RNDynamsoftCaptureVisionRouter *router = [self.bridge moduleForClass:[RNDynamsoftCaptureVisionRouter class]];
    [router.filterDictionary removeObjectForKey:key];
}

RCT_EXPORT_METHOD(enableResultCrossVerification:(NSString *)key type:(NSInteger)type isEnabled:(BOOL)isEnabled) {
    RNDynamsoftCaptureVisionRouter *router = [self.bridge moduleForClass:[RNDynamsoftCaptureVisionRouter class]];
    DSMultiFrameResultCrossFilter *filter = [router.filterDictionary objectForKey:key];
    [filter enableResultCrossVerification:type isEnabled:isEnabled];
}

RCT_EXPORT_METHOD(isResultCrossVerificationEnabled:(NSString *)key
                  type:(NSInteger)type
                  resolver:(RCTPromiseResolveBlock)resolve
                  rejecter:(RCTPromiseRejectBlock)reject)
{
    RNDynamsoftCaptureVisionRouter *router = [self.bridge moduleForClass:[RNDynamsoftCaptureVisionRouter class]];
    DSMultiFrameResultCrossFilter *filter = [router.filterDictionary objectForKey:key];
    resolve(@([filter isResultCrossVerificationEnabled:type]));
}

RCT_EXPORT_METHOD(enableResultDeduplication:(NSString *)key type:(NSInteger)type isEnabled:(BOOL)isEnabled) {
    RNDynamsoftCaptureVisionRouter *router = [self.bridge moduleForClass:[RNDynamsoftCaptureVisionRouter class]];
    DSMultiFrameResultCrossFilter *filter = [router.filterDictionary objectForKey:key];
    [filter enableResultDeduplication:type isEnabled:isEnabled];
}

RCT_EXPORT_METHOD(isResultDeduplicationEnabled:(NSString *)key
                  type:(NSInteger)type
                  resolver:(RCTPromiseResolveBlock)resolve
                  rejecter:(RCTPromiseRejectBlock)reject)
{
    RNDynamsoftCaptureVisionRouter *router = [self.bridge moduleForClass:[RNDynamsoftCaptureVisionRouter class]];
    DSMultiFrameResultCrossFilter *filter = [router.filterDictionary objectForKey:key];
    resolve(@([filter isResultDeduplicationEnabled:type]));
}

RCT_EXPORT_METHOD(setDuplicateForgetTime:(NSString *)key type:(NSInteger)type time:(NSInteger)time) {
    RNDynamsoftCaptureVisionRouter *router = [self.bridge moduleForClass:[RNDynamsoftCaptureVisionRouter class]];
    DSMultiFrameResultCrossFilter *filter = [router.filterDictionary objectForKey:key];
    [filter setDuplicateForgetTime:type duplicateForgetTime:time];
}

RCT_EXPORT_METHOD(getDuplicateForgetTime:(NSString *)key
                  type:(NSInteger)type
                  resolver:(RCTPromiseResolveBlock)resolve
                  rejecter:(RCTPromiseRejectBlock)reject)
{
    RNDynamsoftCaptureVisionRouter *router = [self.bridge moduleForClass:[RNDynamsoftCaptureVisionRouter class]];
    DSMultiFrameResultCrossFilter *filter = [router.filterDictionary objectForKey:key];
    resolve(@([filter getDuplicateForgetTime:type]));
}

RCT_EXPORT_METHOD(enableLatestOverlapping:(NSString *)key type:(NSInteger)type isEnabled:(BOOL)isEnabled) {
    RNDynamsoftCaptureVisionRouter *router = [self.bridge moduleForClass:[RNDynamsoftCaptureVisionRouter class]];
    DSMultiFrameResultCrossFilter *filter = [router.filterDictionary objectForKey:key];
    [filter enableLatestOverlapping:type isEnabled:isEnabled];
}

RCT_EXPORT_METHOD(isLatestOverlappingEnabled:(NSString *)key
                  type:(NSInteger)type
                  resolver:(RCTPromiseResolveBlock)resolve
                  rejecter:(RCTPromiseRejectBlock)reject)
{
    RNDynamsoftCaptureVisionRouter *router = [self.bridge moduleForClass:[RNDynamsoftCaptureVisionRouter class]];
    DSMultiFrameResultCrossFilter *filter = [router.filterDictionary objectForKey:key];
    resolve(@([filter isLatestOverlappingEnabled:type]));
}

RCT_EXPORT_METHOD(setMaxOverlappingFrames:(NSString *)key type:(NSInteger)type maxOverlappingFrames:(NSInteger)maxOverlappingFrames) {
    RNDynamsoftCaptureVisionRouter *router = [self.bridge moduleForClass:[RNDynamsoftCaptureVisionRouter class]];
    DSMultiFrameResultCrossFilter *filter = [router.filterDictionary objectForKey:key];
    [filter setMaxOverlappingFrames:type maxOverlappingFrames:maxOverlappingFrames];
}

RCT_EXPORT_METHOD(getMaxOverlappingFrames:(NSString *)key
                  type:(NSInteger)type
                  resolver:(RCTPromiseResolveBlock)resolve
                  rejecter:(RCTPromiseRejectBlock)reject)
{
    RNDynamsoftCaptureVisionRouter *router = [self.bridge moduleForClass:[RNDynamsoftCaptureVisionRouter class]];
    DSMultiFrameResultCrossFilter *filter = [router.filterDictionary objectForKey:key];
    resolve(@([filter getMaxOverlappingFrames:type]));
}

@end
