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.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 lombok.val;

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

@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);
        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())) {
                List<BaseHandle<?>> list = new ArrayList<>();
                if (isAdmin()) {
                    list.add(new At(user));
                    list.add(new Text("切换为4.0了"));
                    BaiduGPTManager.getManager().clear();
                    BaiduGPTManager.getManager().setModelFor40();
                    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 ("GPT切换到3.5".equals(event.getTextMessage())) {
                List<BaseHandle<?>> list = new ArrayList<>();
                if (isAdmin()) {
                    list.add(new At(user));
                    list.add(new Text("切换为3.5了"));
                    BaiduGPTManager.getManager().clear();
                    BaiduGPTManager.getManager().setModelFor35();
                    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("画画")){
                val file = BaiduGPTManager.getManager().textToImage(String.valueOf(qq), event.getTextMessage().replace("@" + QQDatabase.getMe().getUserId(), "").replace("画画", "").trim());
                if(file==null){
                    QQBotManager.getInstance().sendMessage(event.isUser(), qq, new Text("画不出"));
                }else{
                    QQBotManager.getInstance().sendMessage(file,qq, event.getMessageId().toString(), "好嘞");
                }
                return;
            }
            if(checkImage()) {
                parseImage(event, qq);
                return;

            }
            Message message = BaiduGPTManager.getManager().sendMessage(
                    String.valueOf(qq),
                    event.getTextMessage().replace("@" + QQDatabase.getMe().getUserId(), "").trim());
            String sb = "调用版本:" +
                    BaiduGPTManager.getManager().getGPTVersion() +
                    "\n" +
                    message.getContent();
            QQBotManager.getInstance().sendMessage(event.isUser(), qq, new Text(sb));
        }
    }
    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();
        BaiduGPTManager.getManager().setModelFor40();
        new BaiduGPT().onMessage(123456789L, messageEvent, false);
      /*  BaiduGPTManager.getManager().clear();
        BaiduGPTManager.getManager().setModelFor35();
        new BaiduGPT().onMessage(123456789L, messageEvent, false);*/
    }
}
