package com.eactive.eai.custom.adapter.http.util;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.eactive.eai.common.util.Logger;
import com.eactive.eai.util.json.JsonPathsTransform;
import com.eactive.eai.util.json.transformer.ValueTransformer;

public class VirtualAccountTransform {
	protected static Logger logger = Logger.getLogger(Logger.LOGGER_ADAPTER);
	
	public static String toAES(String aesKey, List<String> encFieldPaths, String message) {
		ToAesTransformer encTramsformer = new ToAesTransformer();
		encTramsformer.setProperty("AESKEY", aesKey);

		Map<String, ValueTransformer<String, String>> pathTransformerMap = new HashMap<>();
		for (String path : encFieldPaths) {
			pathTransformerMap.put(path.trim(), encTramsformer);
		}
		String modifiedJsonString = JsonPathsTransform.modifyValuesAtPaths(message, pathTransformerMap, false);

		return modifiedJsonString;
	}

	public static byte[] toCubeOne(String aesKey, List<String> encFieldPaths, byte[] messageBytes) {
		String defaultCharset = "utf-8";
		try {
			String message = new String(messageBytes, defaultCharset);
			String retMessage = toCubeOne(aesKey, encFieldPaths, message);
			return retMessage.getBytes(defaultCharset);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			return messageBytes;
		}
	}

	public static String toCubeOne(String aesKey, List<String> encFieldPaths, String message) {
		ToCubeOneTransformer encTramsformer = new ToCubeOneTransformer();
		
		if(logger.isDebug()) {
			logger.debug("AESKEY=" + aesKey);
		}
		encTramsformer.setProperty("AESKEY", aesKey);

		Map<String, ValueTransformer<String, String>> pathTransformerMap = new HashMap<>();
		for (String path : encFieldPaths) {
			pathTransformerMap.put(path.trim(), encTramsformer);
		}
		String modifiedJsonString = JsonPathsTransform.modifyValuesAtPaths(message, pathTransformerMap, false);

		return modifiedJsonString;
	}

	public static void main(String[] args) {
		String aesKey = "ONAuROzlqWB4VuBwO5C/FA==";
		String text = "Hello AES 한글";
		String encText = "3hQyg4xrUrFQt09XKLkmutQ4pZyuKc242LGQ1dyDMTg=";
		String json = "{\"encAccount\":\"3hQyg4xrUrFQt09XKLkmutQ4pZyuKc242LGQ1dyDMTg=\"}";
		List<String> list = new ArrayList<>();
		list.add(" $.encAccount ");
		list.add(" $.accounts$.encAccount ");
		String cubeOneJson = toCubeOne(aesKey, list, json);
		// System.out.println(cubeOneJson);
		// System.out.println(toAES(aesKey, list, cubeOneJson));
	}

}