package com.ch.jedge.utils;

import com.changhong.jedge.JMgbusClient;
import com.changhong.qlib.QData;
import com.changhong.qlib.intf.QIData;
import com.changhong.qlib.util.StringUtils;
import com.changhong.qlib.util.file.FileUtils;

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

import static com.ch.jedge.jbot.JEJBotLLMConst.sval_milvus_cfg_path;

public class JEVdbConfigUtil {
    protected static final Map<String, QIData> mConfigs= new HashMap<>();

    public static void loadConfigs() {
        FileUtils.listDirFiles(sval_milvus_cfg_path, file -> {
            if(!file.getName().endsWith(".json"))
                return null;
            QIData cfgData = QData.fromFile(file);
            if(cfgData.isEmpty()) return null;
            synchronized (mConfigs) {
                String fn = FileUtils.getFilenameWithoutExt(file.getName());
                mConfigs.put(fn, cfgData);
            }
            return null;
        });
    }

    public static void releaseConfigs() {
        synchronized (mConfigs) {
            mConfigs.clear();
        }
    }

    public static void setConfigProp(String cfgKey, String prop, Object val) {
        synchronized (mConfigs) {
            if(!mConfigs.containsKey(cfgKey))
                return;
            QIData cfgData=mConfigs.get(cfgKey);
            cfgData.put(prop, val);
        }
    }

    public static Object getConfigProp(String cfgKey, String prop, Object defVal) {
        synchronized (mConfigs) {
            if(!mConfigs.containsKey(cfgKey))
                return defVal;
            QIData cfgData=mConfigs.get(cfgKey);
            return cfgData.containsKey(prop) ?
                    cfgData.get(prop) : defVal;
        }
    }

    public static boolean getConfigBoolean(String cfgKey, String prop, boolean defVal) {
        synchronized (mConfigs) {
            if(!mConfigs.containsKey(cfgKey))
                return defVal;
            QIData cfgData=mConfigs.get(cfgKey);
            Object obj = cfgData.containsKey(prop) ?
                    cfgData.get(prop) : defVal;
            return obj instanceof Boolean? (boolean) obj: defVal;
        }
    }

    public static int getConfigInteger(String cfgKey, String prop, int defVal) {
        synchronized (mConfigs) {
            if(!mConfigs.containsKey(cfgKey))
                return defVal;
            QIData cfgData=mConfigs.get(cfgKey);
            Object obj = cfgData.containsKey(prop) ?
                    cfgData.get(prop) : defVal;
            return obj instanceof Integer? (int) obj: defVal;
        }
    }

    public static String getConfigObjSimpleFmtString(String cfgKey, String prop, String defVal) {
        synchronized (mConfigs) {
            if(!mConfigs.containsKey(cfgKey))
                return null;
            QIData cfgData=mConfigs.get(cfgKey);
            return cfgData.getObjSimpleFmtString(prop, defVal);
        }
    }

    public static QIData getConfig(String cfgKey) {
        synchronized (mConfigs) {
            return mConfigs.get(cfgKey);
        }
    }

    public static void saveConfig(String cfgKey) {
        synchronized (mConfigs) {
            QIData cfg = mConfigs.get(cfgKey);
            if(cfg!=null) {
                cfg.saveToFile(FileUtils.contactPath(sval_milvus_cfg_path, cfgKey+".json"));
            }
        }
    }

    public static boolean setupInitConfigs(JMgbusClient mgClient, QIData param, String defaultClientName) {

        String host = param.getString("host");
        int port = param.getInteger("port");
        String jam = param.getString("jam");

        if(StringUtils.isNotValidStr(host))
            host = JEVdbConfigUtil.getConfigObjSimpleFmtString(defaultClientName, "jedge.host", null);
        if(StringUtils.isNotValidStr(host)) host = "127.0.0.1";
        if(StringUtils.isNotValidStr(jam))
            jam = JEVdbConfigUtil.getConfigObjSimpleFmtString(defaultClientName, "jedge.name", defaultClientName);
        if(port<0) {
            Object oport = JEVdbConfigUtil.getConfigProp(defaultClientName, "jedge.port", 8877);
            if (!(oport instanceof Integer))
                port = 8877;
            else
                port = (int) oport;
        }
        //设置默认启动接入的站点

        mgClient.setDefaultHost(host, port);
        mgClient.resetClientName(jam);

        //后台运行 -p
        return !param.getBoolean("p", false);
    }
}