package com.eactive.eai.custom.adapter.http.dynamic.filter;

import java.util.Arrays;
import java.util.List;
import java.util.Properties;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;

import com.eactive.eai.adapter.AdapterManager;
import com.eactive.eai.adapter.http.dynamic.HttpAdapterServiceKey;
import com.eactive.eai.adapter.http.dynamic.HttpAdapterServiceSupport;
import com.eactive.eai.adapter.http.dynamic.filter.HttpAdapterFilter;
import com.eactive.eai.authserver.service.OAuth2Manager;
import com.eactive.eai.authserver.vo.ClientVO;
import com.eactive.eai.common.dao.DAOException;
import com.eactive.eai.common.property.PropGroupVO;
import com.eactive.eai.common.property.PropManager;
import com.eactive.eai.custom.adapter.http.util.VirtualAccountBatchUtil;
import com.eactive.eai.custom.adapter.http.util.VirtualAccountTransform;

public class VirtualAccountCryptoFilter implements HttpAdapterFilter {
	// FIXME : kbank - 프라퍼티 그릅정의 확정
	private static final String BATCH_CHECK_URL = "/batch/";
	private static final String BATCH_PROP_GROUP = "VIRTUALACCOUNT_INFO";
	private static final String BATCH_FILE_PATH = "BATCH_FILE_PATH";
	private static final String FILE_NAME_PATTERN = "FILE_NAME_PATTERN";
	
	private String getAesKey(String clientId) {
		OAuth2Manager manager = OAuth2Manager.getInstance();			
		try {
			ClientVO client = manager.getClientInfo(clientId);
			return client.getSecurityKey();
		} catch (DAOException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	private boolean isBatch(HttpServletRequest request) {
	    String uri = request.getRequestURI();
	    return uri != null && uri.contains(BATCH_CHECK_URL);
	}
	
	private String getRootFilePath() {
		PropGroupVO vo = PropManager.getInstance().getPropGroupVO(BATCH_PROP_GROUP);
		return vo.getProperty(BATCH_FILE_PATH);
	}
	
	private String getFileNamePattern() {
		PropGroupVO vo = PropManager.getInstance().getPropGroupVO(BATCH_PROP_GROUP);
		return vo.getProperty(FILE_NAME_PATTERN);
	}
	
	@Override
	public Object doPreFilter(String adptGrpName, String adptName, Object message, Properties prop,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		String charset = StringUtils.defaultIfBlank(AdapterManager.getInstance().getAdapterGroupVO(adptGrpName).getMessageEncode(), "UTF-8");
		String encPaths = prop.getProperty(HttpAdapterServiceKey.ENC_PATHS);
		List<String> encFieldList = null;
		String clientId = prop.getProperty(HttpAdapterServiceSupport.PROPERTIES_NAME_CLIENT_ID);		
		String aesKey = getAesKey(clientId);
	
		String convertedMessage = null;
		if(isBatch(request)) {
			String rootFilePath = getRootFilePath();
			String fileNamePattern = getFileNamePattern();
			VirtualAccountBatchUtil batchUtil = new VirtualAccountBatchUtil();
			if(message instanceof byte[]) {
				convertedMessage = new String((byte[])message, charset);
			} else {
				convertedMessage = (String) message;
			}
			batchUtil.unzipAndSave(aesKey, convertedMessage, rootFilePath, fileNamePattern);
		}
		else {
			if(StringUtils.isNotBlank(encPaths)) {
				String[] items = encPaths.split(",");
				encFieldList = Arrays.asList(items);
				if(message instanceof byte[]) {
					convertedMessage = new String((byte[])message, charset);
				} else {
					convertedMessage = (String) message;
				}
				convertedMessage = VirtualAccountTransform.toCubeOne(aesKey, encFieldList, convertedMessage);
			}
		}
		return convertedMessage;
	}

	@Override
	public Object doPostFilter(String adptGrpName, String adptName, Object resultMessage, Properties prop,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		String charset = StringUtils.defaultIfBlank(AdapterManager.getInstance().getAdapterGroupVO(adptGrpName).getMessageEncode(), "UTF-8");
		String encPaths = prop.getProperty(HttpAdapterServiceKey.ENC_PATHS);
		List<String> encFieldList = null;
		String clientId = prop.getProperty(HttpAdapterServiceSupport.PROPERTIES_NAME_CLIENT_ID);		
		String aesKey = getAesKey(clientId);
		
		if(isBatch(request)) {
			return resultMessage;
		}
		else {
			String resMessageString = null;
			if(StringUtils.isNotBlank(encPaths)) {
				String[] items = encPaths.split(",");
				encFieldList = Arrays.asList(items);
				if(resultMessage instanceof byte[]) {
					resMessageString = new String((byte[])resultMessage, charset);
				} else {
					resMessageString = (String) resultMessage;
				}
				resMessageString = VirtualAccountTransform.toAES(aesKey, encFieldList, resMessageString);
			}			
			return resMessageString;
		}
	}
}
