package com.playerblocklife;

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

import java.util.List;
import java.util.UUID;

public class CheckLifeBlocksCommand implements CommandExecutor {
    private final PlayerBlockLife plugin;

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

    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        if (!(sender instanceof Player)) {
            sender.sendMessage("§c只有玩家可以使用此命令！");
            return true;
        }

        Player player = (Player) sender;
        UUID playerId = player.getUniqueId();

        List<Location> blocks = plugin.getBlockManager().getPlayerBlocks(playerId);
        int remaining = blocks.size();

        if (remaining == 0) {
            player.sendMessage("§c你还没有设置生命方块！");
            player.sendMessage("§7使用 §e/setlifeblocks §7来设置你的生命方块");
            return true;
        }

        player.sendMessage("§a========== 你的生命方块信息 ==========");
        player.sendMessage("§7剩余方块数量: §e" + remaining + " §7/ §a5");
        player.sendMessage("§7当前生命值: §c" +
                (plugin.getLifeSystem().getPlayerHealth(playerId) != null ?
                        plugin.getLifeSystem().getPlayerHealth(playerId) : "20") + " ❤");

        if (remaining <= 2) {
            player.sendMessage("§c⚠ 警告！生命方块即将耗尽！");
        }

        player.sendMessage("§7方块位置:");
        for (int i = 0; i < blocks.size(); i++) {
            Location loc = blocks.get(i);
            String worldName = loc.getWorld() != null ? loc.getWorld().getName() : "未知世界";
            player.sendMessage("§7" + (i + 1) + ". §e世界: " + worldName +
                    " §7坐标: §a" + loc.getBlockX() + " §7, §a" +
                    loc.getBlockY() + " §7, §a" + loc.getBlockZ());
        }

        player.sendMessage("§a======================================");

        if (remaining > 0) {
            player.sendMessage("§e提示：");
            player.sendMessage("§7- 方块距离你: §a" +
                    (int) player.getLocation().distance(blocks.get(0)) + " §7格");
            player.sendMessage("§7- 使用指南针可以追踪方块位置");
        }

        return true;
    }
}