package com.eactive.eai.common.util;

import com.eactive.eai.common.b2bextractor.B2BExtractManager;
import com.eactive.eai.common.b2bextractor.B2BExtractVO;
import com.eactive.eai.common.exception.ExceptionUtil;
import com.eactive.eai.flowcontrol.action.RouteAction;
import com.eactive.eai.flowcontrol.action.RouteActionFactory;

// B2BRouterFCProcess 에서 기관코드/업무데이터을 추출
public final class B2BExtractor {
	private static Logger logger = Logger.getLogger(Logger.LOGGER_DEFAULT);

	private B2BExtractor() {

	}

	public static String[] getB2BRouteKeyValues(String eaiSvcCd, int svcPssSeq, Object message, String charset) throws Exception {
		String[] keys = null;
		try {
			String actionName = "";
			// example "com.eactive.eai.flowcontrol.action.SGWRouteAction";
			B2BExtractManager manager = B2BExtractManager.getInstance();
			B2BExtractVO vo = manager.getB2BExtract(eaiSvcCd + svcPssSeq);

			if (vo == null) {
				return null;
			}

			actionName = vo.getRouteActionName();

			if (actionName != null && actionName.trim().length() > 0) {
				RouteActionFactory factory = new RouteActionFactory();
				RouteAction action = factory.createAction(actionName);
				if (message instanceof byte[]) {
					keys = action.getRouteKeys((byte[]) message);
				} 
				else if (message instanceof String) {
					keys = action.getRouteKeys(((String) message).getBytes(charset));
				}
				else {
					throw new Exception("Invalid message EAISVCCD [" + eaiSvcCd + "] message[" + message + "]");
				}
			}
		} catch (Exception e) {
			logger.error("getB2BRouteKeyValues error", e);
			// throw new Exception("RECEAICBX001");
			// B2B Service Routing 키정보 추출 중 오류가 발생
			throw new Exception(ExceptionUtil.getErrorCode(e, "RECEAICBX001"));
		}
		return keys;
	}

	public static String[] getB2BAdapterRouteKeyValues(String eaiSvcCd, int svcPssSeq, Object message, String charset)
			throws Exception {
		String[] keys = null;
		try {
			String actionName = "";
			// example "com.eactive.eai.flowcontrol.action.SGWAdapterRouteAction";
			B2BExtractManager manager = B2BExtractManager.getInstance();
			B2BExtractVO vo = manager.getB2BExtract(eaiSvcCd + svcPssSeq);

			if (vo == null) {
				return null;
			}
			actionName = vo.getAdapterActionName();

			if (actionName != null && actionName.trim().length() > 0) {
				RouteActionFactory factory = new RouteActionFactory();
				RouteAction action = factory.createAction(actionName);
				if (message instanceof byte[]) {
					keys = action.getRouteKeys((byte[]) message);
				} 
				else if (message instanceof String) {
					keys = action.getRouteKeys(((String) message).getBytes(charset));
				}
				else {
					throw new Exception("Invalid message EAISVCCD [" + eaiSvcCd + "] message[" + message + "]");
				}
			}
		} catch (Exception e) {
			logger.error("getB2BAdapterRouteKeyValues error", e);
			// throw new Exception("RECEAICBX001");
			// B2B Adapter Routing 키정보 추출 중 오류가 발생
			throw new Exception(ExceptionUtil.getErrorCode(e, "RECEAICBX002"));
		}
		return keys;
	}
}