package com.yutou.qqbot.models.WebSign;

import com.yutou.qqbot.Annotations.UseModel;
import com.yutou.qqbot.QQBotManager;
import com.yutou.qqbot.bilibili.BiliBiliManga;
import com.yutou.qqbot.bilibili.BiliLogin;
import com.yutou.qqbot.interfaces.ObjectInterface;
import com.yutou.qqbot.models.Model;
import com.yutou.qqbot.utlis.QRCodeUtils;
import com.yutou.napcat.event.MessageEvent;

import java.io.File;

@UseModel
public class BiliBiliMangeSign extends Model {
    @Override
    public boolean isUserPublic() {
        return false;
    }

    @Override
    public String[] getUsePowers() {
        return new String[]{
                QQFromCommands.BILI_MANGA_SIGN,
                QQFromCommands.BILI_MANGA_PAY,
                QQFromCommands.BILI_MANGA_PAY_STOP
        };
    }

    @Override
    public String getModelName() {
        return "B站漫画签到and商城兑换";
    }

    @Override
    public void onMessage(Long qq, MessageEvent event, boolean isGroup) {
        super.onMessage(qq, event, isGroup);
        if(user==null){
            return;
        }
        BiliBiliManga manga = new BiliBiliManga(user);
        if (msg.equals(QQFromCommands.BILI_MANGA_SIGN)) {
            String msg;
            if (new BiliLogin(user).testLogin()) {
                if (BiliBiliManga.sign() == null) {
                    msg = "B站漫画已经签到过了";
                } else {
                    msg = "B站漫画签到完成";
                }
                QQBotManager.getInstance().sendMessage(qq, msg);
            } else {
                new BiliLogin(user).loginAsQQ();
            }
        } else if (msg.equals(QQFromCommands.BILI_MANGA_PAY)) {
            StringBuilder builder = new StringBuilder();

            builder.append("可用积分:").append(BiliBiliManga.getMyPoint()).append("\n");
            builder.append("-------商城列表-------").append("\n");
            for (BiliBiliManga.Product product : BiliBiliManga.getListProduct()) {
                builder.append(product).append("\n");
            }
            builder.append("-------------------").append("\n");
            builder.append("使用方法:")
                    .append(QQFromCommands.BILI_MANGA_PAY)
                    .append(" id 数量[num]")
                    .append("\n");
            builder.append("参数说明:")
                    .append("\n")
                    .append("id=物品id")
                    .append("\n")
                    .append("num=物品数量，缺省或-1为全部")
                    .append("\n");
            if (BiliBiliManga.isPayMission()) {
                builder.append("当前已有执行任务：").append(BiliBiliManga.getMission());
            }
            QQBotManager.getInstance().sendMessage(qq, builder.toString());
        } else if (msg.equals(QQFromCommands.BILI_MANGA_PAY_STOP)) {
            manga.stopPayMission();
            QQBotManager.getInstance().sendMessage(qq, "当前任务已取消:" + BiliBiliManga.getMission());
        } else if (msg.startsWith(QQFromCommands.BILI_MANGA_PAY)) {
            msg = msg.replace(QQFromCommands.BILI_MANGA_PAY, "");
            String[] message = msg.split(" ");
            int num = -1;
            int id = 0;
            for (String s : message) {
                try {
                    if (id == 0) {
                        id = Integer.parseInt(s);
                    } else if (num == -1) {
                        num = Integer.parseInt(s);
                    }
                } catch (Exception ignored) {

                }
            }

            if (id == 0) {
                System.out.println(msg);
                for (String s : message) {
                    System.out.println(s);
                }
                QQBotManager.getInstance().sendMessage(qq, "商城id错误");
                return;
            }

            manga.addInterface(new ObjectInterface() {
                @Override
                public void out(String data) {
                    super.out(data);
                    if (!data.startsWith("[2]") && !data.startsWith("[4]") && !data.startsWith("[1]")) {
                        QQBotManager.getInstance().sendMessage(qq, "任务异常:" + data);
                        manga.stopPayMission();
                    }
                }
            });
            QQBotManager.getInstance().sendMessage(qq, manga.startPayMission(id, num).getString("msg"));
        }
    }

    @Override
    public void onTime(Long qq, String time) {
        super.onTime(qq, time);
        if ("07:05:00".equals(time)) {
            if (new BiliLogin(QQBotManager.defQQ).testLogin()) {
                String msg;
                if (BiliBiliManga.sign() == null) {
                    msg = "B站漫画已经签到过了";
                } else {
                    msg = "B站漫画签到完成";
                }
                QQBotManager.getInstance().sendMessage(msg);
            }
        }
    }
}
