package com.yutou.qqbot.models.Commands;

import com.yutou.napcat.QQDatabase;
import com.yutou.napcat.handle.Text;
import com.yutou.qqbot.Annotations.UseModel;
import com.yutou.qqbot.QQBotManager;
import com.yutou.qqbot.data.baidu.ResponseMessage;
import com.yutou.qqbot.models.Model;
import com.yutou.qqbot.utlis.BaiduGPTManager;
import com.yutou.napcat.event.MessageEvent;
import com.yutou.qqbot.utlis.ConfigTools;
import com.yutou.qqbot.utlis.StringUtils;
import lombok.val;
import org.apache.catalina.valves.JsonErrorReportValve;

import java.util.ArrayList;

@UseModel
public class BaiduGPT extends Model {

    @Override
    public boolean isUserPublic() {
        return true;
    }

    @Override
    public String[] getUsePowers() {
        return new String[]{
                QQGroupCommands.GPT,
                QQGroupCommands.GPT_CLEAR
        };
    }

    @Override
    public String getModelName() {
        return "百度文言一心GPT";
    }

    @Override
    public void onMessage(Long qq, MessageEvent event, boolean isGroup) {
        super.onMessage(qq, event, isGroup);
        String version = ConfigTools.load(ConfigTools.CONFIG, ConfigTools.BAIDU_GPT_VERSION, String.class);
        if (StringUtils.isEmpty(version)) {
            version = "3.5";
            BaiduGPTManager.getManager().setModelFor35();
        }
        if ("3.5".equals(version)) {
            BaiduGPTManager.getManager().setModelFor35();
        } else if ("4.0".equals(version)) {
            BaiduGPTManager.getManager().setModelFor40();
        }
        if (event.getTextMessage().equals(QQGroupCommands.GPT_CLEAR)) {
            BaiduGPTManager.getManager().clear();
            QQBotManager.getInstance().sendMessage(event.isUser(), qq, new Text("已经失忆捏"));
        } else if (event.isAtMe()) {
            if (event.getTextMessage().contains("省流") || event.getTextMessage().contains("总结")) {
                return;
            }
            if ("GPT切换到4.0".equals(event.getTextMessage())) {
                BaiduGPTManager.getManager().clear();
                BaiduGPTManager.getManager().setModelFor40();
                QQBotManager.getInstance().sendMessage(event.isUser(), qq, "切换为4.0了");
                return;
            } else if ("GPT切换到3.5".equals(event.getTextMessage())) {
                BaiduGPTManager.getManager().clear();
                BaiduGPTManager.getManager().setModelFor35();
                QQBotManager.getInstance().sendMessage(event.isUser(), qq, "切换为3.5了");
                return;
            }
            ResponseMessage message = BaiduGPTManager.getManager().sendMessage(
                    String.valueOf(user),
                    event.getTextMessage().replace("@" + QQDatabase.getMe().getUserId(), "").trim());
            String sb = "调用版本:" +
                    BaiduGPTManager.getManager().getGPTVersion() +
                    "\n" +
                    message.getResult();
            QQBotManager.getInstance().sendMessage(event.isUser(), qq, new Text(sb));
        }
    }

    public static void main(String[] args) {
        System.out.println(ConfigTools.load(ConfigTools.CONFIG, ConfigTools.BAIDU_GPT_VERSION));
        val messageEvent = new MessageEvent();
        messageEvent.setMessage(new ArrayList<>());
        messageEvent.setRawMessage("");
        new BaiduGPT().onMessage(123456789L, messageEvent, false);
        BaiduGPTManager.getManager().clear();
        BaiduGPTManager.getManager().setModelFor40();
        new BaiduGPT().onMessage(123456789L, messageEvent, false);
      /*  BaiduGPTManager.getManager().clear();
        BaiduGPTManager.getManager().setModelFor35();
        new BaiduGPT().onMessage(123456789L, messageEvent, false);*/
    }
}
