package com.yutou.qqbot.models.Commands;

import com.yutou.napcat.QQDatabase;
import com.yutou.napcat.handle.*;
import com.yutou.qqbot.Annotations.UseModel;
import com.yutou.qqbot.QQBotManager;
import com.yutou.qqbot.data.baidu.Message;
import com.yutou.qqbot.gpt.AbsGPTManager;
import com.yutou.qqbot.gpt.SiliconGPTManager;
import com.yutou.qqbot.interfaces.DownloadInterface;
import com.yutou.qqbot.models.Model;
import com.yutou.qqbot.gpt.BaiduGPTManager;
import com.yutou.napcat.event.MessageEvent;
import com.yutou.qqbot.utlis.ConfigTools;
import com.yutou.qqbot.utlis.HttpTools;
import com.yutou.qqbot.utlis.Log;
import com.yutou.qqbot.utlis.StringUtils;
import lombok.val;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

@UseModel
public class BaiduGPT extends Model {
    private Class<?> gptManager = SiliconGPTManager.class;

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

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

    public BaiduGPT() {
        val load = ConfigTools.load(ConfigTools.CONFIG, ConfigTools.GPT, String.class);
        if(!StringUtils.isEmpty(load)){
            switch (load){
                case "baidu":
                    gptManager = BaiduGPTManager.class;
                    break;
                case "silicon":
                    gptManager = SiliconGPTManager.class;
                    break;
            }
        }else{
            gptManager = BaiduGPTManager.class;
        }
    }

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

    @Override
    public void onMessage(Long qq, MessageEvent event, boolean isGroup) {
        super.onMessage(qq, event, isGroup);
        if (event.getTextMessage().equals(QQGroupCommands.GPT_CLEAR)) {
            AbsGPTManager.getManager(gptManager).clear();
            QQBotManager.getInstance().sendMessage(event.isUser(), qq, new Text("已经失忆捏"));
        } else if (event.isAtMe()) {
            if (event.getTextMessage().contains("省流") || event.getTextMessage().contains("总结")) {
                return;
            }
            if (event.getTextMessage().contains("画画")) {
                val file = BaiduGPTManager.getManager().textToImage(String.valueOf(qq), event.getTextMessage().replace("@" + QQDatabase.getMe().getUserId(), "").replace("画画", "").trim());
                Log.i("画图",event.getTextMessage());
                if (file == null) {
                    QQBotManager.getInstance().sendMessage(event.isUser(), qq, new Text("画不出"));
                } else {
                    QQBotManager.getInstance().sendMessage(file, qq, event.getMessageId().toString(), "好嘞");
                }
                return;
            } else if (event.getTextMessage().contains("GPT切换到")) {
                val text = event.getTextMessage().replace("@" + QQDatabase.getMe().getUserId(), "").replace("GPT切换到", "").trim();
                List<BaseHandle<?>> list = new ArrayList<>();
                if (isAdmin()) {
                    list.add(new At(user));
                    list.add(new Text("切换为" + text));
                    AbsGPTManager.getManager(gptManager).clear();
                    AbsGPTManager.getManager(gptManager).setModel(text);
                    QQBotManager.getInstance().sendMessage(event.isUser(), qq, list);
                } else {
                    list.add(new At(user));
                    list.add(new Text("你没有权限"));
                    QQBotManager.getInstance().sendMessage(event.isUser(), qq, list);
                }
                return;
            }else if(event.getTextMessage().contains("大模型切换到")){
                List<BaseHandle<?>> list = new ArrayList<>();
                val text = event.getTextMessage().replace("@" + QQDatabase.getMe().getUserId(), "").replace("大模型切换到", "").trim();
                if(text.contains("baidu")|| text.contains("百度")){
                    gptManager= BaiduGPTManager.class;
                    ConfigTools.save(ConfigTools.CONFIG, ConfigTools.GPT, "baidu");
                }else if(text.contains("silicon")||text.contains("硅基")){
                    gptManager= SiliconGPTManager.class;
                    ConfigTools.save(ConfigTools.CONFIG, ConfigTools.GPT, "silicon");
                }
                list.add(new At(user));
                list.add(new Text("切换为" + gptManager.getSimpleName()));
                AbsGPTManager.getManager(gptManager).clear();
                QQBotManager.getInstance().sendMessage(event.isUser(), qq, list);
                return;
            }
            if (checkImage()) {
                parseImage(event, qq);
                return;

            }
            Message message = AbsGPTManager.getManager(gptManager).sendMessage(
                    String.valueOf(qq),
                    event.getTextMessage().replace("@" + QQDatabase.getMe().getUserId(), "").trim());
            String sb = "调用版本:" +
                    gptManager.getSimpleName()+
                    "\n" +
                    "使用模型:"+
                    AbsGPTManager.getManager(gptManager).getGPTVersion() +
                    "\n"+
                    message.getContent();
            QQBotManager.getInstance().sendMessage(event.isUser(), qq, new Text(sb));
            AbsGPTManager.getManager(gptManager).sendMessageAndScheduleClear();
        }
    }

    private void parseImage(MessageEvent event, Long qq) {
        Image imageHandle = event.findType(Image.class);
        val replyHandle = event.findType(Reply.class);
        if (replyHandle != null && imageHandle == null) {
            imageHandle = getReply(replyHandle.getData().getId()).findType(Image.class);
        }
        if (imageHandle == null) {
            return;
        }
        HttpTools.download(imageHandle.getData().getUrl(), "gpt_parse_image.png", new DownloadInterface() {
            @Override
            public void onDownload(File file) {
                super.onDownload(file);
                if (file == null) {
                    return;
                }
                val text = BaiduGPTManager.getManager().imageToText(String.valueOf(qq), file);
                QQBotManager.getInstance().sendMessage(event.isUser(), qq, new Reply(event.getMessageId()), new Text(text));
            }
        });

    }

    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();
        new BaiduGPT().onMessage(123456789L, messageEvent, false);
      /*  BaiduGPTManager.getManager().clear();
        BaiduGPTManager.getManager().setModelFor35();
        new BaiduGPT().onMessage(123456789L, messageEvent, false);*/
    }
}
