package com.eactive.eai.inbound.action;

import com.eactive.eai.adapter.AdapterGroupVO;
import com.eactive.eai.adapter.AdapterManager;
import com.eactive.eai.adapter.Keys;
import com.eactive.eai.adapter.http.dynamic.HttpAdapterServiceKey;
import com.eactive.eai.adapter.http.dynamic.impl.HttpAdapterServiceRest;
import com.eactive.eai.common.exception.ExceptionUtil;
import com.eactive.eai.common.util.MessageKeyExtractor;
import com.eactive.eai.inbound.action.ActionException;
import com.eactive.eai.inbound.action.RequestActionSupport;
import org.apache.commons.lang3.StringUtils;

public class RestAdapterMethodUrlRequestAction extends RequestActionSupport {

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

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

        if (StringUtils.equals(adptGrpVO.getType(), Keys.TYPE_HTTP_CUSTOM)
                || StringUtils.equals(adptGrpVO.getType(), Keys.TYPE_REST)) {
            try {
                String requestMethod = prop
                        .getProperty(HttpAdapterServiceRest.PROPERTIES_NAME_HTTP_REQUEST_METHOD);

                // /api/v1/public/getUserInfo.svc
                String inboundUri = prop.getProperty(HttpAdapterServiceKey.INBOUND_EXTURI);
                String apiPath = prop.getProperty(HttpAdapterServiceKey.API_PATH);

                // /getUserInfo.svc
                String apiUri = org.apache.commons.lang.StringUtils.removeStart(inboundUri, apiPath);
                // _TST_IN_RST_SyS|POST:getUserInfo.svc
                String apiKey = adptGrpName + ":" + requestMethod + "|" + StringUtils.removeStart(apiUri, "/");

                return new String[] { apiKey };
            } catch (Exception e) {
                throw new ActionException(ExceptionUtil.getErrorCode(e, "RECEAIIRA001"));
            }
        }

        if (message instanceof byte[]) {
            try {
                key = MessageKeyExtractor.getMessageKeyValueForRest(this.adapterGroupName, message, prop, "url", true);
            } catch (Exception e) {
                throw new ActionException(ExceptionUtil.getErrorCode(e, "RECEAIIRA001"));
            }
            if (logger.isDebug())
                logger.debug(adapterName + "] received data >> [" + new String((byte[]) message) + "]");
        } else {
            try {
                key = MessageKeyExtractor.getMessageKeyValueForRest(this.adapterGroupName, message, prop, "url", true);
            } catch (Exception e) {
                throw new ActionException(ExceptionUtil.getErrorCode(e, "RECEAIIRA001"));
            }
            if (logger.isDebug())
                logger.debug(adapterName + "] received data >> [" + message + "]");
        }

        for (int i = 0; i < key.length; i++) {
            key[i] = adptGrpName.trim() + key[i];
        }

        return key;
    }

}
