package com.regula.documentreader;

import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.ReadableArray;
import com.regula.documentreader.api.DocumentReader;
import com.regula.documentreader.api.params.Customization;
import com.regula.documentreader.api.params.Functionality;
import com.regula.documentreader.api.params.ProcessParam;
import android.graphics.Typeface;
import com.regula.documentreader.RfidScenarioAdapter;

// You can read more about each these settings at 
// https://github.com/regulaforensics/DocumentReader-Android/wiki/Custom-settings

public class RegulaConfig {

  static String[] extractStringArray(ReadableArray readableArray) {
    String[] result = new String[readableArray.size()];
    for (int i = 0; i < readableArray.size(); i++) {
      result[i] = readableArray.getString(i);
    }
    return result;
  }

  static int[] extractIntArray(ReadableArray readableArray) {
    int[] result = new int[readableArray.size()];
    for (int i = 0; i < readableArray.size(); i++) {
      result[i] = readableArray.getInt(i);
    }
    return result;
  }

  public static void setConfig(DocumentReader reader, ReadableMap opts) {
    if (opts.hasKey("customization")) {
      setCustomization(reader.customization, opts.getMap("customization"));
    }
    if (opts.hasKey("functionality")) {
      setFunctionality(reader.functionality, opts.getMap("functionality"));
    }
    if (opts.hasKey("processParams")) {
      setProcessParams(reader.processParams, opts.getMap("processParams"));
    }
  }

  public static void setProcessParams(ProcessParam processParams, ReadableMap opts) {
    if (opts.hasKey("multipageProcessing")) {
      processParams.multipageProcessing = opts.getBoolean("multipageProcessing");
    }

    if (opts.hasKey("dateFormat")) {
      processParams.dateFormat = opts.getString("dateFormat");
    }

    if (opts.hasKey("logs")) {
      processParams.logs = opts.getBoolean("logs");
    }

    if (opts.hasKey("debugSaveImages")) {
      processParams.debugSaveImages = opts.getBoolean("debugSaveImages");
    }

    if (opts.hasKey("debugSaveLogs")) {
      processParams.debugSaveLogs = opts.getBoolean("debugSaveLogs");
    }

    if (opts.hasKey("debugSaveCroppedImages")) {
      processParams.debugSaveCroppedImages = opts.getBoolean("debugSaveCroppedImages");
    }

    if (opts.hasKey("scenario")) {
      processParams.scenario = opts.getString("scenario");
    }

    if (opts.hasKey("doBarcodes")) {
      processParams.doBarcodes = extractStringArray(opts.getArray("doBarcodes"));
    }

    if (opts.hasKey("uvTorchEnabled")) {
      processParams.uvTorchEnabled = opts.getBoolean("uvTorchEnabled");
    }

    if (opts.hasKey("rfidScenario")) {
      RfidScenarioAdapter.setRfidScenario(processParams.rfidScenario, opts.getMap("rfidScenario"));
    }

    if (opts.hasKey("documentIDList")) {
      processParams.documentIDList = extractIntArray(opts.getArray("documentIDList"));
    }

    if (opts.hasKey("fieldTypesFilter")) {
      processParams.fieldTypesFilter = extractIntArray(opts.getArray("fieldTypesFilter"));
    }

    if (opts.hasKey("disableFocusingCheck")) {
      processParams.disableFocusingCheck = opts.getBoolean("disableFocusingCheck");
    }
  }

  public static void setFunctionality(Functionality functionality, ReadableMap opts) {
    if (opts.hasKey("cameraFrame")) {
      functionality.cameraFrame = opts.getString("cameraFrame");
    }

    if (opts.hasKey("showTorchButton")) {
      functionality.showTorchButton = opts.getBoolean("showTorchButton");
    }

    if (opts.hasKey("showCloseButton")) {
      functionality.showCloseButton = opts.getBoolean("showCloseButton");
    }

    if (opts.hasKey("showCaptureButton")) {
      functionality.showCaptureButton = opts.getBoolean("showCaptureButton");
    }

    if (opts.hasKey("showChangeFrameButton")) {
      functionality.showChangeFrameButton = opts.getBoolean("showChangeFrameButton");
    }

    if (opts.hasKey("showSkipNextPageButton")) {
      functionality.showSkipNextPageButton = opts.getBoolean("showSkipNextPageButton");
    }

    if (opts.hasKey("showCameraSwitchBtn")) {
      functionality.showCameraSwitchBtn = opts.getBoolean("showCameraSwitchBtn");
    }

    if (opts.hasKey("videoCaptureMotionControl")) {
      functionality.videoCaptureMotionControl = opts.getBoolean("videoCaptureMotionControl");
    }

    if (opts.hasKey("skipFocusingFrames")) {
      functionality.skipFocusingFrames = opts.getBoolean("skipFocusingFrames");
    }

    if (opts.hasKey("orientation")) {
      functionality.orientation = opts.getInt("orientation");
    }

    if (opts.hasKey("pictureOnBoundsReady")) {
      functionality.pictureOnBoundsReady = opts.getBoolean("pictureOnBoundsReady");
    }
  }

  public static void setCustomization(Customization customization, ReadableMap opts) {
    if (opts.hasKey("cameraFrameBorderWidth")) {
      customization.cameraFrameBorderWidth = opts.getInt("cameraFrameBorderWidth");
    }

    if (opts.hasKey("cameraFrameDefaultColor")) {
      customization.cameraFrameDefaultColor = opts.getString("cameraFrameDefaultColor");
    }

    if (opts.hasKey("cameraFrameActiveColor")) {
      customization.cameraFrameActiveColor = opts.getString("cameraFrameActiveColor");
    }

    if (opts.hasKey("cameraFrameShapeType")) {
      customization.cameraFrameShapeType = opts.getInt("cameraFrameShapeType");
    }

    if (opts.hasKey("cameraFrameLineLength")) {
      customization.cameraFrameLineLength = opts.getInt("cameraFrameLineLength");
    }

    if (opts.hasKey("status")) {
      customization.status = opts.getString("status");
    }

    if (opts.hasKey("showStatusMessages")) {
      customization.showStatusMessages = opts.getBoolean("showStatusMessages");
    }

    if (opts.hasKey("statusTextFont")) {
      customization.statusTextFont = Typeface.create(opts.getString("statusTextFont"),
          opts.getInt("statusTextFontStyle"));// CHECK
    }

    if (opts.hasKey("statusTextSize")) {
      customization.statusTextSize = opts.getInt("statusTextSize");
    }

    if (opts.hasKey("statusTextColor")) {
      customization.statusTextColor = opts.getString("statusTextColor");
    }

    if (opts.hasKey("statusPositionMultiplier")) {
      customization.statusPositionMultiplier = opts.getDouble("statusPositionMultiplier");
    }

    if (opts.hasKey("showResultStatusMessages")) {
      customization.showResultStatusMessages = opts.getBoolean("showResultStatusMessages");
    }

    if (opts.hasKey("resultStatusTextFont")) {
      customization.resultStatusTextFont = Typeface.create(opts.getString("resultStatusTextFont"),
          opts.getInt("resultStatusTextFontStyle"));// CHECK
    }

    if (opts.hasKey("resultStatusTextSize")) {
      customization.resultStatusTextSize = opts.getInt("resultStatusTextSize");
    }

    if (opts.hasKey("resultStatusTextColor")) {
      customization.resultStatusTextColor = opts.getString("resultStatusTextColor");
    }

    if (opts.hasKey("resultStatusBackgroundColor")) {
      customization.resultStatusBackgroundColor = opts.getString("resultStatusBackgroundColor");
    }

    if (opts.hasKey("resultStatusPositionMultiplier")) {
      customization.resultStatusPositionMultiplier = opts.getDouble("resultStatusPositionMultiplier");
    }

    if (opts.hasKey("showHelpAnimation")) {
      customization.showHelpAnimation = opts.getBoolean("showHelpAnimation");
    }

    if (opts.hasKey("showNextPageAnimation")) {
      customization.showNextPageAnimation = opts.getBoolean("showNextPageAnimation");
    }

    if (opts.hasKey("helpAnimationImageID")) {
      customization.helpAnimationImageID = opts.getInt("helpAnimationImageID"); // May not work
    }

    if (opts.hasKey("multipageAnimationFrontImage")) {
      customization.multipageAnimationFrontImage = opts.getInt("multipageAnimationFrontImage"); // May not work
    }

    if (opts.hasKey("multipageAnimationBackImage")) {
      customization.multipageAnimationBackImage = opts.getInt("multipageAnimationBackImage"); // May not work
    }

    if (opts.hasKey("tintColor")) {
      customization.tintColor = opts.getString("tintColor");
    }

    if (opts.hasKey("multipageButtonBackgroundColor")) {
      customization.multipageButtonBackgroundColor = opts.getString("multipageButtonBackgroundColor");
    }

    if (opts.hasKey("activityIndicatorColor")) {
      customization.activityIndicatorColor = opts.getString("activityIndicatorColor");
    }

    if (opts.hasKey("showBackgroundMask")) {
      customization.showBackgroundMask = opts.getBoolean("showBackgroundMask");
    }

    if (opts.hasKey("borderBackgroundImage")) {
      customization.borderBackgroundImage = opts.getInt("borderBackgroundImage"); // May not work
    }
  }
}