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

public class SetLifeBlocksCommand implements CommandExecutor {
    private final PlayerBlockLife plugin;

    public SetLifeBlocksCommand(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();

        if (args.length > 0 && args[0].equalsIgnoreCase("help")) {
            showHelp(player);
            return true;
        }

        if (args.length > 0 && player.hasPermission("playerblocklife.admin")) {
            if (args[0].equalsIgnoreCase("other")) {
                if (args.length < 2) {
                    player.sendMessage("§c用法: /setlifeblocks other <玩家名>");
                    return true;
                }

                Player target = Bukkit.getPlayer(args[1]);
                if (target == null) {
                    player.sendMessage("§c找不到玩家: " + args[1]);
                    return true;
                }

                setBlocksForPlayer(target, player);
                return true;
            }
        }

        setBlocksForPlayer(player, null);
        return true;
    }

    private void setBlocksForPlayer(Player target, Player executor) {
        UUID targetId = target.getUniqueId();

        if (plugin.getBlockManager().hasLifeBlocks(targetId)) {
            if (executor != null && !targetId.equals(executor.getUniqueId())) {
                executor.sendMessage("§c玩家 " + target.getName() + " 已经有生命方块了！");
            } else {
                target.sendMessage("§c你已经有生命方块了！");
                target.sendMessage("§e使用 /checklifeblocks 查看位置");
                target.sendMessage("§e使用 /setlifeblocks reset 重置方块位置");
            }
            return;
        }

        if (!plugin.getSkinManager().isSkinLoaded(targetId)) {
            if (executor != null && !targetId.equals(executor.getUniqueId())) {
                executor.sendMessage("§e玩家 " + target.getName() + " 的皮肤正在加载中，请稍候...");
            } else {
                target.sendMessage("§e你的皮肤正在加载中，请稍候...");
                target.sendMessage("§7(如果长时间未加载完成，请重新加入服务器)");
            }

            plugin.getSkinManager().loadPlayerSkinAsync(target);

            Bukkit.getScheduler().runTaskLater(plugin, () -> {
                boolean success = plugin.getBlockManager().setLifeBlocks(target, target.getLocation());
                if (success) {
                    if (executor != null && !targetId.equals(executor.getUniqueId())) {
                        executor.sendMessage("§a已为玩家 " + target.getName() + " 生成生命方块！");
                    }
                } else {
                    if (executor != null && !targetId.equals(executor.getUniqueId())) {
                        executor.sendMessage("§c为玩家 " + target.getName() + " 生成生命方块失败！");
                    } else {
                        target.sendMessage("§c生成失败，请稍后再试或联系管理员");
                    }
                }
            }, 40L);

            return;
        }

        boolean success = plugin.getBlockManager().setLifeBlocks(target, target.getLocation());

        if (success) {
            if (executor != null && !targetId.equals(executor.getUniqueId())) {
                executor.sendMessage("§a已为玩家 " + target.getName() + " 生成生命方块！");
            }
        } else {
            if (executor != null && !targetId.equals(executor.getUniqueId())) {
                executor.sendMessage("§c为玩家 " + target.getName() + " 生成生命方块失败！");
            } else {
                target.sendMessage("§c生成失败，请确保周围有足够空间");
            }
        }
    }

    private void showHelp(Player player) {
        player.sendMessage("§a========== PlayerBlockLife 帮助 ==========");
        player.sendMessage("§e/setlifeblocks §7- 设置你的生命方块");
        player.sendMessage("§e/checklifeblocks §7- 查看你的生命方块位置");
        player.sendMessage("§e/setlifeblocks reset §7- 重置生命方块位置");

        if (player.hasPermission("playerblocklife.admin")) {
            player.sendMessage("§6管理员命令:");
            player.sendMessage("§e/setlifeblocks other <玩家> §7- 为其他玩家设置生命方块");
            player.sendMessage("§e/pblreload §7- 重载插件配置");
            player.sendMessage("§e/pbldelete <玩家> §7- 删除玩家的生命方块");
        }

        player.sendMessage("§7游戏规则:");
        player.sendMessage("§7- 每个玩家有5个生命方块");
        player.sendMessage("§7- 方块被挖光时，玩家死亡");
        player.sendMessage("§7- 方块使用玩家的皮肤作为材质");
        player.sendMessage("§a======================================");
    }
}