package com.eactive.eai.custom.message;

import java.util.Properties;

import com.eactive.eai.common.server.EAIServerManager;
import com.eactive.eai.common.server.Keys;
import com.eactive.eai.common.util.DatetimeUtil;
import com.eactive.eai.common.util.MessageUtil;
import com.eactive.eai.common.util.StringUtil;
import com.eactive.eai.message.StandardItem;
import com.eactive.eai.message.StandardMessage;
import com.eactive.eai.message.manager.StandardMessageManager;
import com.eactive.eai.message.service.DefaultStandardMessageCoordinator;
import com.eactive.eai.message.service.InterfaceMapper;
import com.ext.eai.common.stdmessage.STDMessageKeys;

public class StandardMessageCoordinatorKBANK extends DefaultStandardMessageCoordinator {
	
	private static final String MESSAGE_PART_SUB_MSG_GOUP = "message_part.subMsgGoup";
	private static final String MESSAGE_PART_MSG_TYPE = "message_part.msgType";
	private static final String MESSAGE_PART = "message_part";

	@Override
	public void coordinateAfterParsing(StandardMessage standardMessage, Object paramObject, Properties prop) {
	}
	
	@Override
	public void coordinateAfterCreateMessageByRule(StandardMessage standardMessage, Object paramObject, Properties prop) {
		// guid 설정
		StandardMessageManager standardManager = StandardMessageManager.getInstance();
		InterfaceMapper mapper = standardManager.getMapper();
		
		String guid = GUIDGeneratorKBANK.makeGUID();
		
		mapper.setGuid(standardMessage, guid);
		
		String orgGuid = guid + mapper.getGuidSeq(standardMessage);
		
		mapper.setOrgGuid(standardMessage, orgGuid);
		
		mapper.setOperationEnv(standardMessage, System.getProperty(Keys.EAI_SYSTEMMODE));
		
		mapper.setFirstReqIp(standardMessage, EAIServerManager.getInstance().getServerIp());
	}
	
	@Override
	public boolean coordinateSetStandardMessageError(StandardMessage standardMessage, InterfaceMapper mapper,
			String errCode, String errMsg) {
		
		mapper.setResponseType(standardMessage, STDMessageKeys.RESPONSE_TYPE_CODE_E);
		
		StandardItem messagePart = standardMessage.findItem(MESSAGE_PART);
		messagePart.setHidden(false);
		messagePart.setSize(1);
		StandardItem messageType = standardMessage.findItem(MESSAGE_PART_MSG_TYPE);
		messageType.setValue("99");
		StandardItem subMsgGoup = standardMessage.findItem(MESSAGE_PART_SUB_MSG_GOUP);
		subMsgGoup.getList().clear();
		subMsgGoup.setRefValue("0");
		
		if(errCode != null) {
			String siteErrCode = MessageUtil.getTobeCode(errCode);
			String siteErrMessage;
			switch(errCode) {
			case("RECEAIIRP070"):
				siteErrMessage = "거래 통제 되었습니다.";
				break;
			default:
				siteErrMessage = MessageUtil.getTobeMessage(errCode);	break;
			}

			mapper.setErrorCode(standardMessage, siteErrCode);
			mapper.setErrorMsg(standardMessage, siteErrMessage);
		}
	     
		return false;
	}

	
	@Override
	public void coordinateAfterRecvNonStdSyncResponse(StandardMessage responseMessage) {
		StandardItem messagePart = responseMessage.findItem(MESSAGE_PART);
		messagePart.setHidden(false);
		messagePart.setSize(1);
		StandardItem subMsgGoup = responseMessage.findItem(MESSAGE_PART_SUB_MSG_GOUP);
		subMsgGoup.getList().clear();
		subMsgGoup.setRefValue("0");
	}
	
}
