package com.zi.mwms.solution.esb;

import org.springframework.amqp.core.Message;
import org.apache.logging.log4j.ThreadContext;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;

import com.cim.idm.framework.event.AbstractMessageEvent;
import com.cim.idm.framework.event.support.DocumentMessageUtil;
import com.cim.idm.framework.fos.idmflow.BpelExRepository;
import com.cim.idm.framework.fos.idmflow.BpelExecutorService;
import com.cim.idm.framework.util.xml.JdomUtils;
import com.cim.idm.wmsextend.generic.util.CommonUtil;
import com.tibco.tibrv.TibrvMsg;

public  class MessageEvent extends AbstractMessageEvent implements InitializingBean  {
	
	private static Logger 	log = LoggerFactory.getLogger(MessageEvent.class);
	private String dataField_TAG = "Message";
	private String rootBpelName	= "dispatching.bpel";
	

	@Override
	public void afterPropertiesSet() throws Exception {
		BpelExRepository bpelRepo = applicationContext.getBean(BpelExRepository.class);
		rootBpelName = bpelRepo.getRootBpelName();
	}

	public MessageEvent()
	{
	}

	public void onReceiveMessage(String beanName, Object data)
	{
		Document document = null;
		if (data instanceof TibrvMsg) {
			document = DocumentMessageUtil.getDocumentFromTibrvMsg((TibrvMsg)data, dataField_TAG);
		}

		if(data instanceof Message){
			document = DocumentMessageUtil.getDocumentFromAmqpMessage((Message)data);
		}

		if(document == null)
		{
			log.info("ERROR Message - Message Format ERROR");
			log.info(data.toString());
			log.info("ERROR Message - Message Format ERROR");
			return;
		}
		try {
			ThreadContext.put("WMS.MSGNAME", JdomUtils.getNodeText(document, "//Message/Header/MESSAGENAME"));
		} catch (Exception e2) {
			e2.printStackTrace();
		}
		try {
			ThreadContext.put("WMS.TRXID", JdomUtils.getNodeText(document, "//Message/Header/TRANSACTIONID"));
		} catch (Exception e1) {
			e1.printStackTrace();
		}

		log.info("["+ beanName + "] execute");

		Element headerElement = null;
		try {
			String nodePath = "//Message/Header";

			headerElement = JdomUtils.getNode(document, nodePath);
			headerElement = CommonUtil.setEventComment(document);
			Element listenerElement = headerElement.getChild("listener");
			if( listenerElement== null )
				JdomUtils.addElement(headerElement, "listener", beanName);
			else
				listenerElement.setText(beanName);

		} catch (Exception e) {
			e.printStackTrace();
		}

		log.info(JdomUtils.toString(document));

		Object[] arguments = new Object[]{document};

		// One Thread
		//WorkflowServiceProxy.getBpelExecuter().executeProcess(arguments, false, null);

		//Multi Thread
		execute(arguments);
		
//		executeService.execute(document);

	}

	public String toCompactString(Document document)
	{
		XMLOutputter out = new XMLOutputter();
		Format format = Format.getCompactFormat();
		out.setFormat(format);

		return out.outputString(document.getRootElement());
	}

	public void execute(Object[] data)
	{
		try
		{
			((BpelExecutorService) applicationContext.getBean(BpelExecutorService.class)).executeProcess(data, rootBpelName);
		} 
		catch (Exception e)
		{
			log.error(e.toString(), e);
		}		
	}
}