package com.eactive.eai.custom.message;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Properties;

import org.apache.commons.lang3.StringUtils;

import com.eactive.eai.common.server.EAIServerManager;
import com.eactive.eai.common.util.DatetimeUtil;
import com.eactive.eai.common.util.Logger;
import com.eactive.eai.inbound.processor.ProcessVO;
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.eactive.eai.message.service.StandardMessageCoordinator;

public class StandardMessageCoordinatorSBI extends DefaultStandardMessageCoordinator {
	static Logger logger = Logger.getLogger(Logger.LOGGER_DEFAULT);

	@Override
	public void coordinateAfterCreateMessageByRule(StandardMessage message, Object paramObject, Properties prop) {
		// STD_HEADER.mesgDvcd 설정
		StandardItem mesgDvcdItem = message.findItem("STD_HEADER.mesgDvcd");
		if (mesgDvcdItem != null && StringUtils.equals(mesgDvcdItem.getValue(), "S")) {
			mesgDvcdItem.setValue("Q");
		}

		// STD_HEADER.mesgKind 설정
		StandardItem mesgKindItem = message.findItem("STD_HEADER.mesgKind");
		if (mesgKindItem != null && StringUtils.isBlank(mesgKindItem.getValue())) {
			mesgKindItem.setValue("3"); // 3:데이터 4:시스템
		}

		// STD_HEADER.envDvcd 설정
		StandardItem envDvcdItem = message.findItem("STD_HEADER.envDvcd");
		EAIServerManager eaiServerManager = EAIServerManager.getInstance();
		if (envDvcdItem != null && StringUtils.isBlank(envDvcdItem.getValue())) {
			envDvcdItem.setValue(getSysOprtEnvDvcd(eaiServerManager));
		}

		// guid 설정
		StandardMessageManager standardManager = StandardMessageManager.getInstance();
		InterfaceMapper mapper = standardManager.getMapper();
		mapper.setGuid(message,
				GUIDGeneratorSBI.getGUID(getSysOprtEnvDvcd(eaiServerManager), eaiServerManager.getSystemType()));

		// STD_HEADER.frstTrnmChnlCd 설정
		StandardItem frstTrnmChnlCdItem = message.findItem("STD_HEADER.frstTrnmChnlCd");
		if (frstTrnmChnlCdItem != null && StringUtils.isBlank(frstTrnmChnlCdItem.getValue())) {
			frstTrnmChnlCdItem.setValue(eaiServerManager.getSystemType());
		}

		// STD_HEADER.trnmChnlCd 설정
		StandardItem trnmChnlCdItem = message.findItem("STD_HEADER.trnmChnlCd");
		if (trnmChnlCdItem != null && StringUtils.isBlank(trnmChnlCdItem.getValue())) {
			trnmChnlCdItem.setValue(eaiServerManager.getSystemType());
		}

		// STD_HEADER.userNo 설정
		StandardItem userNoItem = message.findItem("STD_HEADER.userNo");
		if (userNoItem != null && StringUtils.isBlank(userNoItem.getValue())) {
			ProcessVO processVo = (ProcessVO) paramObject;
			if (StringUtils.isNotBlank(processVo.getInAdapterUserEmpid())) {
				userNoItem.setValue(eaiServerManager.getSystemType());
			}
		}

		// STD_HEADER.mesgDmndDttm 설정
		StandardItem mesgDmndDttmItem = message.findItem("STD_HEADER.mesgDmndDttm");
		if (mesgDmndDttmItem != null && StringUtils.isBlank(mesgDmndDttmItem.getValue())) {
			mesgDmndDttmItem.setValue(DatetimeUtil.getCurrentTimeMillis());
		}

		// STD_HEADER.ipAd 설정
		StandardItem ipAdItem = message.findItem("STD_HEADER.ipAd");
		if (ipAdItem != null && StringUtils.isBlank(ipAdItem.getValue())) {
			try {
				ipAdItem.setValue(InetAddress.getLocalHost().getHostAddress());
			} catch (UnknownHostException e) {
				logger.error(e.getMessage());
			}
		}

		// STD_HEADER.mesgTycd 설정
		StandardItem mesgTycdItem = message.findItem("STD_HEADER.mesgTycd");
		if (mesgTycdItem != null && StringUtils.isBlank(mesgTycdItem.getValue())) {
			mesgTycdItem.setValue("1");
		}

		// STD_HEADER.mesgCntySrno 설정
		StandardItem mesgCntySrnoItem = message.findItem("STD_HEADER.mesgCntySrno");
		if (mesgCntySrnoItem != null && StringUtils.isBlank(mesgCntySrnoItem.getValue())) {
			mesgCntySrnoItem.setValue("00");
		}

		// STD_HEADER.bfrDtLoinYn 설정
		StandardItem bfrDtLoinYnItem = message.findItem("STD_HEADER.bfrDtLoinYn");
		if (bfrDtLoinYnItem != null && StringUtils.isBlank(bfrDtLoinYnItem.getValue())) {
			bfrDtLoinYnItem.setValue("N");
		}

		// STD_HEADER.canCrctDvcd 설정
		StandardItem canCrctDvcdItem = message.findItem("STD_HEADER.canCrctDvcd");
		if (canCrctDvcdItem != null && StringUtils.isBlank(canCrctDvcdItem.getValue())) {
			canCrctDvcdItem.setValue("0");
		}

		// STD_HEADER.cmpgRelmUseDvcd 설정
		StandardItem cmpgRelmUseDvcdItem = message.findItem("STD_HEADER.cmpgRelmUseDvcd");
		if (cmpgRelmUseDvcdItem != null && StringUtils.isBlank(cmpgRelmUseDvcdItem.getValue())) {
			cmpgRelmUseDvcdItem.setValue("0");
		}

		// STD_HEADER.bzwrSvrCd 설정
		StandardItem bzwrSvrCdItem = message.findItem("STD_HEADER.bzwrSvrCd");
		if (bzwrSvrCdItem != null && StringUtils.isBlank(bzwrSvrCdItem.getValue())) {
			String serverName = eaiServerManager.getLocalServerName();
			if (StringUtils.isNotBlank(serverName)) {
				String instanceid1 = serverName.substring(0, 2);
				String instanceid2 = serverName.substring(serverName.length() - 2, serverName.length());
				bzwrSvrCdItem.setValue(StringUtils.upperCase(instanceid1 + instanceid2));
			}
		}
	}

	public String getSysOprtEnvDvcd(EAIServerManager eaiServerManager) {
		String result = "";
		if (eaiServerManager.isPEAIServer()) { // 운영
			result = "R";
		} else if (eaiServerManager.isDEAIServer()) { // 개발
			result = "D";
		} else if (eaiServerManager.isSEAIServer()) { // 테스트/검증
			result = "T";
		}
		return result;
	}


}
