package com.eactive.eai.common.logger;

import com.eactive.eai.common.message.EAIMessage;
import com.eactive.eai.common.util.Logger;
import com.eactive.eai.common.util.MessageUtil;
import com.eactive.eai.transformer.layout.Layout;
import com.eactive.eai.transformer.message.BytesMessage;
import com.eactive.eai.transformer.message.JSONMessage;
import com.eactive.eai.transformer.message.Message;
import com.eactive.eai.transformer.message.MessageFactory;
import com.eactive.eai.transformer.message.XMLMessage;

public class LogMessageUtil {
	
	static Logger logger = Logger.getLogger(Logger.LOGGER_DEFAULT);
	
	public static String makeMaskData(EAIMessage message, Object bizMsg, Layout l,  int logPssSno, String tgtAdptrMsgPtrnCd
			, String svcTsmtUsgTp, String psvItfTp) throws Exception {
		String bizMsgStr="";
		Message msg = MessageFactory.getFactory().getMessage(l.getName());
		if("XML".equals(l.getLayoutType().getName())) {
	    	if (logger.isInfo()) logger.info("EAILogDAO] XML Message Masking - "+ message.getEAISvcCd()+" - "+ logPssSno);

	    	String xmlString = "";
	        if(bizMsg instanceof byte[]) {
	            xmlString = new String((byte[])bizMsg);
	        } else {
	            xmlString = (String)bizMsg;
	        }

	        String rootItemName = ((XMLMessage)msg).getLayout().getRootItem().getName();
            xmlString = MessageUtil.removeXMLHeader(xmlString);
            xmlString = "<"+rootItemName+">"+xmlString+"</"+rootItemName+">";
            msg.setData(xmlString);
            bizMsgStr  = MessageUtil.removeXMLRootItem(msg.toLogString(), rootItemName);
	    }
	    else if("JSON".equals(l.getLayoutType().getName())) {
	    	if (logger.isInfo()) logger.info("EAILogDAO] JSON Message Masking - "+ message.getEAISvcCd()+" - "+ logPssSno);
	    	String jsonString = "";

        	jsonString = (String)bizMsg;

//	        String rootItemName = ((JSONMessage)msg).getLayout().getRootItem().getName();
//	    	jsonString = MessageUtil.addJsonRootItem(jsonString, rootItemName);
//	        msg.setData(jsonString);
//	        bizMsgStr  = MessageUtil.removeJsonRootItem(msg.toLogString(), rootItemName);;
	        
	        // JSON Tuning
	    	msg.setData(jsonString);
	        bizMsgStr  = msg.toLogString();
	    }
//	    else if("UJSON".equals(l.getLayoutType().getName())) {
//	    	if (logger.isInfo()) logger.info("EAILogDAO] UJSON Message Masking - "+ message.getEAISvcCd()+" - "+ logPssSno);
//
//	    	String jsonString = "";
//
//	        if(bizMsg instanceof byte[]) {
//	        	jsonString = new String((byte[])bizMsg, UJSONMessage.encode);
//	        } else {
//	        	jsonString = (String)bizMsg;
//	        }
//
//	        String rootItemName = ((UJSONMessage)msg).getLayout().getRootItem().getName();
//        	 jsonString = MessageUtil.addJsonRootItem(jsonString, rootItemName);
//             msg.setData(jsonString);
//             bizMsgStr  = MessageUtil.removeJsonRootItem(msg.toLogString(), rootItemName);;
//	    }
	    else if("EBCDIC".equals(l.getLayoutType().getName())) {
	    	if (logger.isInfo()) logger.info("EAILogDAO] EBCDIC Message Masking - "+ message.getEAISvcCd()+" - "+ logPssSno);

	    	byte[] orgBytes = (byte[])bizMsg;
	    	byte[] msgBytes = null;
	    	byte[] header = null;

    		msg.setData(bizMsg);
            bizMsgStr  = ((BytesMessage)msg).toLogString(true);
	    }
	    else if( "BYTES".equals(l.getLayoutType().getName()) ) {
	    	if (logger.isInfo()) logger.info("EAILogDAO] BYTES Message Masking - "+ logPssSno);
	    	byte[] orgBytes = (byte[])bizMsg;
	    	byte[] msgBytes = null;
	    	byte[] header = null;

	    	if (logger.isInfo()) logger.info("EAILogDAO] BYTES Standard Header Type = " + tgtAdptrMsgPtrnCd);

        	if (logger.isInfo()) logger.info("EAILogDAO] "+l.getLayoutType().getName()+" Message Masking - "+ message.getEAISvcCd()+" - "+ logPssSno);
        	msg.setData(bizMsg);
			bizMsgStr  = msg.toLogString();
	    }
	    else {
	        msg.setData(bizMsg);
	        bizMsgStr  = msg.toLogString();
	    }
		return bizMsgStr;
	}
}
