package com.yutou.qqbot.models.BiliBili;

import com.yutou.napcat.event.MessageEvent;
import com.yutou.qqbot.Annotations.UseModel;
import com.yutou.qqbot.QQBotManager;
import com.yutou.qqbot.bilibili.BiliBiliUtils;
import com.yutou.qqbot.bilibili.BiliLogin;
import com.yutou.qqbot.models.Model;
import com.yutou.qqbot.utlis.RedisTools;

import java.util.Set;

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

    @Override
    public String[] getUsePowers() {
        return new String[]{
                Model.QQGroupCommands.BILI_LIVE_DANMU_LIST,
                Model.QQGroupCommands.BILI_LIVE_DANMU_SEND,
                QQGroupCommands.BILI_LIVE_DANMU_DEL
        };
    }

    @Override
    public String getModelName() {
        return "BiliBili Live Sign in";
    }

    @Override
    public synchronized void onTime(Long qq, String time) {
        super.onTime(qq, time);
        if ("07:01:00".equals(time)) {
            if (!new BiliLogin(QQBotManager.defQQ).testLogin()) {
                new BiliLogin(QQBotManager.defQQ).loginAsQQ();
                System.out.println(BiliBiliUtils.getInstance(QQBotManager.defQQ).getLoginInfo());
                return;
            }
            signLive(QQBotManager.defQQ, qq);
        }
    }

    private void signLive(long qq, long sendQQ) {
        if (!new BiliLogin(qq).testLogin()) {
            new BiliLogin(qq).loginAsQQ();
            return;
        }
        BiliBiliUtils biliUtils = BiliBiliUtils.getInstance(qq);
        QQBotManager.getInstance().sendMessage(sendQQ, biliUtils.liveSignIn());
        Set<String> biliLive = RedisTools.list_get("bili_live");
        StringBuilder builder = new StringBuilder();
        for (String id : biliLive) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            boolean sign = biliUtils.sendLiveDanmu(Integer.parseInt(id), "打卡");
            builder.append("BiliLiveSign").append(id).append(":").append(sign).append("\n");
        }
        QQBotManager.getInstance().sendMessage(sendQQ, builder.toString());
       /* BiliBiliAppUtils appUtils = new BiliBiliAppUtils(QQBotManager.defQQ);
        AppUserTask oldTask = appUtils.startAppTask();
        AppUserTask newTask = appUtils.getTaskProgress();
        builder = new StringBuilder();
        builder.append("执行APP任务").append("\n").append(AppUserTask.toMessageFormat(oldTask, newTask));
        QQBotManager.getInstance().sendMessage(sendQQ, builder.toString());*/
    }

    @Override
    public void onMessage(Long qq, MessageEvent event, boolean isGroup) {
        super.onMessage(qq, event, isGroup);
        if (!msg.startsWith(QQGroupCommands.BILI_LIVE_DANMU_SEND) &&
                !msg.startsWith(QQGroupCommands.BILI_LIVE_DANMU_DEL) &&
                !msg.startsWith(QQGroupCommands.BILI_LIVE_DANMU_LIST)) {
            return;
        }
        if (msg.equals(QQGroupCommands.BILI_LIVE_DANMU_SEND)) {
            signLive(user, qq);
            return;
        }

        BiliBiliUtils biliUtils = BiliBiliUtils.getInstance(isGroup ? event.getSource().getFromId() : qq);
        StringBuilder message;
        message = new StringBuilder();
        try {
            boolean isDel = false;
            if (msg.startsWith(QQGroupCommands.BILI_LIVE_DANMU_DEL)) {
                isDel = true;
                msg = msg.replace(QQGroupCommands.BILI_LIVE_DANMU_DEL, "").trim();
            } else if (msg.startsWith(QQGroupCommands.BILI_LIVE_DANMU_SEND)) {
                msg = msg.replace(QQGroupCommands.BILI_LIVE_DANMU_SEND, "").trim();
            } else {
                msg = "0";
            }
            Integer roomId = Integer.parseInt(msg);
            if (!new BiliLogin(qq).testLogin()) {
                new BiliLogin(qq).loginAsQQ();
                return;
            }
            if (biliUtils.checkLiveRoom(roomId) && roomId != 0) {
                if (isDel && RedisTools.list_isExist("bili_live", roomId + "")) {
                    RedisTools.list_remove("bili_live", roomId + "");
                    message.append("直播签到删除成功").append("\n");
                } else if (!RedisTools.list_isExist("bili_live", roomId + "")) {
                    RedisTools.list_add("bili_live", roomId + "");
                    message.append("直播签到添加成功").append("\n");
                }
            } else if (roomId != 0) {
                message.append("直播签到操作失败\n");
            }
        } catch (Exception e) {
            message = new StringBuilder("直播签到添加失败\n");
        }
        message.append("-----直播签到房间号-----\n");
        Set<String> biliLive = RedisTools.list_get("bili_live");
        for (String id : biliLive) {
            message.append(id)
                    .append(":")
                    .append(biliUtils.getUserInfo(
                                    biliUtils.getLiveRoom(Integer.parseInt(id))
                                            .getJSONObject("data")
                                            .getInteger("uid"))
                            .getJSONObject("data")
                            .getString("name"))
                    .append("\n");
        }
        QQBotManager.getInstance().sendMessage(qq, message.toString());

    }

    public static void main(String[] args) {
        new BiliBiliLive().signLive(QQBotManager.defQQ, 0);
    }
}
