package com.eactive.eai.message.test;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.eactive.eai.message.StandardMessage;
import com.eactive.eai.message.manager.StandardMessageManager;
import com.eactive.eai.message.service.InterfaceMapper;
import com.eactive.eai.message.service.InterfaceMessage;

public class InterfaceTest {
	static Logger logger = LoggerFactory.getLogger(InterfaceTest.class);

	public InterfaceTest() {
	}

	public static void main(String[] args) throws Exception {
//		System.setProperty(StandardMessageManager.STANDARD_MESSAGE_CONFIG, "./resources/standard-message-config-test.properties");
		System.setProperty(StandardMessageManager.STANDARD_MESSAGE_CONFIG, "./resources/standard-message-config.properties");
		testInterfaceWithManager();		
	}
	
	public static void testInterfaceWithManager() {
		StandardMessageManager manager = StandardMessageManager.getInstance();
		try {
			manager.init();
		}
		catch(Exception ex) {
			ex.printStackTrace();
			return;
		}
		
		StandardMessage standardMessage = manager.getStandardMessage();
		InterfaceMapper mapper = manager.getMapper();
		
		String serviceCode = mapper.getEaiSvcCode(standardMessage);
		
		// get Interface message with serviceCode
		InterfaceMessage interfaceMessage = new InterfaceMessage();
		
		interfaceMessage.setStandardMessage(standardMessage);
		interfaceMessage.setMapper(mapper);
		
		logger.debug(">> Init.");
		logger.debug("getServiceId = {}",interfaceMessage.getMapper().getServiceId(interfaceMessage.getStandardMessage()));
		logger.debug("getGuid = {}",interfaceMessage.getMapper().getGuid(interfaceMessage.getStandardMessage()));
		logger.debug("getReturnType = {}",interfaceMessage.getMapper().getSendRecvDivision(interfaceMessage.getStandardMessage()));
		logger.debug("getReturnType = {}",interfaceMessage.getMapper().getSendRecvDivision(interfaceMessage.getStandardMessage()));
		
		logger.debug("getEaiSvcCode = {}", interfaceMessage.getMapper().getEaiSvcCode(standardMessage));
		logger.debug("standardMessage = {}",interfaceMessage.getStandardMessage().toPrettyJson());
		
		String changed = "ELKCHANGED";
		logger.debug("\n>> Change : setServiceId = {}", changed);
		interfaceMessage.getMapper().setServiceId(interfaceMessage.getStandardMessage(), changed);
		interfaceMessage.getMapper().setErrorCode(interfaceMessage.getStandardMessage(), "ERR01");
		interfaceMessage.getMapper().setErrorMsg(interfaceMessage.getStandardMessage(), "내부처리 중 에러");
		interfaceMessage.getMapper().setResponseType(interfaceMessage.getStandardMessage(), "E");
		
		logger.debug("getServiceId = {}",mapper.getServiceId(interfaceMessage.getStandardMessage()));
		logger.debug("getEaiSvcCode = {}", interfaceMessage.getMapper().getEaiSvcCode(standardMessage));
		
		try {
			interfaceMessage.getStandardMessage().setBizData("{\"response\":\"response message is here!\"}", "utf-8");
		} catch (Exception e) {
			logger.error("bizdata set error", e);
		}
		logger.debug("toPrettyJson = {}",interfaceMessage.getStandardMessage().toPrettyJson());
		
		try {
			interfaceMessage.getStandardMessage().setBizData("<response>response message is here!</response>", "utf-8");
		} catch (Exception e) {
			logger.error("bizdata set error", e);
		}
		logger.debug("toXML = {}",interfaceMessage.getStandardMessage().toPrettyXML());
		
	}

}
