package com.eactive.eai.common.routing;

import java.util.Properties;

import com.eactive.eai.adapter.AdapterGroupVO;
import com.eactive.eai.adapter.AdapterManager;
import com.eactive.eai.common.message.EAIMessage;
import com.eactive.eai.common.message.MessageType;
import com.eactive.eai.common.util.DatetimeUtil;
import com.eactive.eai.message.StandardMessage;
import com.eactive.eai.message.manager.StandardMessageManager;
import com.eactive.eai.message.parser.StandardReader;
import com.eactive.eai.message.service.InterfaceMapper;
import com.ext.eai.common.stdmessage.STDMessageKeys;

public abstract class Process {

	public static final String ADAPTER_TIMEOUT             = "ADAPTER_TIMEOUT";
    public static final String ADAPTER_DEFAULT_TIMEOUT     = "ADAPTER_DEFAULT_TIMEOUT";
    public static final String HTTP_ADAPTER_TIMEOUT_PROP   = "HTTP_ADAPTER_TIMEOUT_PROP";
    public static final String SOCKET_ADAPTER_TIMEOUT_PROP = "SOCKET_ADAPTER_TIMEOUT_PROP";
    
    public static final String INTERFACE_TIME_OUT          = "INTERFACE_TIME_OUT";
	
	
    public abstract void callService(EAIMessage message, Properties prop) throws Exception;
    public abstract EAIMessage clientReturn() throws Exception;
	
    protected Object makeStandardMessageData(EAIMessage eaiMessage, String msgType
    		, Object bizObject, String charset) throws Exception{
    	StandardMessage standardMessage = eaiMessage.getStandardMessage();

    	String outboundAdapterGroupName = eaiMessage.getCurrentSvcMsg().getPsvSysItfTp();
    	AdapterGroupVO outboundAdapterGvo = AdapterManager.getInstance().getAdapterGroupVO(outboundAdapterGroupName);
    	standardMessage.setBizData(bizObject, outboundAdapterGvo.getMessageEncode());

		StandardMessageManager.getInstance().getMessageCoordinator().coordinateBeforeStandardMessageSend(standardMessage, msgType, charset);

    	if(MessageType.JSON.equals(msgType)) {
    		return standardMessage.toJson();
    	}
    	else if(MessageType.XML.equals(msgType)) {
    		return standardMessage.toXML();
    	}
    	else if(MessageType.ASC.equals(msgType)) {
    		return standardMessage.toByteArray(charset);
    	}
    	else {
    		throw new Exception("Unsupported msgType - " + msgType);
    	}
    }
    
    protected String getGuidSeq(EAIMessage eaiMessage) {
    	InterfaceMapper mapper = eaiMessage.getMapper();
    	if(mapper == null) return null;
    	StandardMessage standardMessage = eaiMessage.getStandardMessage();
    	return mapper.getGuidSeq(standardMessage);
    }
    
    protected String getGuid(EAIMessage eaiMessage) {
    	InterfaceMapper mapper = eaiMessage.getMapper();
    	if(mapper == null) return null;
    	StandardMessage standardMessage = eaiMessage.getStandardMessage();
    	return mapper.getGuid(standardMessage);
    }
    
    protected String getOrgGuid(EAIMessage eaiMessage) {
    	InterfaceMapper mapper = eaiMessage.getMapper();
    	if(mapper == null) return null;
    	StandardMessage standardMessage = eaiMessage.getStandardMessage();
    	return mapper.getOrgGuid(standardMessage);
    }
    
    protected String getReqSysCode(EAIMessage eaiMessage) {
    	InterfaceMapper mapper = eaiMessage.getMapper();
    	if(mapper == null) return null;
    	StandardMessage standardMessage = eaiMessage.getStandardMessage();
    	return mapper.getReqSysCode(standardMessage);
    }
    
    protected String getTimeout(EAIMessage eaiMessage) {
    	InterfaceMapper mapper = eaiMessage.getMapper();
    	if(mapper == null) return null;
    	StandardMessage standardMessage = eaiMessage.getStandardMessage();
    	return mapper.getTimeout(standardMessage);
    }

	protected void setTimeout(EAIMessage eaiMessage, String timeout) {
		InterfaceMapper mapper = eaiMessage.getMapper();
		StandardMessage standardMessage = eaiMessage.getStandardMessage();
		mapper.setTimeout(standardMessage, timeout);
	}
    
    protected String getRecoverYn(EAIMessage eaiMessage) {
    	InterfaceMapper mapper = eaiMessage.getMapper();
    	if(mapper == null) return null;
    	StandardMessage standardMessage = eaiMessage.getStandardMessage();
    	return mapper.getRecoverYn(standardMessage);
    }
    
    protected String getInstCode(EAIMessage eaiMessage) {
    	InterfaceMapper mapper = eaiMessage.getMapper();
    	if(mapper == null) return null;
    	StandardMessage standardMessage = eaiMessage.getStandardMessage();
    	return mapper.getInstCode(standardMessage);
    }
    
    protected String getOperationEnv(EAIMessage eaiMessage) {
    	InterfaceMapper mapper = eaiMessage.getMapper();
    	if(mapper == null) return null;
    	StandardMessage standardMessage = eaiMessage.getStandardMessage();
    	return mapper.getOperationEnv(standardMessage);
    }
    
    protected String getFirstRequestSysIp(EAIMessage eaiMessage) {
    	InterfaceMapper mapper = eaiMessage.getMapper();
    	if(mapper == null) return null;
    	StandardMessage standardMessage = eaiMessage.getStandardMessage();
    	return mapper.getFirstReqIp(standardMessage);
    }
    
    protected String getServiceId(EAIMessage eaiMessage) {
    	InterfaceMapper mapper = eaiMessage.getMapper();
    	if(mapper == null) return null;
    	StandardMessage standardMessage = eaiMessage.getStandardMessage();
    	return mapper.getServiceId(standardMessage);
    }
    
    protected String getReturnType(EAIMessage eaiMessage) {
    	InterfaceMapper mapper = eaiMessage.getMapper();
    	if(mapper == null) return null;
    	StandardMessage standardMessage = eaiMessage.getStandardMessage();
    	return mapper.getSendRecvDivision(standardMessage);
    }
    
    protected void setReturnType(EAIMessage eaiMessage, String returnType) {
    	InterfaceMapper mapper = eaiMessage.getMapper();
    	StandardMessage standardMessage = eaiMessage.getStandardMessage();
    	mapper.setSendRecvDivision(standardMessage, returnType);    	
    }
    
    protected EAIMessage setResponseForOutbound(EAIMessage eaiMessage) {
    	InterfaceMapper mapper = eaiMessage.getMapper();
    	StandardMessage standardMessage = eaiMessage.getStandardMessage();
    	mapper.setSendRecvDivision(standardMessage, STDMessageKeys.SEND_RECV_CD_RECV);
    	return eaiMessage;
    }
    
    protected EAIMessage setSendTime(EAIMessage eaiMessage) {
    	InterfaceMapper mapper = eaiMessage.getMapper();
    	StandardMessage standardMessage = eaiMessage.getStandardMessage();
    	mapper.setSendTime(standardMessage, DatetimeUtil.getCurrentTimeMillis());
    	return eaiMessage;
    }

	protected EAIMessage setRcvLogInfo(EAIMessage eaiMessage, String msgType, Object message, String charset)
			throws Exception {
		InterfaceMapper mapper = eaiMessage.getMapper();
		StandardMessageManager standardManager = StandardMessageManager.getInstance();
		StandardMessage standardMessage = standardManager.getStandardMessage();
		StandardReader reader = standardManager.getReader(msgType);
		if (reader == null) {
			throw new Exception("StandardReader not found, msgType - " + msgType);
		}
		reader.parse(standardMessage, message);
		standardMessage.setBizDataCharset(charset);
		standardManager.getMessageCoordinator().coordinateAfterParsing(standardMessage, null, new Properties());
		eaiMessage.setStandardMessage(standardMessage);

		mapper.setRecvTime(standardMessage, DatetimeUtil.getCurrentTimeMillis());
		return eaiMessage;
	}
}
