//
//  RegulaConfig.m
//  RNRegulaDocumentReader
//
//  Created by Pavel Masuk on 5/2/19.
//  Copyright © 2019 Facebook. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "RegulaConfig.h"

@implementation RegulaConfig
+(void)setConfig:(NSDictionary*) options : (DocReader*) reader{
    if([options valueForKey:@"customization"] != nil){
        [self setCustomization: [options valueForKey:@"customization"]: reader.customization];
    }
    if([options valueForKey:@"functionality"] != nil){
        [self setFunctionality: [options valueForKey:@"functionality"]: reader.functionality];
    }
    if([options valueForKey:@"processParams"] != nil){
        [self setProcessParams: [options valueForKey:@"processParams"]: reader.processParams];
    }
}

+(UIColor *)getUIColorObjectFromHexString:(NSString *)hexStr alpha:(CGFloat)alpha
{
    unsigned int hexint = [self intFromHexString:hexStr];
    UIColor *color =
    [UIColor colorWithRed:((CGFloat) ((hexint & 0xFF0000) >> 16))/255
                    green:((CGFloat) ((hexint & 0xFF00) >> 8))/255
                     blue:((CGFloat) (hexint & 0xFF))/255
                    alpha:alpha];
    
    return color;
}

+ (unsigned int)intFromHexString:(NSString *)hexStr
{
    unsigned int hexInt = 0;
    NSScanner *scanner = [NSScanner scannerWithString:hexStr];
    [scanner setCharactersToBeSkipped:[NSCharacterSet characterSetWithCharactersInString:@"#"]];
    [scanner scanHexInt:&hexInt];
    
    return hexInt;
}

+(UIImage*)imageFromBase64:(NSString *)base64image
{
    NSMutableString *base64 = [NSMutableString stringWithString: base64image];
    if(![[base64image substringToIndex:10] isEqualToString:@"data:image"]){
        base64 = [NSMutableString stringWithFormat: @"%@%@", @"data:image/jpeg;base64,", base64image];
    }
    NSURL *url = [NSURL URLWithString:base64];
    NSData *imageData = [NSData dataWithContentsOfURL:url];
    UIImage *image = [UIImage imageWithData:imageData];
    return image;
}

+(void)setCustomization:(NSDictionary*) options : (Customization*) customization{
    if([options valueForKey:@"cameraFrameBorderWidth"] != nil){
        customization.cameraFrameBorderWidth = [[options valueForKey:@"customization"] floatValue];
    }
    if([options valueForKey:@"cameraFrameDefaultColor"] != nil){
        customization.cameraFrameDefaultColor = [self getUIColorObjectFromHexString:[options valueForKey:@"cameraFrameDefaultColor"] alpha:1];
    }
    if([options valueForKey:@"cameraFrameActiveColor"] != nil){
        customization.cameraFrameActiveColor = [self getUIColorObjectFromHexString:[options valueForKey:@"cameraFrameActiveColor"] alpha:1];
    }
    if([options valueForKey:@"cameraFrameShapeType"] != nil){
        customization.cameraFrameShapeType = [[options valueForKey:@"cameraFrameShapeType"]intValue];
    }
    if([options valueForKey:@"cameraFrameLineLength"] != nil){
        customization.cameraFrameLineLength = [[options valueForKey:@"cameraFrameLineLength"] intValue];
    }
    if([options valueForKey:@"status"] != nil){
        customization.status = [options valueForKey:@"status"];
    }
    if([options valueForKey:@"showStatusMessages"] != nil){
        customization.showStatusMessages = [[options valueForKey:@"showStatusMessages"] boolValue];
    }
    if([options valueForKey:@"statusTextFont"] != nil){
        customization.statusTextFont = [UIFont fontWithName:[options valueForKey:@"statusTextFont"] size:[[options valueForKey:@"statusTextSize"] floatValue]];
    }
    if([options valueForKey:@"statusTextColor"] != nil){
        customization.statusTextColor = [self getUIColorObjectFromHexString:[options valueForKey:@"statusTextColor"] alpha:1];
    }
    if([options valueForKey:@"statusPositionMultiplier"] != nil){
        customization.statusPositionMultiplier = [[options valueForKey:@"statusPositionMultiplier"] doubleValue];
    }
    if([options valueForKey:@"showResultStatusMessages"] != nil){
        customization.showResultStatusMessages = [[options valueForKey:@"showResultStatusMessages"] boolValue];
    }
    if([options valueForKey:@"resultStatusTextFont"] != nil){
        customization.resultStatusTextFont = [UIFont fontWithName:[options valueForKey:@"resultStatusTextFont"] size:[[options valueForKey:@"resultStatusTextSize"] floatValue]];
    }
    if([options valueForKey:@"resultStatusTextColor"] != nil){
        customization.resultStatusTextColor = [self getUIColorObjectFromHexString:[options valueForKey:@"resultStatusTextColor"] alpha:1];
    }
    if([options valueForKey:@"resultStatusBackgroundColor"] != nil){
        customization.resultStatusBackgroundColor = [self getUIColorObjectFromHexString:[options valueForKey:@"resultStatusBackgroundColor"] alpha:1];
    }
    if([options valueForKey:@"resultStatusPositionMultiplier"] != nil){
        customization.resultStatusPositionMultiplier = [[options valueForKey:@"resultStatusPositionMultiplier"] doubleValue];
    }
    if([options valueForKey:@"showHelpAnimation"] != nil){
        customization.showHelpAnimation = [[options valueForKey:@"showHelpAnimation"] boolValue];
    }
    if([options valueForKey:@"showNextPageAnimation"] != nil){
        customization.showNextPageAnimation = [[options valueForKey:@"showNextPageAnimation"] boolValue];
    }
    if([options valueForKey:@"helpAnimationImage"] != nil){
        customization.helpAnimationImage = [self imageFromBase64:[options valueForKey:@"helpAnimationImage"]];
    }
    if([options valueForKey:@"multipageAnimationFrontImage"] != nil){
        customization.multipageAnimationFrontImage = [self imageFromBase64:[options valueForKey:@"multipageAnimationFrontImage"]];
    }
    if([options valueForKey:@"multipageAnimationBackImage"] != nil){
        customization.multipageAnimationBackImage = [self imageFromBase64:[options valueForKey:@"multipageAnimationBackImage"]];
    }
    if([options valueForKey:@"tintColor"] != nil){
        customization.tintColor = [self getUIColorObjectFromHexString:[options valueForKey:@"tintColor"] alpha:1];
    }
    if([options valueForKey:@"multipageButtonBackgroundColor"] != nil){
        customization.multipageButtonBackgroundColor = [self getUIColorObjectFromHexString:[options valueForKey:@"multipageButtonBackgroundColor"] alpha:1];
    }
    if([options valueForKey:@"activityIndicatorColor"] != nil){
        customization.activityIndicatorColor = [self getUIColorObjectFromHexString:[options valueForKey:@"activityIndicatorColor"] alpha:1];
    }
    if([options valueForKey:@"showBackgroundMask"] != nil){
        customization.showBackgroundMask = [[options valueForKey:@"showBackgroundMask"] boolValue];
    }
    if([options valueForKey:@"borderBackgroundImage"] != nil){
        customization.borderBackgroundImage = [self imageFromBase64:[options valueForKey:@"borderBackgroundImage"]];
    }
}

+(void)setFunctionality:(NSDictionary*) options : (Functionality*) functionality{
    if([options valueForKey:@"cameraFrame"] != nil){
        functionality.cameraFrame = [[options valueForKey:@"cameraFrame"] intValue];
    }
    if([options valueForKey:@"showTorchButton"] != nil){
        functionality.showTorchButton = [[options valueForKey:@"showTorchButton"] boolValue];
    }
    if([options valueForKey:@"showCloseButton"] != nil){
        functionality.showCloseButton = [[options valueForKey:@"showCloseButton"] boolValue];
    }
    if([options valueForKey:@"showCaptureButton"] != nil){
        functionality.showCaptureButton = [[options valueForKey:@"showCaptureButton"] boolValue];
    }
    if([options valueForKey:@"showChangeFrameButton"] != nil){
        functionality.showChangeFrameButton = [[options valueForKey:@"showChangeFrameButton"] boolValue];
    }
    if([options valueForKey:@"showSkipNextPageButton"] != nil){
        functionality.showSkipNextPageButton = [[options valueForKey:@"showSkipNextPageButton"] boolValue];
    }
    if([options valueForKey:@"showCameraSwitchBtn"] != nil){
        functionality.showCameraSwitchBtn = [[options valueForKey:@"showCameraSwitchBtn"] boolValue];
    }
    if([options valueForKey:@"videoCaptureMotionControl"] != nil){
        functionality.videoCaptureMotionControl = [[options valueForKey:@"videoCaptureMotionControl"] boolValue];
    }
    if([options valueForKey:@"skipFocusingFrames"] != nil){
        functionality.skipFocusingFrames = [[options valueForKey:@"skipFocusingFrames"] boolValue];
    }
    if([options valueForKey:@"orientation"] != nil){
        functionality.orientation = [[options valueForKey:@"orientation"] intValue];
    }
    if([options valueForKey:@"videoSessionPreset"] != nil){
        NSInteger preset = [[options valueForKey:@"videoSessionPreset"]intValue];
        switch(preset){
            case 1:
                functionality.videoSessionPreset = AVCaptureSessionPresetLow;
                break;
            case 2:
                functionality.videoSessionPreset = AVCaptureSessionPresetHigh;
                break;
            case 3:
                functionality.videoSessionPreset = AVCaptureSessionPresetPhoto;
                break;
            case 4:
                functionality.videoSessionPreset = AVCaptureSessionPresetMedium;
                break;
            case 5:
                functionality.videoSessionPreset = AVCaptureSessionPreset352x288;
                break;
            case 6:
                functionality.videoSessionPreset = AVCaptureSessionPreset640x480;
                break;
            case 7:
                functionality.videoSessionPreset = AVCaptureSessionPreset1280x720;
                break;
            case 8:
                functionality.videoSessionPreset = AVCaptureSessionPreset1920x1080;
                break;
            case 9:
                functionality.videoSessionPreset = AVCaptureSessionPreset3840x2160;
                break;
            case 10:
                functionality.videoSessionPreset = AVCaptureSessionPresetiFrame960x540;
                break;
            case 11:
                functionality.videoSessionPreset = AVCaptureSessionPresetInputPriority;
                break;
            default:
                functionality.videoSessionPreset = AVCaptureSessionPreset1920x1080;
                break;
        }
    }
    if([options valueForKey:@"singleResult"] != nil){
        functionality.singleResult = [[options valueForKey:@"singleResult"] boolValue];
    }
    if([options valueForKey:@"cameraPosition"] != nil){
        functionality.cameraPosition = [[options valueForKey:@"cameraPosition"] intValue];
    }
}

+(void)setProcessParams:(NSDictionary*) options : (ProcessParams*) processParams{
    if([options valueForKey:@"multipageProcessing"] != nil){
        processParams.multipageProcessing = [[options valueForKey:@"multipageProcessing"] boolValue];
    }
    if([options valueForKey:@"dateFormat"] != nil){
        processParams.dateFormat = [options valueForKey:@"dateFormat"];
    }
    if([options valueForKey:@"logs"] != nil){
        processParams.logs = [[options valueForKey:@"logs"] boolValue];
    }
    if([options valueForKey:@"debugSaveImages"] != nil){
        processParams.debugSaveImages = [[options valueForKey:@"debugSaveImages"] boolValue];
    }
    if([options valueForKey:@"debugSaveCroppedImages"] != nil){
        processParams.debugSaveCroppedImages = [[options valueForKey:@"debugSaveCroppedImages"] boolValue];
    }
    if([options valueForKey:@"debugSaveLogs"] != nil){
        processParams.debugSaveLogs = [[options valueForKey:@"debugSaveLogs"] boolValue];
    }
    if([options valueForKey:@"scenario"] != nil){
        processParams.scenario = [options valueForKey:@"scenario"];
    }
    if([options valueForKey:@"barcodeTypes"] != nil){
        processParams.barcodeTypes = [[options valueForKey:@"barcodeTypes"] array];
    }
    if([options valueForKey:@"documentIDList"] != nil){
        processParams.documentIDList = [[options valueForKey:@"documentIDList"] array];
    }
    if([options valueForKey:@"fieldTypesFilter"] != nil){
        processParams.fieldTypesFilter = [[options valueForKey:@"fieldTypesFilter"] array];
    }
    if([options valueForKey:@"disableFocusingCheck"] != nil){
        processParams.disableFocusingCheck = [[options valueForKey:@"disableFocusingCheck"] boolValue];
    }
}

@end
