package com.yutou.qqbot.utlis;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;

import java.io.*;

/**
 * 配置和参数
 */
public class ConfigTools {
    public static final String CONFIG = "config.json";
    public static final String DATA = "data.json";
    public static final String SQLITE = "sqlite.json";
    public static final String BiliBili = "bilibili.cookie";
    public static final String QQ = "qq_bot";
    public static final String QQ_NUMBER = "qq_number";
    public static final String QQ_PASSWORD = "qq_password";
    public static final String MODEL = "model";
    public static final String OS = "os";
    public static final String MUSIC_SCAN = "musicScan";
    public static final String SERVICE_LOG = "service.log";
    public static final String QQ_LOG = "qq.log";
    public static final String CHROME = "chrome";
    public static final String FIREFOX = "firefox";
    public static final String FFMPEG = "ffmpeg";
    public static final String SIGN_URL = "sign_url";
    public static final String NAPCAT_URL = "napcat.url";
    public static final String SERVER_URL = "server.url";
    public static final String BAIDU_GPT_VERSION = "baidu.gpt.version";
    public static final String BAIDU_GPT_APPID = "baidu.gpt.appid";
    public static final String BAIDU_GPT_API_KEY = "baidu.gpt.apikey";
    public static final String BAIDU_GPT_ACCESS_KEY = "baidu.gpt.accessKey";
    public static final String BAIDU_GPT_SECRET_KEY = "baidu.gpt.SecretKey";
    public static final String TURNIP_PROPHET_SERVER = "turnip.server";
    public static final String TURNIP_PROPHET_SEND_TMP_GROUP = "turnip.send.tmp.group";


    static {
        try {
            File file = new File(CONFIG);
            if (!file.exists()) {
                file.createNewFile();
            }
            file = new File(DATA);
            if (!file.exists()) {
                file.createNewFile();
            }
            file = null;
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public static Object load(String type, String key) {
        return load(type, key, Object.class, null);
    }

    public static <T> T load(String type, String key, Class<T> t) {
        return load(type, key, t, null);
    }

    public static <T> T load(String type, String key, Class<T> t, T def) {
        File file = new File(type);
        //com.yutou.nas.utils.Log.i(type+"配置文件地址:"+file.getAbsolutePath());
        String src = readFile(file);
        if (src != null) {
            try {
                JSONObject json = JSONObject.parseObject(src, JSONObject.class);
                return json.getObject(key, t);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return def;
    }

    public static String loadIni(File file, String key) {
        try {
            BufferedReader reader = new BufferedReader(new FileReader(file));
            String tmp, str = null;
            while ((tmp = reader.readLine()) != null) {
                if (tmp.startsWith(key + "=")) {
                    str = tmp.split("=")[1];
                    if (StringUtils.isEmpty(str)) {
                        str = null;
                    }
                    break;
                }
            }
            return str;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static boolean save(String type, String key, Object data) {
        File file = new File(type);
        String src = readFile(file);
        if (src == null) {
            src = "{}";
        }
        JSONObject json = JSON.parseObject(src);
        json.put(key, data);
        saveFile(file, json.toJSONString());
        return false;
    }

    public static boolean saveFile(File file, String data) {
        try {
            FileWriter writer = new FileWriter(file);
            writer.write(data);
            writer.flush();
            writer.close();
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }

    public static String readFile(File file) {
        try {
            BufferedReader reader = new BufferedReader(new FileReader(file));
            String tmp;
            StringBuilder str = new StringBuilder();
            while ((tmp = reader.readLine()) != null) {
                str.append(tmp).append("\n");
            }
            reader.close();
            return str.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String getServerUrl() {
        return ConfigTools.load(CONFIG, SERVER_URL, String.class);
    }
}
