package com.yutou.qqbot.models.Commands;

import com.yutou.napcat.event.MessageEvent;
import com.yutou.napcat.handle.At;
import com.yutou.napcat.handle.Text;
import com.yutou.napcat.http.NapCatApi;
import com.yutou.napcat.model.GroupUserBean;
import com.yutou.okhttp.BaseBean;
import com.yutou.okhttp.HttpCallback;
import com.yutou.qqbot.Annotations.UseModel;
import com.yutou.qqbot.QQBotManager;
import com.yutou.qqbot.interfaces.ObjectInterface;
import com.yutou.qqbot.models.Model;
import com.yutou.qqbot.utlis.RedisTools;

import java.text.SimpleDateFormat;
import java.util.*;

@UseModel
public class QQBean extends Model {
    @Override
    public boolean isUserPublic() {
        return true;
    }

    @Override
    public String[] getUsePowers() {
        return new String[0];
    }

    @Override
    public String getModelName() {
        return "QQ禁言";
    }

    Random random = new Random();

    @Override
    public void onMessage(Long qq, MessageEvent event, boolean isGroup) {
        super.onMessage(qq, event, isGroup);
        if (!isGroup) return;
        if ("抽奖".equals(msg)) {
            int hour = 60;
            int day = 1440;
            int max = 30 * day;
            int time = 1;
            if (random.nextInt(100) == 23) {
                releaseAll(qq, true);
            } else if (random.nextInt(10) > 2) {
                time = random.nextInt(hour);
            } else if (random.nextInt(10) > 4) {
                time = random.nextInt(day);
            } else if (random.nextInt(10) > 5) {
                time = random.nextInt(day, 3 * day);
            } else {
                time = random.nextInt(max);

            }
            int sendTime = time;
            QQBotManager.getInstance().groupBan(qq, user, sendTime * 60, new ObjectInterface() {
                @Override
                public void out(String data) {
                    super.out(data);
                    if (data != null) {
                        QQBotManager.getInstance().sendMessage(event.isUser(), qq,
                                new Text("恭喜"),
                                new At(user),
                                new Text("获得了" + sendTime + "分钟的禁言," + sendAchievement(qq, user, sendTime))
                        );
                        if (sendTime > day && random.nextInt(10) >= 3) {
                            int tmp = random.nextInt(sendTime / 2, sendTime);
                            QQBotManager.getInstance().groupBan(qq, user, (sendTime - tmp) * 60, null);
                            QQBotManager.getInstance().sendMessage(qq, "触发减伤:-" + tmp);
                        }
                    }
                }
            });
        } else if ("大赦天下".equals(msg) && isAdmin()) {
            releaseAll(qq, true);
        } else if ("查看禁言列表".equals(msg) && isAdmin()) {
            releaseAll(qq, false);
        }

    }

    String sendAchievement(long qq, long user, int timer) {
        Calendar data1 = Calendar.getInstance();
        Calendar data2 = Calendar.getInstance();
        data1.setTime(new Date());
        data2.set(Calendar.HOUR_OF_DAY, 23);
        data2.set(Calendar.MINUTE, 59);
        data2.set(Calendar.SECOND, 59);
        long time = data2.getTimeInMillis() - data1.getTimeInMillis();
        String achievement = RedisTools.get("shut_" + qq + "_" + user);
        int achievementTimer = timer;
        if (achievement != null) {
            achievementTimer += Integer.parseInt(achievement);
        }
        RedisTools.set("shut_" + qq + "_" + user, achievementTimer + "", time / 1000);
        return "今日累计" + achievementTimer + "分钟禁言";
    }

    void releaseAll(long qq, boolean isRelease) {
        QQBotManager.getInstance().getShutUpList(qq, new HttpCallback<List<GroupUserBean>>() {
            @Override
            public void onResponse(int code, String status, List<GroupUserBean> response, String rawResponse) {
                List<GroupUserBean> shutList = new ArrayList<>();
                for (GroupUserBean bean : response) {
                    if (bean.getShutUpTimestamp() > 60) {
                        shutList.add(bean);
                    }
                }
                if (!shutList.isEmpty()) {

                    if (!isRelease) {
                        StringBuilder sb = new StringBuilder();
                        for (GroupUserBean bean : shutList) {
                            sb.append(bean.getNickname()).append(":").append(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",Locale.CHINA).format(new Date(bean.getShutUpTimestamp()))).append("\n");
                        }
                        QQBotManager.getInstance().sendMessage(qq, "当前塞了：" + shutList.size() + "人" + "\n" + sb);
                        return;
                    }
                    QQBotManager.getInstance().sendMessage(qq, "触发自动解禁，解禁人数：" + shutList.size() + "人");
                    for (GroupUserBean bean : shutList) {
                        NapCatApi.getGroupApi().groupBan(qq, bean.getUserId(), 0).enqueue(new HttpCallback<BaseBean>() {
                            @Override
                            public void onResponse(int code, String status, BaseBean response, String rawResponse) {

                            }

                            @Override
                            public void onFailure(Throwable throwable) {

                            }
                        });
                    }
                } else {
                    QQBotManager.getInstance().sendMessage(qq, "阿巴阿巴");
                }
            }

            @Override
            public void onFailure(Throwable throwable) {

            }
        });
    }
}
