package de.viper.survivalplus.commands;

import de.viper.survivalplus.SurvivalPlus;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.SkullMeta;

public class HeadCommand implements CommandExecutor {
    private SurvivalPlus plugin;

    public HeadCommand(SurvivalPlus plugin) {
        this.plugin = plugin;
    }

    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
        if (!(sender instanceof Player)) {
            sender.sendMessage("Nur Spieler können diesen Befehl nutzen.");
            return true;
        }
        Player player = (Player) sender;

        if (args.length == 0) {
            player.sendMessage(plugin.getMessage("commands.player-only"));
            return true;
        }

        if (!player.hasPermission("survivalplus.head")) {
            player.sendMessage(ChatColor.RED + "Keine Berechtigung!");
            return true;
        }

        String targetName = args[0];
        OfflinePlayer target = Bukkit.getOfflinePlayer(targetName);

        if (target == null) {
            player.sendMessage(ChatColor.RED + "Spieler '" + targetName + "' wurde nie gefunden.");
            return true;
        }

        ItemStack head = new ItemStack(Material.PLAYER_HEAD);
        SkullMeta meta = (SkullMeta) head.getItemMeta();
        meta.setOwningPlayer(target);
        meta.setDisplayName(ChatColor.YELLOW + "Kopf von " + targetName);
        head.setItemMeta(meta);

        player.getInventory().addItem(head);
        player.sendMessage(ChatColor.GREEN + "Du hast den Kopf von " + targetName + " erhalten!");
        return true;
    }
}