package com.eactive.eai.custom.message;

import org.apache.commons.lang3.StringUtils;

import com.eactive.eai.common.server.EAIServerManager;
import com.eactive.eai.common.util.StringUtil;
import com.eactive.eai.message.StandardMessage;
import com.eactive.eai.message.service.DefaultInterfaceMapper;
import com.ext.eai.common.stdmessage.STDMessageKeys;

@SuppressWarnings("serial")
public class InterfaceMapperKJB extends DefaultInterfaceMapper {
	private static String SESSION_ID = "INTERFACE_COMMON.MCI_SESN_ID";
	private static String INSTANCE_ID = "INTERFACE_COMMON.MCI_INSTNC_ID";

	@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
		if (interfaceId == null)
			interfaceId = "";
		interfaceId = interfaceId.trim();

		// TODO : site에 맞게 조합해야 함.
		eaiSvcCode = interfaceId + returnType + StringUtils.defaultString(inExDivision);
		return eaiSvcCode;
	}

	@Override
	public void setGuid(StandardMessage standardMessage, String guid) {
		if (guid == null)
			throw new RuntimeException("guid is null");
		if (guid.length() != 36)
			throw new RuntimeException("The length of guid is not 36");
		setItemValue(standardMessage, getPath(GUID), guid);
	}

	@Override
	public String getGuid(StandardMessage standardMessage) {
		String guid = getItemValue(standardMessage, getPath(GUID));
		if (guid.length() == 36) {
			guid = guid.substring(0, 33);
		}
		return guid;
	}

	@Override
	public String getGuidSeq(StandardMessage standardMessage) {
		String guidseq = null;
		String guid = getItemValue(standardMessage, getPath(GUID));
		if (guid.length() == 36) {
			guidseq = guid.substring(33);
		} else {
			guidseq = "000";
		}
		return guidseq;
	}

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

	@Override
	public String getOrgGuid(StandardMessage standardMessage) {
		String orgGuid = getItemValue(standardMessage, getPath(GUID));
		if (orgGuid.length() == 36) {
			orgGuid = orgGuid.substring(0, 33);
		}
		return orgGuid;
	}

	@Override
	public void setOrgGuid(StandardMessage standardMessage, String orgGuid) {
		if (orgGuid.length() == 36) {
			orgGuid = orgGuid.substring(0, 33);
		}
		setItemValue(standardMessage, getPath(GUID_ORG), orgGuid);
	}

	@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 = StringUtil.stringFormat(Integer.toString(seq), true, '0', 3);
		setGuidSeq(standardMessage, guidSeq);
		return guidSeq;
	}

	/**
	 * 표준전문의 복원은 COMMON.ORTR_RESTR_YN 의 value는 (Y/N) 엔진에서 복원여부 의 value는 (1/0)
	 */
	@Override
	public String getRecoverYn(StandardMessage standardMessage) {
		String data = getItemValue(standardMessage, getPath(RECOVER_YN));
		if ("Y".equals(data)) {
			return "1";
		} else {
			return "0";
		}
	}

	@Override
	public void setRecoverYn(StandardMessage standardMessage, String recoverYn) {
		if ("1".equals(recoverYn)) {
			setItemValue(standardMessage, getPath(RECOVER_YN), "Y");
		} else {
			setItemValue(standardMessage, getPath(RECOVER_YN), "N");
		}
	}

	@Override
	public String getInstCode(StandardMessage standardMessage) {
		EAIServerManager server = EAIServerManager.getInstance();
		if (server != null && server.isMCI()) {
			return getItemValue(standardMessage, INSTANCE_ID);
		} else {
			return "";
		}
	}

	@Override
	public void setInstCode(StandardMessage standardMessage, String instCode) {
		EAIServerManager server = EAIServerManager.getInstance();
		if (server != null && server.isMCI()) {
			setItemValue(standardMessage, INSTANCE_ID, instCode);
		}
	}

	@Override
	public String getSessionId(StandardMessage standardMessage) {
		EAIServerManager server = EAIServerManager.getInstance();
		if (server != null && server.isMCI()) {
			return getItemValue(standardMessage, SESSION_ID);
		} else {
			return "";
		}
	}

	@Override
	public void setSessionId(StandardMessage standardMessage, String sessionId) {
		EAIServerManager server = EAIServerManager.getInstance();
		if (server != null && server.isMCI()) {
			setItemValue(standardMessage, SESSION_ID, sessionId);
		}
	}


	/*
	 * 엔진 에서는 public static final String RESPONSE_TYPE_CODE_N = "0";//정상(normal)
	 * public static final String RESPONSE_TYPE_CODE_E = "1";//오류(error)
	 */
	@Override
	public void setResponseType(StandardMessage standardMessage, String responseType) {
		if (responseType != null && (STDMessageKeys.RESPONSE_TYPE_CODE_N.equals(responseType)
				|| STDMessageKeys.RESPONSE_TYPE_CODE_E.equals(responseType))) {
			if (STDMessageKeys.RESPONSE_TYPE_CODE_N.equals(responseType)) {
				setItemValue(standardMessage, getPath(RESPONSE_TYPE), "NR");
			} else {
				setItemValue(standardMessage, getPath(RESPONSE_TYPE), "ER");
			}
		} else {
			setItemValue(standardMessage, getPath(RESPONSE_TYPE), responseType);
		}

	}

	@Override
	public String getInterfaceId(StandardMessage standardMessage) {
		String interfaceId = getItemValue(standardMessage, getPath(INTERFACE_ID));
		if (interfaceId == null)
			return "";
		interfaceId = interfaceId.trim();
		return interfaceId;
	}

	@Override
	public String getReqSysCode(StandardMessage standardMessage) {
		// apiPath의 값에서 추출할 예정 : serviceId = api_path
		String apiPath = getItemValue(standardMessage, getPath(SERVICE_ID));
		if (apiPath != null) {
			String[] split = apiPath.split("/");
			if (split.length > 2) {
				String route = split[1].toUpperCase();
				if (route.length() > 3) {
					route = route.substring(0, 3);
				}
				return route;
			}
		}
		return null;
	}
}