package com.eactive.eai.custom.message;

import org.apache.commons.lang3.StringUtils;

import com.eactive.eai.message.StandardItem;
import com.eactive.eai.message.StandardMessage;
import com.eactive.eai.message.filter.MessageFilter;
import com.eactive.eai.message.manager.StandardMessageManager;
import com.eactive.eai.message.service.InterfaceMapper;
import com.ext.eai.common.stdmessage.STDMessageKeys;

@SuppressWarnings("serial")
public class FlatMessageFilterSBI implements MessageFilter {

	@Override
	public StandardMessage doFilter(StandardMessage message) {
		// total 메시지 길이 설정
		int totalSize = 0;
		try {
			totalSize = message.getBytesDataLength();
			message.setLlData(String.valueOf(totalSize));
		} catch (Exception e) {
		}

		// whlMesgLen=전체길이 - 채널헤더길이(24) - ZZ(2)
		try {
			StandardItem whlMesgLenItem = message.findItem("STD_HEADER.whlMesgLen");
			if (whlMesgLenItem != null) {
				int whlMesgLen = totalSize - 24 - 2;
				whlMesgLenItem.setValue(String.valueOf(whlMesgLen));
			}

			// stndHdrLen=전체길이 - 채널헤더길이(24) - bizData길이 - ZZ(2)
			StandardItem stndHdrLenItem = message.findItem("STD_HEADER.stndHdrLen");
			if (stndHdrLenItem != null) {
				int stndHdrLen = totalSize - 24 - message.getBizDataBytes().length - 2;
				stndHdrLenItem.setValue(String.valueOf(stndHdrLen));
			}

		} catch (Exception e) {
		}

		// STD_HEADER.rsltCd 설정
		StandardMessageManager standardManager = StandardMessageManager.getInstance();
		InterfaceMapper mapper = standardManager.getMapper();
		if (StringUtils.equals(mapper.getSendRecvDivision(message), "R")) {
			StandardItem rsltCdItem = message.findItem("STD_HEADER.rsltCd");
			if (StringUtils.equals(mapper.getResponseType(message), STDMessageKeys.RESPONSE_TYPE_CODE_N)) {
				rsltCdItem.setValue("00"); // STD_MSG_RSLT_CD_SUCCESS
			} else {
				rsltCdItem.setValue("99"); // STD_MSG_RSLT_CD_ERROR_UNKNOWN
			}
		}

		return message;
	}
}
