package com.example.demo.common;

import com.alibaba.fastjson.JSON;
import com.example.demo.module.smartbowsdk.config.GameChannel;
import pers.jc.util.JCFileTool;

import java.io.File;

/**
 * 全局配置
 */
public class GlobalConfig {
    public static boolean isLinux;
    public static String rootPath;
    public static ServerConfig serverConfig;

    public static void init()
    {
        try {
            isLinux = System.getProperties().getProperty("os.name").startsWith("Linux");
            rootPath = new File("").getCanonicalPath();
            loadServerConfig();
            GameChannel.loadConfigList();
            LocalTestConfig.loadConfig();
        } catch (Exception e) {
            int[] error = {};
            error[0] = 0;
        }
    }

    private static void loadServerConfig() throws Exception {
        String path = rootPath + File.separator + "res" + File.separator + "ServerConfig.json";
        String text = new JCFileTool().readStr(new File(path));
        serverConfig = JSON.parseObject(text, ServerConfig.class);
    }

    public static boolean checkServerIndexValid(int serverIndex) {
        return serverIndex >= 0 && serverIndex <= 1;
    }

    public static String getBusinessWS(int serverIndex) {
        if (GlobalConfig.isLinux) {
            if (serverIndex == 0) {
                return "ws://118.195.187.121/SmartBowBusinessServerSK2";
            }
            if (serverIndex == 1) {
                return "wss://54.165.56.200/SmartBowBusinessServerSK2";
            }
        }
        return "ws://" + LocalTestConfig.instance.getLocalIp() + ":11433/SmartBowBusinessServerSK2";
    }

    public static String getGamePkWS() {
        int serverIndex = serverConfig.getServerIndex();
        if (GlobalConfig.isLinux) {
            if (serverIndex == 0) {
                return "ws://118.195.187.121/SmartBowGameServer";
            }
            if (serverIndex == 1) {
                return "wss://54.165.56.200/SmartBowGameServer";
            }
        } return "ws://" + LocalTestConfig.instance.getLocalIp() + ":11811/SmartBowGameServer";
    }
}
