package com.eactive.eai.outbound;

import com.eactive.eai.common.exception.ExceptionUtil;
import com.eactive.eai.common.message.EAIMessageKeys;
import com.eactive.eai.control.LogSender;
import com.eactive.eai.control.Transform;
import com.ext.eai.common.stdmessage.STDMessageKeys;

public class LoopBackProcess extends DefaultProcess {
	static final long serialVersionUID = 1L;

	public void flow() throws Exception {
		if (!isRealCall()) {
			return;
		}

		init();
		// 송신전로그
		logSenderCtrlSendReq();

		// 송신변환
		if (isSourceTransform()) {
			// 수동메시지 변환 DI
			transControlTransformSingleReq();
			// 응답메시지설정
			setTranMessage();
		} else {
			// 메시지 설정
			setReturn();
		}
		setReturnType(this.resEaiMsg, STDMessageKeys.SEND_RECV_CD_RECV);
		// 응답로그설정
		logSenderCtrlSendRes();
		clientReturn();
	}

	public void transControlTransformSingleReq() throws Exception {
		this.srcTranObject = this.reqObject;
		this.diURI = this.reqEaiMsg.getCurrentSvcMsg().getCnvMsgID();
		try {
			this.tgtTranObject = Transform.transformSingle(this.guidLogPrefix, true, this.diURI, this.srcTranObject);
		} catch (Exception e) {
			if (logger.isError())
				logger.error(guidLogPrefix + " 200 변환오류");
			String[] msgArgs = new String[1];
			msgArgs[0] = this.reqEaiMsg.getEAISvcCd();
			String rspErrorCode = "RECEAIOSA020";
			String rspErrorMsg = ExceptionUtil.make(e, rspErrorCode, msgArgs);
			if (logger.isError())
				logger.error(rspErrorMsg);
//			this.resEaiMsg.setRspErrCd(rspErrorCode);
//			this.resEaiMsg.setRspErrMsg(rspErrorMsg);
			this.resEaiMsg.setRspErr(rspErrorCode,rspErrorMsg);
		}
	}

	public void init() throws Exception {
		this.logPssSno = 200;
		this.prop = null;
		this.adapterName = null;
		this.resEaiMsg = this.reqEaiMsg;

		String bizData = this.reqEaiMsg.getStandardMessage().getBizData();
		if (bizData != null && bizData.length() > 0) {
			this.reqObject = bizData;
		}
	}

	public boolean isSourceTransform() {
		if (EAIMessageKeys.YES_FLAG.equals(this.reqEaiMsg.getCurrentSvcMsg().getCnvEn())) {
			return true;
		} else {
			return false;
		}
	}

	public void setReturn() throws Exception {
		if (logger.isDebug())
			logger.debug(guidLogPrefix + " 변환룰이 적용되지 않았으므로 요청메시지를 리턴합니다.");
		this.resEaiMsg.getStandardMessage().setBizData("response", this.inboundCharset);
	}

	public void setTranMessage() throws Exception {
		if (logger.isDebug())
			logger.debug(guidLogPrefix + " 변환룰이 적용되어 변환된 응답메시지를 리턴합니다.");
		this.resEaiMsg.getStandardMessage().setBizData(resObject, this.inboundCharset);
	}

	@Override
	public void logSenderCtrlSendReq() throws Exception {
		try {
			LogSender.send(this.logPssSno, this.reqEaiMsg, this.prop);
		} catch (Exception e) {
			if (logger.isWarn())
				logger.warn(guidLogPrefix + " EAI 서비스코드[" + this.reqEaiMsg.getEAISvcCd() + "] 거래로그 중 오류발생");
		}
	}

	@Override
	public void logSenderCtrlSendRes() throws Exception {
		this.logPssSno = 300;
		LogSender.send(this.logPssSno, this.resEaiMsg, this.prop);

	}

	@Override
	public void outboundCtrlCallService() throws Exception {

	}

	@Override
	public void outboundCtrlCallServiceAsync() throws Exception {

	}

	@Override
	public void transControlTransformSingleRes() throws Exception {

	}

	@Override
	public void printCallError(Exception e1) throws Exception {

	}

	@Override
	public void setRcvLogInfo() throws Exception {

	}

	@Override
	public void setTopicResult() throws Exception {

	}

	@Override
	public void checkResultMessage() throws Exception {

	}

	@Override
	public void checkSendResult() throws Exception {

	}

	@Override
	public void setSendMsg() throws Exception {

	}

	@Override
	public void getTopicProperty() throws Exception {

	}

	@Override
	public void performSend() throws Exception {

	}

	@Override
	public void performRecv() throws Exception {

	}

	@Override
	public void storeMessage() throws Exception {

	}

	@Override
	public void getBizMessage() throws Exception {

	}

	@Override
	public void waitCheckForSync() throws Exception {

	}

	@Override
	public void transControlHeaderTrnsformSinglesRes() throws Exception {

	}

}
