package com.eactive.eai.custom.adapter.http.client.impl.filter;

import java.util.Properties;

import com.eactive.eai.adapter.http.client.impl.HttpClient5AdapterServiceRest;
import com.eactive.eai.adapter.http.client.impl.filter.HttpClientAdapterFilter;
import com.eactive.eai.adapter.http.client.impl.filter.OutCryptoFilter;
import com.eactive.eai.common.util.JacksonUtil;
import com.eactive.eai.util.JsonPathUtil;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
/**
 * 제주은행 Open API 기본 암복호화 필더
 */
public class EncFieldOutCryptoFilter implements HttpClientAdapterFilter {

	private final OutCryptoFilter filter = new OutCryptoFilter();

	protected Properties setProp(Properties tempProp) {
		tempProp.setProperty(OutCryptoFilter.PROP_DEC_FROM_PATH, "/encryptedData");
		tempProp.setProperty(OutCryptoFilter.PROP_ENC_TO_PATH, "/encryptedData");
		return tempProp;
	}

	@Override
	public Object doPreFilter(String adptGrpName, String adptName, Properties prop, Object message, Properties tempProp)
			throws Exception {
		String headerGroupName = prop.getProperty(HttpClient5AdapterServiceRest.HEADER_GROUP);
		
		JsonNode root = JacksonUtil.readTree(message);
		ObjectNode rootObjectNode = (ObjectNode) root;
		JsonNode header = rootObjectNode.get(headerGroupName);
		rootObjectNode.remove(headerGroupName);
		
		Object enc = filter.doPreFilter(adptGrpName, adptName, prop, rootObjectNode, setProp(tempProp));
		
		ObjectNode encJson = (ObjectNode)JacksonUtil.readTree(enc);
		encJson.set(headerGroupName, header);
		
		return encJson;
		
	}

	@Override
	public Object doPostFilter(String adptGrpName, String adptName, Properties prop, Object message,
			Properties tempProp) throws Exception {
		return filter.doPostFilter(adptGrpName, adptName, prop, message, setProp(tempProp));
	}
}
