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 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;

import java.util.List;

public class BindAdminCmd extends Command {


    private static String  prefix;

    public BindAdminCmd() {
        super("bindadmin");
    }

    @Override
    public void execute(CommandSender sender, String[] args) {
        if(!sender.hasPermission("bungeecord.*")){return;}
        if(args.length == 0){
            sender.sendMessage("§e------- ======= §6妖魂的MC机器人 §e======= -------");
            sender.sendMessage("§2/bindadmin clear §f- §2清理所有缓存");
            sender.sendMessage("§2/bindadmin change §e[玩家名] §b<企鹅号> §f- §2修改绑定");
            sender.sendMessage("§2/bindadmin group §e[群号] §f- §2新增/删除检测群");
            sender.sendMessage("§e------- ======= §6妖魂的MC机器人 §e======= -------");
            return;
        }
        if(args.length == 1 && args[0].equalsIgnoreCase("clear")){
            CacheManager.codeDataMap.clear();
            CacheManager.randomCodeMap.clear();
            sender.sendMessage("[Bot管理] 所有缓存数据已被清理");
            return;
        }
        if(args[0].equalsIgnoreCase("change")){
            if(args.length == 1){
                sender.sendMessage("[Bot管理] 请填入正确的玩家名.");
                return;
            }
            if(args.length == 2){
                sender.sendMessage("[Bot管理] 请填入正确的企鹅号.");
                return;
            }
            String playerName = args[1];
            String senderID = args[2];
            SQLIO.saveGroup(playerName, senderID);
            sender.sendMessage("[Bot管理] 已更改 "+playerName+" 的绑定为 <"+senderID+">");
            return;
        }
        if(args[0].equalsIgnoreCase("group")){
            if(args.length == 1){
                sender.sendMessage("[Bot管理] 请填写群号.");
                return;
            }
            long groupID = Long.parseLong(args[1]);
            List<Long> groupAccountList = Config.BOT_GroupAccounts;
            if(groupAccountList.contains(groupID)){
                Config.BOT_GroupAccounts.remove(groupID);
                sender.sendMessage("[Bot管理] 已移除检测群: "+groupID);
            } else {
                Config.BOT_GroupAccounts.add(groupID);
                sender.sendMessage("[Bot管理] 已添加检测群: "+groupID);
            }
            Config.saveBOT_GroupAccounts();
            return;
        }
    }
}
