package com.eactive.eai.message.service;
import java.util.HashMap;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.eactive.eai.message.StandardItem;
import com.eactive.eai.message.StandardMessage;

@SuppressWarnings("serial")
public class DefaultInterfaceMapper implements InterfaceMapper {
	static Logger logger = LoggerFactory.getLogger(DefaultInterfaceMapper.class);

	HashMap<String, String> itemPathMap = new HashMap<String, String>();
	
	public void initPathMap(HashMap<String, String> map) {
		itemPathMap = map;
	}
	
	public String getPath(String key) {
		String path = itemPathMap.get(key);
		if(StringUtils.isBlank(path)) {
			logger.debug("message-mapping not defined key = {}", key);
		}
		return path;
	}
	
	public DefaultInterfaceMapper() {

	}

	protected String getItemValue(StandardMessage standardMessage, String path) {
		if(standardMessage == null || StringUtils.isBlank(path)) return null;
		return standardMessage.findItemValue(path);		
	}

	protected void setItemValue(StandardMessage standardMessage, String path, String value) {
		if(standardMessage == null || StringUtils.isBlank(path)) {
			return;
		}
		StandardItem item = standardMessage.findItem(path);
		if(item != null) item.setValue(value);		
	}
	//------------------------------------------------------------
	// 표준전문과 Mapping 되는 항목을 모두 정의해야 함.
	//------------------------------------------------------------
	@Override
	public String getEaiSvcCode(StandardMessage standardMessage) {
		String eaiSvcCode = null;
		String interfaceId = getInterfaceId(standardMessage);
		String returnType = getSendRecvDivision(standardMessage);  // S | R
		String inExDivision = getInExDivision(standardMessage); // 1 | 2
		// TODO : site에 맞게 조합해야 함.
		eaiSvcCode = interfaceId + returnType + StringUtils.defaultString(inExDivision);
		return eaiSvcCode;
	}
	
	@Override
	public String nextGuidSeq(StandardMessage standardMessage) {
		String guidSeq = getGuidSeq(standardMessage);
		if(guidSeq == null) return "";
		int seq = 0;
		try {
			seq = Integer.parseInt(guidSeq) + 1;
		}
		catch(NumberFormatException e) {
			seq = 1;
		}
		guidSeq = Integer.toString(seq);
		setGuidSeq(standardMessage, guidSeq);
		return guidSeq;
	}
	
	@Override
	public String getServiceId(StandardMessage standardMessage) {
		return getItemValue(standardMessage, getPath(SERVICE_ID));
	}

	@Override
	public void setServiceId(StandardMessage standardMessage, String serviceId) {
		setItemValue(standardMessage,getPath(SERVICE_ID), serviceId);
	}

	@Override
	public String getReqSysCode(StandardMessage standardMessage) {
		return getItemValue(standardMessage, getPath(REQ_SYS_CODE));
	}

	@Override
	public String getChannelTypeCd(StandardMessage standardMessage) {
		return getItemValue(standardMessage, getPath(CHANNEL_TYPE_CD));
	}

	@Override
	public String getChannelDetailCd(StandardMessage standardMessage) {
		return getItemValue(standardMessage, getPath(CHANNEL_DETAIL_CD));
	}

	@Override
	public String getSendTime(StandardMessage standardMessage) {
		return getItemValue(standardMessage, getPath(SEND_TIME));
	}

	@Override
	public String getGuid(StandardMessage standardMessage) {
		return getItemValue(standardMessage, getPath(GUID));
	}

	@Override
	public String getSystemType(StandardMessage standardMessage) {
		return getItemValue(standardMessage, getPath(SYSTEM_TYPE));
	}

	@Override
	public String getRecvTime(StandardMessage standardMessage) {
		return getItemValue(standardMessage, getPath(RECV_TIME));
	}

	@Override
	public String getReturnValue(StandardMessage standardMessage) {
		return getItemValue(standardMessage, getPath(RETURN_VALUE));
	}

	@Override
	public String getOrgGuid(StandardMessage standardMessage) {
		return getItemValue(standardMessage, getPath(GUID_ORG));
	}

	@Override
	public String getGuidSeq(StandardMessage standardMessage) {
		return getItemValue(standardMessage, getPath(GUID_SEQ));
	}

	@Override
	public String getReturnServiceId(StandardMessage standardMessage) {
		return getItemValue(standardMessage, getPath(RETURN_SERVICE_ID));
	}

	@Override
	public String getInterfaceId(StandardMessage standardMessage) {
		return getItemValue(standardMessage, getPath(INTERFACE_ID));
	}

	@Override
	public String getSessionId(StandardMessage standardMessage) {
		return getItemValue(standardMessage, getPath(SESSION_ID));
	}

	@Override
	public String getSendRecvDivision(StandardMessage standardMessage) {
		return getItemValue(standardMessage, getPath(SEND_RECV_DIVISION));
	}

	@Override
	public String getInExDivision(StandardMessage standardMessage) {
		return getItemValue(standardMessage, getPath(IN_EX_DIVISION));
	}

	@Override
	public String getOperationEnv(StandardMessage standardMessage) {
		return getItemValue(standardMessage, getPath(OPERATION_ENV));
	}

	@Override
	public String getFirstReqIp(StandardMessage standardMessage) {
		return getItemValue(standardMessage, getPath(FIRST_REQ_IP));
	}

	@Override
	public String getRecoverYn(StandardMessage standardMessage) {
		return getItemValue(standardMessage, getPath(RECOVER_YN));
	}

	@Override
	public String getInstCode(StandardMessage standardMessage) {
		return getItemValue(standardMessage, getPath(INST_CODE));
	}

	@Override
	public String getTimeout(StandardMessage standardMessage) {
		return getItemValue(standardMessage, getPath(TIMEOUT));
	}

	@Override
	public String getErrorCode(StandardMessage standardMessage) {
		return getItemValue(standardMessage, getPath(ERROR_CODE));
	}

	@Override
	public String getErrorMsg(StandardMessage standardMessage) {
		return getItemValue(standardMessage, getPath(ERROR_MSG));
	}

	@Override
	public String getUserId(StandardMessage standardMessage) {
		return getItemValue(standardMessage, getPath(USER_ID));
	}

	@Override
	public String getResponseType(StandardMessage standardMessage) {
		return getItemValue(standardMessage, getPath(RESPONSE_TYPE));
	}

	@Override
	public String getSyncAsyncType(StandardMessage standardMessage) {
		return getItemValue(standardMessage, getPath(SYNC_ASYNC_TYPE));
	}

	@Override
	public void setReqSysCode(StandardMessage standardMessage, String reqSysCode) {
		setItemValue(standardMessage,getPath(REQ_SYS_CODE), reqSysCode);
		
	}

	@Override
	public void setChannelTypeCd(StandardMessage standardMessage, String channelTypeCd) {
		setItemValue(standardMessage,getPath(CHANNEL_TYPE_CD), channelTypeCd);
		
	}

	@Override
	public void setChannelDetailCd(StandardMessage standardMessage, String channelDetailCd) {
		setItemValue(standardMessage,getPath(CHANNEL_DETAIL_CD), channelDetailCd);
		
	}

	@Override
	public void setSendTime(StandardMessage standardMessage, String sendTime) {
		setItemValue(standardMessage,getPath(SEND_TIME), sendTime);		
	}

	@Override
	public void setGuid(StandardMessage standardMessage, String guid) {
		setItemValue(standardMessage,getPath(GUID), guid);		
	}

	@Override
	public void setSystemType(StandardMessage standardMessage, String systemType) {
		setItemValue(standardMessage,getPath(SYSTEM_TYPE), systemType);		
	}

	@Override
	public void setRecvTime(StandardMessage standardMessage, String recvTime) {
		setItemValue(standardMessage,getPath(RECV_TIME), recvTime);		
	}

	@Override
	public void setReturnValue(StandardMessage standardMessage, String returnValue) {
		setItemValue(standardMessage,getPath(RETURN_VALUE), returnValue);		
	}

	@Override
	public void setOrgGuid(StandardMessage standardMessage, String orgGuid) {
		setItemValue(standardMessage,getPath(GUID_ORG), orgGuid);		
	}

	@Override
	public void setGuidSeq(StandardMessage standardMessage, String guidSeq) {
		setItemValue(standardMessage,getPath(GUID_SEQ), guidSeq);		
	}

	@Override
	public void setReturnServiceId(StandardMessage standardMessage, String returnServiceId) {
		setItemValue(standardMessage,getPath(RETURN_SERVICE_ID), returnServiceId);		
	}

	@Override
	public void setInterfaceId(StandardMessage standardMessage, String interfaceId) {
		setItemValue(standardMessage,getPath(INTERFACE_ID), interfaceId);		
	}

	@Override
	public void setSessionId(StandardMessage standardMessage, String sessionId) {
		setItemValue(standardMessage,getPath(SESSION_ID), sessionId);		
	}

	@Override
	public void setSendRecvDivision(StandardMessage standardMessage, String sendRecvDivision) {
		setItemValue(standardMessage,getPath(SEND_RECV_DIVISION), sendRecvDivision);		
	}

	@Override
	public void setInExDivision(StandardMessage standardMessage, String inExDivision) {
		setItemValue(standardMessage,getPath(IN_EX_DIVISION), inExDivision);		
	}

	@Override
	public void setOperationEnv(StandardMessage standardMessage, String operationEnv) {
		setItemValue(standardMessage,getPath(OPERATION_ENV), operationEnv);		
	}

	@Override
	public void setFirstReqIp(StandardMessage standardMessage, String firstReqIp) {
		setItemValue(standardMessage,getPath(FIRST_REQ_IP), firstReqIp);		
	}

	@Override
	public void setRecoverYn(StandardMessage standardMessage, String recoverYn) {
		setItemValue(standardMessage,getPath(RECOVER_YN), recoverYn);
	}

	@Override
	public void setInstCode(StandardMessage standardMessage, String instCode) {
		setItemValue(standardMessage,getPath(INST_CODE), instCode);
	}

	@Override
	public void setTimeout(StandardMessage standardMessage, String timeout) {
		setItemValue(standardMessage,getPath(TIMEOUT), timeout);
	}

	@Override
	public void setErrorCode(StandardMessage standardMessage, String errorCode) {
		setItemValue(standardMessage,getPath(ERROR_CODE), errorCode);
	}

	@Override
	public void setErrorMsg(StandardMessage standardMessage, String errorMsg) {
		setItemValue(standardMessage,getPath(ERROR_MSG), errorMsg);
	}

	@Override
	public void setUserId(StandardMessage standardMessage, String userId) {
		setItemValue(standardMessage,getPath(USER_ID), userId);
	}

	@Override
	public void setResponseType(StandardMessage standardMessage, String responseType) {
		setItemValue(standardMessage,getPath(RESPONSE_TYPE), responseType);
	}

	@Override
	public void setSyncAsyncType(StandardMessage standardMessage, String syncAsyncType) {
		setItemValue(standardMessage,getPath(SYNC_ASYNC_TYPE), syncAsyncType);
	}	
}
