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

#import "RNDynamsoftCameraView.h"
#import <DynamsoftCaptureVisionBundle/DynamsoftCaptureVisionBundle.h>

@interface RNDynamsoftCameraView ()

@end

@implementation RNDynamsoftCameraView

- (instancetype)init
{
    self = [super init];
    if (self) {
        _cameraView = [[DSCameraView alloc] init];
        [self addSubview:_cameraView];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    self.cameraView.frame = self.bounds;
}

- (void)setVisibleLayerIds:(NSArray<NSNumber *> *)visibleLayerIds {
    _visibleLayerIds = visibleLayerIds;
    NSArray<NSNumber *> *layerArray = @[@(DSDrawingLayerIdDBR), @(DSDrawingLayerIdDDN), @(DSDrawingLayerIdDLR)];
    for (NSNumber *layerId in layerArray) {
        DSDrawingLayer *layer = [self.cameraView getDrawingLayer:layerId.unsignedIntegerValue];
        if ([visibleLayerIds containsObject:layerId]) {
            layer.visible = true;
        } else {
            layer.visible = false;
        }
    }
}

- (void)setScanRegionMaskVisible:(BOOL)scanRegionMaskVisible {
    _scanRegionMaskVisible = scanRegionMaskVisible;
    self.cameraView.scanRegionMaskVisible = scanRegionMaskVisible;
}

- (void)setScanLaserVisible:(BOOL)scanLaserVisible {
    _scanLaserVisible = scanLaserVisible;
    self.cameraView.scanLaserVisible = scanLaserVisible;
}

- (void)setTorchButton:(NSDictionary *)torchButton {
    if (torchButton != nil) {
        _torchButton = torchButton;
        NSDictionary *location = [torchButton valueForKey:@"location"];
        NSNumber *visible = [torchButton valueForKey:@"visible"];
        NSString *torchOnImageBase64 = [torchButton valueForKey:@"torchOnImageBase64"];
        NSString *torchOffImageBase64 = [torchButton valueForKey:@"torchOffImageBase64"];
        if (location != nil) {
            NSNumber *x = [location valueForKey:@"x"];
            NSNumber *y = [location valueForKey:@"y"];
            NSNumber *width = [location valueForKey:@"width"];
            NSNumber *height = [location valueForKey:@"height"];
            CGRect rect = CGRectMake(x.floatValue, y.floatValue, width.floatValue, height.floatValue);
            UIImage *torchOnImage, *torchOffImage;
            if ((torchOnImageBase64 != nil) && (torchOffImageBase64 != nil)) {
                torchOnImage = [UIImage imageWithData: [[NSData alloc]initWithBase64EncodedString:torchOnImageBase64 options:(NSDataBase64DecodingIgnoreUnknownCharacters)]];
                torchOffImage = [UIImage imageWithData: [[NSData alloc]initWithBase64EncodedString:torchOffImageBase64 options:(NSDataBase64DecodingIgnoreUnknownCharacters)]];
            }
            [self.cameraView setTorchButtonWithFrame:rect torchOnImage:torchOnImage torchOffImage:torchOffImage];
        }
        if (visible != nil) {
            self.cameraView.torchButtonVisible = visible.boolValue;
        }
    }
}

- (void)setTorchButtonVisible:(BOOL)torchButtonVisible {
    _torchButtonVisible = torchButtonVisible;
    self.cameraView.torchButtonVisible = torchButtonVisible;
}

@end
