package com.ch.jedge.jbot;

import com.ch.jedge.utils.JEVdbConfigUtil;
import com.changhong.qlib.QData;
import com.changhong.qlib.intf.QIData;
import com.changhong.qlib.util.cmdline.ArgUtils;
import com.changhong.qlib.util.cmdline.CmdUtils;
import com.changhong.qlib.util.sync.SystemUtils;

import static com.ch.jedge.jbot.JEJBotLLMConst.sval_default_jbot_jedge;
public class JBotJava  extends QData implements CmdUtils.CmdController {

    private static final JBotJava instance = new JBotJava(sval_default_jbot_jedge);

    public JBotJava(String botName) {
        setName(botName);
    }

    @Override
    public String getControllerName() {
        return getName();
    }

    @Override
    public void setControllerName(String s) {
        setName(s);
    }


    public static void jbot_main(String[] args) {

        JEVdbConfigUtil.loadConfigs();

        JedgeLlmBotClient jbot = JedgeLlmBotClient.getInstance();

        QIData param = ArgUtils.handleArgVList(args);
        boolean runBack = JEVdbConfigUtil.setupInitConfigs(jbot, param, sval_default_jbot_jedge);

        if(!jbot.start()) {
            jbot.errLog("Fail to start ap app : " + jbot.getName());
            return;
        }

        while(!jbot.isActive()) {
            //等待接入成功
            jbot.warnLog("Wait for apApp to connect to mgbus : " + jbot.getName());
            SystemUtils.tryWait(2000);
        }
//
//        try {
//            botService = (JedgeBotGptService) jbot.findService(sval_default_gpt_service);
//        } catch (Exception ignored) {}

        CmdUtils.run(instance, runBack, param);

        jbot.shutdown();

        JEVdbConfigUtil.releaseConfigs();
    }

}
