package top.octopusyan.config;

import lombok.Getter;
import org.apache.commons.lang3.StringUtils;

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("香港", "xg.frp.octopusyan.top");
        serverPath.put("北京", "bj.frp.octopusyan.top");
        serverPath.put("上海", "frp.octopusyan.top");

        serverIp.put("香港", "101.32.202.135");
        serverIp.put("北京", "112.125.120.135");
        serverIp.put("上海", "81.68.214.67");

        typePort.put("http", 80);
        typePort.put("https", 80);
        typePort.put("ssh", 22);
        typePort.put("tcp", 0);
        typePort.put("udp", 0);
    }


    @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("http"),
        HTTPS("https"),
        SSH("tcp"),
        TCP("tcp"),
        UDP("udp"),
        ;

        private final String type;

        public String getType() {
            return type;
        }

        ProxyType(String type) {
            this.type = type;
        }
    }

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

    public static int getServerNode(String serverName) {
        for (ProxyServer server : Arrays.asList(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();
    }

    /**
     * 获取服务器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) {
        return typePort.get(type);
    }

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