package com.yutou.qqbot.models.Commands;

import com.yutou.napcat.handle.BaseHandle;
import com.yutou.napcat.handle.Image;
import com.yutou.napcat.handle.Text;
import com.yutou.qqbot.Annotations.UseModel;
import com.yutou.qqbot.QQBotManager;
import com.yutou.qqbot.models.Model;
import com.yutou.qqbot.utlis.BangumiTools;
import com.yutou.qqbot.utlis.RedisTools;
import com.yutou.napcat.event.MessageEvent;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@UseModel
public class Bangumi extends Model {


    @Override
    public void onMessage(Long qq, MessageEvent event, boolean isGroup) {
        super.onMessage(qq, event, isGroup);

        switch (msg) {
            case QQGroupCommands.QQ_BANGUMI_TODAY -> {
                QQBotManager.getInstance().sendMessage(event.isUser(), qq, "获取中...");
                RedisTools.remove("reportToDayBangumi");
                QQBotManager.getInstance().sendMessage(event.isUser(), qq, new Text(BangumiTools.reportToDayBangumi()));
            }
            case QQGroupCommands.QQ_BANGUMI_LIST -> {
                QQBotManager.getInstance().sendMessage(event.isUser(), qq, "获取中...");
                QQBotManager.getInstance().sendMessage(event.isUser(), qq, BangumiTools.reportBangumiList());
            }
            default -> {
                if (msg.startsWith(QQGroupCommands.QQ_BANGUMI_SUB)) {
                    subBanGumi(qq, msg);
                }
            }
        }

    }

    private void subBanGumi(long qq, String msg) {
        List<String> infos = null;
        QQBotManager.getInstance().sendMessage(event.isUser(), qq, "获取中...");
        try {
            int id = Integer.parseInt(msg.replace(QQGroupCommands.QQ_BANGUMI_SUB, "").trim());
            infos = BangumiTools.reportBangumiInfo(id);
        } catch (Exception e) {
            String key = msg.replace(QQGroupCommands.QQ_BANGUMI_SUB, "").trim();
            infos = BangumiTools.reportSearchBangumi(key);
        }
        for (String info : infos) {
            List<String> imgs = new ArrayList<>();
            if (info.contains("<img") && info.contains(" /img>")) {
                imgs = getImages(info);
                for (String img : imgs) {
                    info = info.replace("<img " + img + " /img>", "");
                }
            }
            sendImagesMsg(imgs, qq, info, msg.replace(QQGroupCommands.QQ_BANGUMI_SUB, "").trim());
        }
    }

    public static List<String> getImages(String str) {
        List<String> list = new ArrayList<>();
        String regex = "<img(.*?)/img>";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(str);
        while (matcher.find()) {
            list.add(matcher.group().replace("<img", "")
                    .replace("/img>", "")
                    .trim());
        }
        return list;
    }

    private int index = 0;

    private void sendImagesMsg(List<String> imgs, Long qq, String text, String key) {
        index = 0;
        if (imgs.size() == 0) {
            QQBotManager.getInstance().sendMessage(event.isUser(), qq, text);
            return;
        }
        List<BaseHandle<?>> list = new ArrayList<>();
        for (String img : imgs) {
            list.add(new Image(img.replace("http://", "https://")));
        }
        list.add(new Text(text));
        QQBotManager.getInstance().sendMessage(event.isUser(), qq, list);
    }

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

    @Override
    public String[] getUsePowers() {
        return new String[]{
                QQGroupCommands.QQ_BANGUMI_SUB,
                QQGroupCommands.QQ_BANGUMI_LIST,
                QQGroupCommands.QQ_BANGUMI_TODAY
        };
    }

    @Override
    public String getModelName() {
        return "新番列表";
    }

    @Override
    public void onTime(Long qq, String time) {
        super.onTime(qq, time);
        switch (time) {
            case "08:00:00", "20:00:00" ->
                    QQBotManager.getInstance().sendMessage(qq, BangumiTools.reportToDayBangumi());
        }
    }
}
