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

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.Properties;
import java.util.TreeMap;

import com.eactive.eai.adapter.http.dynamic.HttpAdapterServiceKey;
import com.eactive.eai.common.property.PropGroupVO;
import com.eactive.eai.common.property.PropManager;
import com.eactive.eai.common.util.Logger;


public class KAKAOBankFilter implements HttpClientAdapterFilter, HttpAdapterServiceKey {
	protected static Logger logger = Logger.getLogger(Logger.LOGGER_ADAPTER);

	public static final String KJB_HEADER = "KJB_Header";
	public static final String PROP_PREFIX = "kakaobank";
	public static final String X_KKB_PARTNER_CODE = "X-KKB-PARTNER-CODE";
	public static final String X_KKB_API_NAME = "X-KKB-API-NAME";
	public static final String X_KKB_API_TX_ID = "X-KKB-API-TX-ID";
	public static final String X_KKB_TX_TIME = "X-KKB-TX-TIME";
	public static final String X_KKB_CMPR_MGMT_NO = "X-KKB-CMPR-MGMT-NO";

	@Override
	public Object doPreFilter(String adptGrpName, String adptName, Properties prop, Object message, Properties tempProp)
			throws Exception {

		logger.debug("KAKAOBankFilter] PreFilter Processing Start!!");
		Object returnMessage = message;
		String partnerCode = "KJB";
		String apiName = "status_change";
		String apiTxId = "0";
		String txTime = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
		String cmprMgmtNo = "0";

		PropGroupVO propGroupVo = PropManager.getInstance().getPropGroupVO(KJB_HEADER);
		if (propGroupVo != null) {
			partnerCode = propGroupVo.getProperty(PROP_PREFIX + "." + X_KKB_PARTNER_CODE);
			apiName = propGroupVo.getProperty(PROP_PREFIX + "." + X_KKB_API_NAME);
			apiTxId = propGroupVo.getProperty(PROP_PREFIX + "." + X_KKB_API_TX_ID);
			cmprMgmtNo = propGroupVo.getProperty(PROP_PREFIX + "." + X_KKB_CMPR_MGMT_NO);
		}

		Properties filterHeaders = (Properties) tempProp.get("FILTERHEADER");
		if (filterHeaders == null) {
			filterHeaders = new Properties();
			tempProp.put("FILTERHEADER", filterHeaders);
		}
		try {
			Map<String, String> inboundHeaderMap = getInboundHeaderProp(tempProp);

			if (!inboundHeaderMap.isEmpty()) {
				if (inboundHeaderMap.containsKey(X_KKB_PARTNER_CODE)) {
					filterHeaders.put(X_KKB_PARTNER_CODE, inboundHeaderMap.get(X_KKB_PARTNER_CODE));
				} else {
					filterHeaders.put(X_KKB_PARTNER_CODE, partnerCode);
				}
				if (inboundHeaderMap.containsKey(X_KKB_API_NAME)) {
					filterHeaders.put(X_KKB_API_NAME, inboundHeaderMap.get(X_KKB_API_NAME));
				} else {
					filterHeaders.put(X_KKB_API_NAME, apiName);
				}
				if (inboundHeaderMap.containsKey(X_KKB_API_TX_ID)) {
					filterHeaders.put(X_KKB_API_TX_ID, inboundHeaderMap.get(X_KKB_API_TX_ID));
				} else {
					filterHeaders.put(X_KKB_API_TX_ID, apiTxId);
				}
				if (inboundHeaderMap.containsKey(X_KKB_TX_TIME)) {
					filterHeaders.put(X_KKB_TX_TIME, inboundHeaderMap.get(X_KKB_TX_TIME));
				} else {
					filterHeaders.put(X_KKB_TX_TIME, txTime);
				}
				if (inboundHeaderMap.containsKey(X_KKB_CMPR_MGMT_NO)) {
					filterHeaders.put(X_KKB_CMPR_MGMT_NO, inboundHeaderMap.get(X_KKB_CMPR_MGMT_NO));
				} else {
					filterHeaders.put(X_KKB_CMPR_MGMT_NO, cmprMgmtNo);
				}
			} else {
				filterHeaders.put(X_KKB_PARTNER_CODE, partnerCode);
				filterHeaders.put(X_KKB_API_NAME, apiName);
				filterHeaders.put(X_KKB_API_TX_ID, apiTxId);
				filterHeaders.put(X_KKB_TX_TIME, txTime);
				filterHeaders.put(X_KKB_CMPR_MGMT_NO, cmprMgmtNo);
			}
			logger.debug("KAKAOBankFilter] Processing Key ["+X_KKB_PARTNER_CODE+"], value ["+filterHeaders.getProperty(X_KKB_PARTNER_CODE)+"]");
			logger.debug("KAKAOBankFilter] Processing Key ["+X_KKB_API_NAME+"], value ["+filterHeaders.getProperty(X_KKB_API_NAME)+"]");
			logger.debug("KAKAOBankFilter] Processing Key ["+X_KKB_API_TX_ID+"], value ["+filterHeaders.getProperty(X_KKB_API_TX_ID)+"]");
			logger.debug("KAKAOBankFilter] Processing Key ["+X_KKB_TX_TIME+"], value ["+filterHeaders.getProperty(X_KKB_TX_TIME)+"]");
			logger.debug("KAKAOBankFilter] Processing Key ["+X_KKB_CMPR_MGMT_NO+"], value ["+filterHeaders.getProperty(X_KKB_CMPR_MGMT_NO)+"]");

		} catch (Exception e) {
			logger.error(e.getMessage());
		}
		
		return returnMessage;
	}

	public Map<String, String> getInboundHeaderProp(Properties prop) {
		Properties header = new Properties();
		Map<String, String> inboundHeaderMap = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
		try {
			Object headerObj = prop.get(HttpAdapterServiceKey.INBOUND_HEADER);
			if (headerObj instanceof Properties) {    //tomcat
				header = (Properties) headerObj;
				for (String name : header.stringPropertyNames()) {
					inboundHeaderMap.put(name, header.getProperty(name));
				}
			} else if (headerObj instanceof String) {  //weblogic
				String str = (String) headerObj;
				str = str.substring(1, str.length() - 1);
				// key=value 쌍 분리
				String[] entries = str.split(",");
				for (String entry : entries) {
				    String[] kv = entry.split("=", 2);
				    if (kv.length == 2) {
				    	inboundHeaderMap.put(kv[0].trim(), kv[1].trim());
				    }
				}
			}	
		} catch (Exception  e) {
		    e.printStackTrace();
		}
		return inboundHeaderMap;
	}

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

}

