package com.eactive.eai.inbound.action;

import com.eactive.eai.adapter.AdapterGroupVO;
import com.eactive.eai.adapter.AdapterManager;
import com.eactive.eai.adapter.http.dynamic.HttpAdapterServiceKey;
import com.eactive.eai.adapter.http.dynamic.impl.HttpAdapterServiceRest;
import com.eactive.eai.common.messagekey.MessageKeyFldVO;
import com.eactive.eai.common.messagekey.MessageKeyGroupVO;
import com.eactive.eai.common.messagekey.MessageKeyManager;
import com.eactive.eai.common.messagekey.MessageKeyVO;
import com.eactive.eai.inbound.action.ActionException;
import com.eactive.eai.inbound.action.RequestActionSupport;

import java.util.List;
import java.util.Properties;

public class RestAdapterMethodHeaderRequestAction extends RequestActionSupport {
    public static final String STEMSG_METHOD_DELIMITER = ":";

    public static final String STEMSG_SERVICE_HEADER_DELIMITER = "^";

    public static final String STEMSG_HEADER_SEPARATOR = ";";

    /**
     * 1. 기능 : Inbound Adapter를 통해 전달받은 비표준메시지에서 표준메시지를 생성하기 위한 키값을 추출
     * 2. 처리 개요 :
     *     - MessageKeyExtractor를 통해 Adapter별 추출 Rule에 따라 메시지를 구분할 수 있는 키값을 추출한다.
     * 3. 주의사항
     *
     * @param message Object 타입의 요청메시지
     * @return String 비표준메시지의 메시지키값
     * @exception
     **/
    public String[] perform(Object message) throws ActionException {

        AdapterManager adapterManager = AdapterManager.getInstance();
        AdapterGroupVO adptGrpVO = adapterManager.getAdapterGroupVO(adapterGroupName);
        String adptGrpName = adptGrpVO.getName();

        String requestMethod = prop.getProperty(HttpAdapterServiceRest.PROPERTIES_NAME_HTTP_REQUEST_METHOD);

        String keyCode = adptGrpName + "I";
        MessageKeyManager manager = MessageKeyManager.getInstance();
        MessageKeyVO msgKey = manager.getMessageKey(keyCode);
        List<MessageKeyGroupVO> alMsgGroups = msgKey.getAlMsgGroups();
        int groupSize = alMsgGroups.size();

        Properties inboundheader = (Properties) prop.get(HttpAdapterServiceKey.INBOUND_HEADER);
        String[] keys = new String[groupSize];

        if (logger.isInfo()) {
            logger.info("MessageKeyExtractor] CURRENT RULE [{}]", keyCode);
        }

        String baseMessageKey = adptGrpName + STEMSG_METHOD_DELIMITER + requestMethod + STEMSG_SERVICE_HEADER_DELIMITER;

        for (int g = 0; g < groupSize; g++) {
            MessageKeyGroupVO groupVo = msgKey.getMessageKeyGroup(g);
            int fldSize = groupVo.getMessageKeyFldLength();
            String[] keyBuffer = new String[fldSize];

            for (int i = 0; i < fldSize; i++) {
                MessageKeyFldVO keyFld = groupVo.getMessageKeyFld(i);
                String tmpKey = inboundheader.getProperty(keyFld.getBwkColNm().toLowerCase(),"");
                keyBuffer[i] = tmpKey;
            }
            String currentKey = baseMessageKey + String.join(STEMSG_HEADER_SEPARATOR, keyBuffer);
            keys[g] = currentKey;
            if (logger.isInfo()) {
                logger.info("MessageKeyExtractor] CURRENT RULE[{}] KEY [{}]", keyCode,currentKey);
            }
        }

        return keys;
    }

}
