package com.playerblocklife;

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

/**
 * PlayerBlockLife命令执行器 - 处理PlayerBlockLife游戏模式的专用命令
 * 
 * <p>提供PlayerBlockLife游戏模式的核心控制命令，包括：
 * <ul>
 *   <li>/PlayerBlockLife start [时间] - 开始游戏，支持可选的限时模式</li>
 *   <li>/PlayerBlockLife rstgm - 重置游戏，重新分配玩家颜色</li>
 * </ul>
 * 
 * <p>这些命令仅允许服务器管理员（OP）执行，用于控制PlayerBlockLife游戏的生命周期。</p>
 * 
 * @author xiaobai
 * @version 2.2.0-1.20.4
 * @since 4.0.0
 */
public class PBLCommands implements CommandExecutor {
    private final PlayerBlockLife plugin;

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

    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        if (!(sender instanceof Player) && !sender.hasPermission("playerblocklife.admin")) {
            // 控制台默认有权限
            if (!sender.isOp() && !sender.hasPermission("playerblocklife.admin")) {
                sender.sendMessage(ChatColor.RED + "你没有权限执行此命令！");
                return true;
            }
        } else if (sender instanceof Player) {
            Player player = (Player) sender;
            if (!player.isOp() && !player.hasPermission("playerblocklife.admin")) {
                player.sendMessage(ChatColor.RED + "你没有权限执行此命令！");
                return true;
            }
        }

        if (args.length == 0) {
            if (sender instanceof Player) {
                showHelp((Player) sender);
            } else {
                showConsoleHelp(sender);
            }
            return true;
        }

        String subCommand = args[0].toLowerCase();

        if (subCommand.equals("start")) {
            handleStartCommand(sender, args);
        } else if (subCommand.equals("rstgm") || subCommand.equals("reset")) {
            handleResetCommand(sender);
        } else {
            if (sender instanceof Player) {
                showHelp((Player) sender);
            } else {
                showConsoleHelp(sender);
            }
        }

        return true;
    }

    private void handleStartCommand(CommandSender sender, String[] args) {
        if (plugin.getGameStateManager().getCurrentState() == GameStateManager.GameState.STARTED) {
            sender.sendMessage(ChatColor.RED + "游戏已在进行中！");
            return;
        }

        int duration = 0;
        boolean isTimed = false;

        // 检查是否有时间参数
        if (args.length > 1) {
            try {
                duration = Integer.parseInt(args[1]);
                if (duration <= 0) {
                    sender.sendMessage(ChatColor.RED + "游戏时间必须大于0分钟！");
                    return;
                }
                isTimed = true;
                sender.sendMessage(ChatColor.GREEN + "限时游戏开始，持续时间: " + duration + " 分钟！");
            } catch (NumberFormatException e) {
                sender.sendMessage(ChatColor.RED + "无效的时间参数！请输入分钟数，例如: /pbl start 30");
                return;
            }
        } else {
            sender.sendMessage(ChatColor.GREEN + "游戏开始！");
        }

        // 开始游戏
        if (isTimed) {
            plugin.getGameStateManager().startTimedGame(duration);
        } else {
            plugin.getGameStateManager().startGame();
        }

        // 广播游戏开始消息
        for (Player onlinePlayer : org.bukkit.Bukkit.getOnlinePlayers()) {
            onlinePlayer.sendMessage(ChatColor.GOLD + "游戏开始！保护好你的生命方块！");
            if (isTimed) {
                onlinePlayer.sendMessage(ChatColor.YELLOW + "限时: " + duration + " 分钟");
            }
        }
    }

    private void handleResetCommand(CommandSender sender) {
        plugin.getGameStateManager().resetGame();
        sender.sendMessage(ChatColor.YELLOW + "游戏已重置，所有玩家回到等待状态。");

        // 广播重置消息
        for (Player onlinePlayer : org.bukkit.Bukkit.getOnlinePlayers()) {
            onlinePlayer.sendMessage(ChatColor.GOLD + "游戏已重置，请等待重新分配颜色。");
        }
    }

    private void showHelp(Player player) {
        player.sendMessage(ChatColor.GOLD + "=== PlayerBlockLife 命令 ===");
        player.sendMessage(ChatColor.YELLOW + "/pbl start [时间(分钟)] - 开始游戏，可选限时");
        player.sendMessage(ChatColor.YELLOW + "/pbl rstgm - 重置游戏");
        player.sendMessage(ChatColor.GRAY + "注：只有管理员(权限)可以执行这些命令");
    }
    
    private void showConsoleHelp(CommandSender sender) {
        sender.sendMessage(ChatColor.GOLD + "=== PlayerBlockLife 命令 ===");
        sender.sendMessage(ChatColor.YELLOW + "/pbl start [时间(分钟)] - 开始游戏，可选限时");
        sender.sendMessage(ChatColor.YELLOW + "/pbl rstgm - 重置游戏");
        sender.sendMessage(ChatColor.GRAY + "注：控制台拥有执行这些命令的权限");
    }
}