package com.docus.server.message.config;

import com.docus.core.util.Func;
import com.docus.server.message.enums.DataFormatEnum;
import com.docus.server.message.util.TableJsonRead;
import lombok.Data;

import java.util.List;
import java.util.Objects;

/**
 * @author wyb
 */
public class MessageForwardConfig {
    private final static String CONFIG_FILE_PATH = "data-config";
    private final static String CONFIG_FILE_NAME = "message-forward-config.json";

    public static MessageForward wsMessageForward(String method) {
        Objects.requireNonNull(method);
        TableJsonRead jsonReader = new TableJsonRead();
        String wsMessageForwardConfigJson = jsonReader.ReadContent(CONFIG_FILE_PATH, CONFIG_FILE_NAME);
        if (wsMessageForwardConfigJson == null) {
            return null;
        }
        List<MessageForward> messageForwardList = Func.parseJsonArray(wsMessageForwardConfigJson, MessageForward.class);
        if (Func.isEmpty(messageForwardList)) {
            return null;
        }
        for (MessageForward messageForward : messageForwardList) {
            if (method.equals(messageForward.getMethod())) {
                return messageForward;
            }
        }
        return null;
    }

    @Data
    public static class MessageForward {
        private String method;
        private List<String> urls;
        private String errorResult;
        /**
         * @see DataFormatEnum#getValue()
         */
        private Integer messageDataType;
    }
}
