package de.viper.survivalplus.commands;

import de.viper.survivalplus.SurvivalPlus;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.inventory.meta.ItemMeta;

import java.util.List;

public class KitCommand implements CommandExecutor {

    private final SurvivalPlus plugin;

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

    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        if (sender instanceof Player) {
            Player player = (Player) sender;

            // Hole die Konfiguration
            FileConfiguration config = plugin.getConfig();

            // Hole die Items aus der Konfiguration
            List<String> items = config.getStringList("first-join-kit.items");

            for (String itemString : items) {
                // Teile den Item-String auf
                String[] itemParts = itemString.split(",");
                String materialName = itemParts[0].toUpperCase();
                int amount = Integer.parseInt(itemParts[1]);
                String displayName = itemParts.length > 2 ? itemParts[2] : "";

                // Erstelle das Item
                Material material = Material.getMaterial(materialName);
                if (material == null) {
                    player.sendMessage("Unbekanntes Item: " + materialName);
                    continue;  // Falls das Item ungültig ist, überspringe es
                }

                ItemStack item = new ItemStack(material, amount);
                ItemMeta meta = item.getItemMeta();

                // Setze den Display-Namen (falls vorhanden)
                if (meta != null && !displayName.isEmpty()) {
                    meta.setDisplayName(displayName);
                }

                item.setItemMeta(meta);
                player.getInventory().addItem(item); // Gib das Item an den Spieler

            }
            player.sendMessage("Du hast dein Kit erhalten!");
            return true;
        }
        return false;
    }
}
