package com.eactive.eai.common.routing.rmi;

import com.eactive.eai.adapter.socket2.common.StopWatch;
import com.eactive.eai.common.message.EAIMessage;
import com.eactive.eai.common.message.ServiceMessage;
import com.eactive.eai.common.routing.ElinkESBProcessProxy;
import com.eactive.eai.common.routing.RoutingManager;
import com.eactive.eai.common.routing.RoutingVO;
import com.eactive.eai.common.util.Logger;

import java.util.Properties;

public class RemoteProxyImpl implements RemoteProxy {
	private static Logger logger = Logger.getLogger(Logger.LOGGER_DEFAULT);
//	private String name = this.getClass().getName();
	
	@Override
	public EAIMessage callService(EAIMessage eaiMessage, Properties callProp) {
		EAIMessage  message = null;
		String guidLogPrefix = "RemoteProxyImpl] ";
		try {
//			guidLogPrefix = "RemoteProxyImpl] GUID["+eaiMessage.getKbMsg().getKBHeader().getGuIdNo()+"] UUID["+eaiMessage.getSvcOgNo()+"] ";
			guidLogPrefix = "RemoteProxyImpl] GUID["+eaiMessage.getMapper().getGuid(eaiMessage.getStandardMessage())+"] UUID["+eaiMessage.getSvcOgNo()+"] ";
			StopWatch stopWatch = new StopWatch();
			stopWatch.start();
			
			if(logger.isDebug()) {
				logger.debug(guidLogPrefix + " called service["+eaiMessage.getEAISvcCd()+ "] UUID["+eaiMessage.getSvcOgNo()+"]");
			}
			ServiceMessage  service =   (eaiMessage.getSvcMsgs().get( eaiMessage.getSvcPssSeq() - 1 ));
	        String routingName      =   service.getOutbRtnNm();
	        RoutingVO routingUrl    =   RoutingManager.getInstance().getRoutingVO( routingName );
	        
			message = ElinkESBProcessProxy.callElinkESBProcess( routingUrl.getSyncRoutingPath(), eaiMessage, true, callProp );
			stopWatch.stop();
			if(logger.isDebug()) {
				logger.debug(guidLogPrefix + " called service["+eaiMessage.getEAISvcCd()+ "] time("+stopWatch.getTime()+") ");
			}
			
		}
		catch(Exception e) {
			if(logger.isError() && eaiMessage != null) {
				logger.error(guidLogPrefix+ " called service["+eaiMessage.getEAISvcCd()+
						"] UUID["+eaiMessage.getSvcOgNo()+"] Error : "+e.getMessage(),e);
			}
		}
		return message;
	}

}
