package com.yaohun.mcbot.commands;

import com.yaohun.mcbot.config.Config;
import com.yaohun.mcbot.data.sql.SQLIO;
import com.yaohun.mcbot.manage.CacheManager;
import gnu.trove.stack.TLongStack;
import net.md_5.bungee.api.CommandSender;
import net.md_5.bungee.api.chat.ClickEvent;
import net.md_5.bungee.api.chat.ComponentBuilder;
import net.md_5.bungee.api.chat.HoverEvent;
import net.md_5.bungee.api.chat.TextComponent;
import net.md_5.bungee.api.connection.ProxiedPlayer;
import net.md_5.bungee.api.plugin.Command;

public class BindTencentCmd extends Command {

    private static String  prefix;

    public BindTencentCmd() {
        super("bind");
        prefix = Config.getMessage("prefix");
    }

    @Override
    public void execute(CommandSender sender, String[] args) {
        // 首先判断输入此命令的是否是玩家
        if(sender instanceof ProxiedPlayer) {
            if (args.length == 2) {
                String tencentQQ = args[0].replaceAll("\\[|\\]", "");
                String tencentQQRepeat = args[1].replaceAll("\\[|\\]", "");
                ProxiedPlayer player = (ProxiedPlayer) sender;
                if (!tencentQQ.equalsIgnoreCase(tencentQQRepeat)) {
                    String msg = Config.getMessage("differentTwice");
                    player.sendMessage(prefix+msg);
                    return;
                }
                String userName = player.getName();
                String cacheCode = CacheManager.getRandomCodeData(userName);
                if(cacheCode != null) {
                    sender.sendMessage(" ");
                    sender.sendMessage(" ");
                    if(!Config.Group_Info.isEmpty()) {
                        sender.sendMessage(Config.Group_Info);
                    }
                    SendCopyMessage(player,cacheCode);
                    sender.sendMessage(" ");
                    return;
                }
                // 判断玩家是否已绑定QQ号  若已绑定则拦截数据
                String userID = SQLIO.getUserID(userName);
                if (userID != null) {
                    String message = Config.getMessage("alreadyBindUserID");
                    player.sendMessage(prefix + message.replace("%qq%", userID));
                    return;
                }
                // 判断QQ是否已绑定过账号  若已绑定则连接数据
                String user = SQLIO.getUserName(tencentQQ);
                if (user != null) {
                    String message = Config.getMessage("alreadyBindUserName");
                    player.sendMessage(prefix + message.replace("%user%", user));
                    return;
                }
                // 获取绑定验证码发送给玩家
                String randomCode = Config.getRandomString();
                CacheManager.setRandomCodeData(userName, randomCode); // 将验证码存入缓存
                CacheManager.setCodeDataMap(randomCode,new CacheManager.CodeData(userName,randomCode,tencentQQ));
                sender.sendMessage(" ");
                sender.sendMessage(" ");
                if(!Config.Group_Info.isEmpty()) {
                    sender.sendMessage(Config.Group_Info);
                }
                SendCopyMessage(player, randomCode);
                sender.sendMessage(" ");
            } else {
                String msg = Config.getMessage("correctOperate");
                sender.sendMessage(prefix+msg);
            }
        } else {
            sender.sendMessage("§c此命令仅限以玩家身份执行.");
        }
    }

    public void SendCopyMessage(ProxiedPlayer player,String bind_code) {
        String message = prefix+Config.getMessage("verificationCode").replace("%code%",bind_code);
        TextComponent component = new TextComponent(message);
        System.out.print("玩家: "+player.getName()+"   randomCode: "+bind_code);
        component.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/绑定 "+bind_code));
        component.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder("§e§l/绑定 "+bind_code).create()));
        player.sendMessage(component);
    }
}
