package com.playerblocklife;

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

import java.util.UUID;

/**
 * AdminCommands - PlayerBlockLife游戏模式的管理员命令处理器
 * 
 * <p>处理插件的传统管理员命令，适配新的PlayerBlockLife游戏模式：<br>
 * <ul>
 *   <li>/pblreload - 重载插件配置</li>
 *   <li>/pbldelete - 删除指定玩家的生命方块</li>
 *   <li>/pblrevive - 复活被淘汰的玩家</li>
 *   <li>/pblstats - 显示PlayerBlockLife游戏统计信息</li>
 * </ul>
 * 
 * <p>这些命令在新模式下会与GameStateManager交互，以正确管理游戏状态。</p>
 * 
 * @author xiaobai
 * @version 2.2.0-1.20.4
 * @since 1.0.0
 */
public class AdminCommands implements CommandExecutor {
    private final PlayerBlockLife plugin;

    /**
     * 构造一个新的管理员命令执行器
     * 
     * @param plugin 插件主类实例
     */
    public AdminCommands(PlayerBlockLife plugin) {
        this.plugin = plugin;
    }

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

        if (commandName.equals("pblreload")) {
            // 重载配置
            plugin.reloadPluginConfig();
            sender.sendMessage(ChatColor.GREEN + "配置已重载！");
            return true;
        }

        if (commandName.equals("pbldelete")) {
            if (args.length < 1) {
                sender.sendMessage(ChatColor.RED + "用法: /pbldelete <玩家>");
                return true;
            }

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

            if (target == null) {
                sender.sendMessage(ChatColor.RED + "找不到玩家: " + targetName);
                return true;
            }

            // 在新模式下，我们使用GameStateManager来清除玩家方块
            // 清除玩家的方块数据
            UUID targetId = target.getUniqueId();
            plugin.getGameStateManager().clearPlayerBlocks(targetId);
            
            sender.sendMessage(ChatColor.GREEN + "已删除玩家 " + targetName + " 的生命方块！");
            target.sendMessage(ChatColor.YELLOW + "你的生命方块已被管理员清除。");
            return true;
        }

        if (commandName.equals("pblrevive")) {
            Player target;

            if (args.length < 1) {
                if (!(sender instanceof Player)) {
                    sender.sendMessage(ChatColor.RED + "用法: /pblrevive <玩家>");
                    return true;
                }
                target = (Player) sender;
            } else {
                target = Bukkit.getPlayer(args[0]);
                if (target == null) {
                    sender.sendMessage(ChatColor.RED + "玩家 " + args[0] + " 不在线！");
                    return true;
                }
            }

            // 在新模式下，复活玩家需要将其从淘汰列表中移除并恢复生存模式
            UUID targetId = target.getUniqueId();
            if (plugin.getGameStateManager().getRemainingBlocks(targetId) <= 0) {
                // 如果玩家已被淘汰，需要将其重新添加到存活玩家列表
                java.util.List<UUID> alivePlayers = plugin.getGameStateManager().getAlivePlayers();
                if (!alivePlayers.contains(targetId)) {
                    // 在新模式下，复活需要重新生成方块或将其重新加入游戏
                    sender.sendMessage(ChatColor.RED + "在新模式下，复活功能需要重新开始游戏或为玩家重新生成方块。");
                    sender.sendMessage(ChatColor.YELLOW + "建议使用 /pbl rstgm 重置游戏。");
                } else {
                    target.setGameMode(org.bukkit.GameMode.SURVIVAL);
                    target.sendMessage(ChatColor.GREEN + "你已被复活！");
                    sender.sendMessage(ChatColor.GREEN + "玩家 " + target.getName() + " 已被复活！");
                }
            } else {
                target.setGameMode(org.bukkit.GameMode.SURVIVAL);
                target.sendMessage(ChatColor.GREEN + "你已被复活！");
                sender.sendMessage(ChatColor.GREEN + "玩家 " + target.getName() + " 已被复活！");
            }
            return true;
        }

        if (commandName.equals("pblstats")) {
            // 显示新模式下的游戏统计
            GameStateManager gameStateManager = plugin.getGameStateManager();
            GameStateManager.GameState currentState = gameStateManager.getCurrentState();
            
            sender.sendMessage(ChatColor.GOLD + "=== PlayerBlockLife 游戏统计 ===");
            sender.sendMessage(ChatColor.AQUA + "游戏状态: " + getStateText(currentState));
            
            int onlineCount = Bukkit.getOnlinePlayers().size();
            int opCount = 0;
            int participantCount = 0;
            
            for (Player player : Bukkit.getOnlinePlayers()) {
                if (player.isOp()) {
                    opCount++;
                } else {
                    participantCount++;
                }
            }
            
            sender.sendMessage(ChatColor.YELLOW + "在线玩家: " + onlineCount + 
                              " (参与者: " + participantCount + ", OP: " + opCount + ")");
            
            if (currentState == GameStateManager.GameState.STARTED) {
                int aliveCount = gameStateManager.getAlivePlayersCount();
                sender.sendMessage(ChatColor.GREEN + "存活玩家: " + aliveCount);
                
                if (gameStateManager.isLimitedTime()) {
                    long currentTime = System.currentTimeMillis();
                    long elapsedMinutes = (currentTime - gameStateManager.getGameStartTime()) / (1000 * 60);
                    long remainingMinutes = Math.max(0, gameStateManager.getGameDuration() - elapsedMinutes);
                    sender.sendMessage(ChatColor.YELLOW + "剩余时间: " + remainingMinutes + " 分钟");
                }
            }
            
            sender.sendMessage(ChatColor.GRAY + "-------------------");
            for (Player player : Bukkit.getOnlinePlayers()) {
                int blocks = gameStateManager.getRemainingBlocks(player.getUniqueId());
                String status = player.isOp() ? ChatColor.GOLD + "OP" : 
                               (blocks > 0 ? ChatColor.GREEN + "存活" : ChatColor.RED + "淘汰");
                sender.sendMessage(ChatColor.WHITE + "- " + player.getName() + ": " + 
                                  ChatColor.YELLOW + blocks + "/5 " + 
                                  ChatColor.GRAY + "(" + status + ChatColor.GRAY + ")");
            }

            return true;
        }

        sender.sendMessage(ChatColor.RED + "未知的管理员命令！");
        return true;
    }
    
    private String getStateText(GameStateManager.GameState state) {
        switch (state) {
            case WAITING: return ChatColor.YELLOW + "等待中";
            case STARTED: return ChatColor.GREEN + "进行中";
            case FINISHED: return ChatColor.RED + "已结束";
            default: return ChatColor.GRAY + "未知";
        }
    }
}