package com.yaohun.mcbot.listsener;

import com.yaohun.mcbot.McBot;
import com.yaohun.mcbot.api.McBotAPI;
import com.yaohun.mcbot.config.Config;
import com.yaohun.mcbot.data.BotFriend;
import com.yaohun.mcbot.data.sql.SQLIO;
import com.yaohun.mcbot.event.BotGroupMessageEvent;
import com.yaohun.mcbot.manage.CacheManager;
import com.yaohun.mcbot.util.CDTimeAPI;
import net.md_5.bungee.BungeeCord;
import net.md_5.bungee.api.ProxyServer;
import net.md_5.bungee.api.connection.ProxiedPlayer;
import net.md_5.bungee.api.plugin.Listener;
import net.md_5.bungee.event.EventHandler;

import java.util.concurrent.TimeUnit;

public class GroupListener implements Listener {

    @EventHandler
    public void onGroupChat(BotGroupMessageEvent e) {
        if (Config.BOT_GroupAccounts.contains(e.getGroupId())) {
            long senderId = e.getSenderId();
            String message = e.getMessage();
            if (message.contains("/在线")) {
                if (CDTimeAPI.isCD(senderId, "groupBotCd")) {
                    return;
                }
                CDTimeAPI.addPlayerCD(senderId, "groupBotCd", 1000 * 10);
                int onlineplayer = BungeeCord.getInstance().getOnlineCount();
                String msg = Config.getMessage("checkServerOnline").replace("%count%", String.valueOf(onlineplayer));
                e.getBotGroup().sendMessage(msg);
            } else if (message.contains("/查询")) {
                if (CDTimeAPI.isCD(senderId, "groupBotCd")) {
                    return;
                }
                CDTimeAPI.addPlayerCD(senderId, "groupBotCd", 1000 * 10);
                // 获取这个发送者的QQ号
                String userName = SQLIO.getUserName(String.valueOf(e.getSenderId()));
                if (userName == null) {
                    String msg = Config.getMessage("notBoundYet");
                    e.getBotGroup().sendMessage(msg);
                    return;
                }
                String msg = Config.getMessage("querySuccessful");
                e.getBotGroup().sendMessage(msg.replace("%user%", userName));
            } else if (message.contains("/绑定 ")) {
                if (CDTimeAPI.isCD(senderId, "groupBotCd")) {
                    return;
                }
                CDTimeAPI.addPlayerCD(senderId, "groupBotCd", 1000 * 5);
                String code = Config.extractBindCode(message);
                // 检测这个验证码是否存在缓存数据
                CacheManager.CodeData codeData = CacheManager.getCodeData(code);
                if (codeData == null) {
                    String msg = Config.getMessage("codeError").replace("@{nickName}", "[CQ:at,qq=" + e.getSenderId() + ",name=@" + e.getSenderName() + "]");
                    e.getBotGroup().sendMessage(msg);
                    return;
                }
                // 判断缓存数据中的QQ和这个发送验证码的QQ是否为同一个
                String snederID = String.valueOf(e.getSenderId());
                if (!codeData.getTencentQQ().equals(snederID)) {
                    String msg = Config.getMessage("codeExpired").replace("@{nickName}", "[CQ:at,qq=" + e.getSenderId() + ",name=@" + e.getSenderName() + "]");
                    e.getBotGroup().sendMessage(msg);
                    return;
                }
                // 判断这个QQ号是否已绑定游戏账号
                String userName = SQLIO.getUserName(snederID);
                if (userName != null) {
                    String msg = Config.getMessage("alreadyGroupBindUserName").replace("@{nickName}", "[CQ:at,qq=" + e.getSenderId() + ",name=@" + e.getSenderName() + "]");
                    e.getBotGroup().sendMessage(msg.replace("%user%", userName));
                    return;
                }
                // 判断这个游戏账号是否已绑定其他QQ号
                String playerName = codeData.getUserName();
                String userID = SQLIO.getUserID(playerName);
                if (userID != null) {
                    String msg = Config.getMessage("alreadyGroupBindUserID").replace("@{nickName}", "[CQ:at,qq=" + e.getSenderId() + ",name=@" + e.getSenderName() + "]");
                    e.getBotGroup().sendMessage(msg.replace("%qq%", userID));
                    return;
                }
                // 删除缓存数据
                CacheManager.cleanUpTheCache(playerName, code);
                ProxyServer.getInstance().getScheduler().schedule(McBot.inst(),
                        new Runnable() {
                            public void run() {
                                //设置数据库数据
                                SQLIO.saveGroup(playerName, snederID);
                                // 向群里发送绑定成功消息
                                String msg1 = Config.getMessage("botBindingSuccessful").replace("@{nickName}", "[CQ:at,qq=" + e.getSenderId() + ",name=@" + e.getSenderName() + "]");
                                e.getBotGroup().sendMessage(msg1.replace("%user%", playerName));
                                // 向游戏发送绑定成功消息
                                ProxiedPlayer player = ProxyServer.getInstance().getPlayer(playerName);
                                if (player != null) {
                                    String msg2 = Config.getMessage("playerBindingSuccessful").replace("%qq%", String.valueOf(senderId));
                                    player.sendMessage(Config.getMessage("prefix") + msg2);
                                }
                            }
                        }, 1L, TimeUnit.SECONDS);
            }
        }
    }

}
