package top.octopusyan.config;

import lombok.Getter;
import org.apache.commons.lang3.StringUtils;
import top.octopusyan.model.ProxySetupModel;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * @author : octopus yan
 * @email : octopus_yan@foxmail.com
 * @description :  代理设置
 * @create : 2022-4-5 18:14
 */
public class ProxyConfig {
    private static final Map<String, String> serverPath = new HashMap<>();
    private static final Map<String, String> serverIp = new HashMap<>();
    private static final Map<String, Integer> typePort = new HashMap<>();

    static {
        serverPath.put("香港", "example.com");
        serverPath.put("北京", "example.com");
        serverPath.put("上海", "example.com");

        serverIp.put("香港", "127.0.0.1");
        serverIp.put("北京", "127.0.0.1");
        serverIp.put("上海", "127.0.0.1");

        typePort.put("http", 80);
        typePort.put("https", 443);
        typePort.put("tcp", 25565);
        typePort.put("udp", 53);
    }


    @Getter
    public enum ProxyServer {
        xg("香港", 1),
        bj("北京", 2),
        sh("上海", 3);
        private final String serverName;
        private final int value;

        ProxyServer(String serverName, int value) {
            this.serverName = serverName;
            this.value = value;
        }

        public static ProxyServer valueOf(int node) {
            for (ProxyServer value : values()) {
                if (value.value == node) return value;
            }
            return null;
        }
    }

    public enum ProxyType {
        HTTP,
        HTTPS,
        TCP,
        UDP,
        STCP,
        XTCP,
        ;
    }

    public static String getServerPath(String serverName) {
        return serverPath.get(serverName);
    }

    public static int getServerNode(String serverName) {
        for (ProxyServer server : ProxyServer.values()) {
            if (server.serverName.equals(serverName)) return server.value;
        }

        return 3;
    }

    /**
     * 获取服务名称
     *
     * @param node 服务器标签
     */
    public static String getServerName(int node) {
        ProxyServer proxyServer = ProxyServer.valueOf(node);
        assert proxyServer != null;
        return proxyServer.getServerName();
    }

    public static boolean isHttp(ProxySetupModel model) {
        return ProxyConfig.getTypeIndex(model.getProxyType()) < 2;
    }

    public static boolean isP2p(ProxySetupModel model) {
        return ProxyConfig.getTypeIndex(model.getProxyType()) > 3;
    }

    /**
     * 获取服务器IP地址
     *
     * @param serverName 服务器名称
     */
    public static String getServerIP(String serverName) {
        return serverIp.get(serverName);
    }

    /**
     * 获取服务器IP地址
     *
     * @param node 服务器标签
     */
    public static String getServerIP(int node) {
        return serverIp.get(getServerName(node));
    }

    /**
     * 获取代理类型默认端口
     *
     * @param type 类型名称
     */
    public static Integer getTypePort(String type) {
        Integer port = typePort.get(type);
        return port == null ? 0 : port;
    }

    public static Integer getTypeIndex(String type) {
        return Arrays.asList(ProxyType.values()).indexOf(ProxyType.valueOf(StringUtils.upperCase(type)));
    }
}
