package com.yutou.common.utils;


import com.alibaba.fastjson2.JSONObject;
import org.springframework.util.StringUtils;

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 NAPCAT_URL = "napcat.url";
    public static final String QQ = "qq_bot";

    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){
            Log.e(e);
        }

    }
    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);
                return json.getObject(key, t);
            } catch (Exception e) {
            }
        }

        return def;
    }
    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=JSONObject.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) {
           Log.e(e);
            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);
            }
            reader.close();
            return str.toString();
        } catch (Exception e) {
            Log.i("error = "+file.getAbsolutePath());
            Log.e(e);
        }
        return null;
    }

    public static String getUserAgent() {
        String ua=load(CONFIG,"userAgent",String.class);
        if(!StringUtils.hasText(ua)){
            ua="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36";
        }
        return ua;
    }
    public static boolean saveUserAgent(String ua) {
        return save(CONFIG,"userAgent",ua);

    }
}
