package com.playerblocklife;

import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import java.util.UUID;

public class AdminCommands implements CommandExecutor {
    private final PlayerBlockLife plugin;

    public AdminCommands(PlayerBlockLife plugin) {
        this.plugin = plugin;
    }

    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        if (!sender.hasPermission("playerblocklife.admin")) {
            sender.sendMessage("§c你没有权限使用此命令！");
            return true;
        }

        if (command.getName().equalsIgnoreCase("pblreload")) {
            plugin.reloadConfig();
            plugin.getBlockManager().loadData();
            plugin.getSkinManager().loadAllSkins();
            sender.sendMessage("§a插件配置已重载！");
            return true;
        }

        if (command.getName().equalsIgnoreCase("pbldelete")) {
            if (args.length < 1) {
                sender.sendMessage("§c用法: /pbldelete <玩家名>");
                return true;
            }

            String targetName = args[0];
            Player target = Bukkit.getPlayer(targetName);
            UUID targetId;

            if (target != null) {
                targetId = target.getUniqueId();
            } else {
                // 尝试从离线玩家获取UUID
                targetId = Bukkit.getOfflinePlayer(targetName).getUniqueId();
            }

            plugin.getBlockManager().clearPlayerBlocks(targetId);
            sender.sendMessage("§a已删除玩家 " + targetName + " 的生命方块");
            return true;
        }

        if (command.getName().equalsIgnoreCase("pblrevive")) {
            Player target;

            if (args.length < 1) {
                if (!(sender instanceof Player)) {
                    sender.sendMessage("§c控制台使用时必须指定玩家名: /pblrevive <玩家名>");
                    return true;
                }
                target = (Player) sender;
            } else {
                target = Bukkit.getPlayer(args[0]);
                if (target == null) {
                    sender.sendMessage("§c玩家不存在或不在线！");
                    return true;
                }
            }

            plugin.getLifeSystem().revivePlayer(target);
            sender.sendMessage("§a玩家 " + target.getName() + " 已复活！");
            return true;
        }

        if (command.getName().equalsIgnoreCase("pblstats")) {
            int totalPlayers = plugin.getBlockManager().getPlayerBlocksCount();
            int totalBlocks = plugin.getBlockManager().getTotalBlocksCount();

            sender.sendMessage("§a===== PlayerBlockLife 统计 =====");
            sender.sendMessage("§7注册玩家数: §e" + totalPlayers);
            sender.sendMessage("§7总生命方块数: §e" + totalBlocks);
            sender.sendMessage("§7在线玩家生命方块:");

            for (Player player : Bukkit.getOnlinePlayers()) {
                int blocks = plugin.getBlockManager().getRemainingBlocks(player.getUniqueId());
                sender.sendMessage("§7- " + player.getName() + ": §e" + blocks + " §7/ §a5");
            }

            return true;
        }

        sender.sendMessage("§c未知的管理员命令！");
        sender.sendMessage("§e可用命令:");
        sender.sendMessage("§7/pblreload §8- §f重载插件配置");
        sender.sendMessage("§7/pbldelete <玩家> §8- §f删除玩家的生命方块");
        sender.sendMessage("§7/pblrevive [玩家] §8- §f复活被淘汰的玩家");
        sender.sendMessage("§7/pblstats §8- §f查看插件统计");
        return true;
    }
}