package de.viper.survivalplus.commands;

import de.viper.survivalplus.Manager.ShopManager;
import de.viper.survivalplus.SurvivalPlus;
import de.viper.survivalplus.gui.ShopGui; // <--- WICHTIG: Dieser Import fehlte dir
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
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;

public class ShopCommand implements CommandExecutor {

    private final ShopManager shopManager;
    private final SurvivalPlus plugin;

    public ShopCommand(SurvivalPlus plugin) {
        this.plugin = plugin;
        this.shopManager = new ShopManager(plugin);
    }

    private String getMessage(String key) {
        return plugin.getMessage("shop." + key);
    }

    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        if (!(sender instanceof Player)) {
            sender.sendMessage(getMessage("only-players"));
            return true;
        }
        Player player = (Player) sender;

        // Shop-GUI öffnen
        if (args.length == 0 || (args.length > 0 && args[0].toLowerCase().equals("gui"))) {
            ShopGui shopGui = new ShopGui(plugin, player, shopManager);
            Bukkit.getPluginManager().registerEvents(shopGui, plugin);
            player.openInventory(shopGui.getInventory());
            return true;
        }

        if (!args[0].toLowerCase().equals("add")) {
            sender.sendMessage(getMessage("unknown-subcommand"));
            return true;
        }

        // --- Neue Logik für /shop add ---

        String itemKey;
        double basePrice;
        int stock;

        // Fall 1: /shop add <Material> <Preis> <Stock> (4 Argumente)
        if (args.length >= 4) {
            String materialName = args[1];
            Material mat = Material.matchMaterial(materialName);
            
            if (mat == null) {
                sender.sendMessage(ChatColor.RED + "Das Material '" + materialName + "' wurde nicht gefunden!");
                return true;
            }
            itemKey = mat.name().toLowerCase();

            try {
                basePrice = Double.parseDouble(args[2]);
                stock = Integer.parseInt(args[3]);
            } catch (NumberFormatException e) {
                sender.sendMessage(ChatColor.RED + "Ungültige Preis oder Bestandszahl! Beispiel: /shop add Diamond 20 64");
                return true;
            }

        } 
        // Fall 2: /shop add <Preis> <Stock> (3 Argumente) -> Nimmt Item in der Hand
        else if (args.length == 3) {
            ItemStack itemInHand = player.getInventory().getItemInMainHand();
            if (itemInHand == null || itemInHand.getType() == Material.AIR) {
                sender.sendMessage(ChatColor.RED + "Du musst das Item, das du hinzufügen willst, in der Hand halten!");
                sender.sendMessage(ChatColor.GRAY + "Oder benutze: /shop add <Material> <Preis> <Bestand>");
                return true;
            }
            itemKey = itemInHand.getType().name().toLowerCase();

            try {
                basePrice = Double.parseDouble(args[1]);
                stock = Integer.parseInt(args[2]);
            } catch (NumberFormatException e) {
                sender.sendMessage(getMessage("number-error"));
                return true;
            }
        } 
        // Falsche Anzahl an Argumenten
        else {
            sender.sendMessage(ChatColor.RED + "Falsche Benutzung!");
            sender.sendMessage(ChatColor.GRAY + "Mit Item in Hand: /shop add <Preis> <Bestand>");
            sender.sendMessage(ChatColor.GRAY + "Mit Namen:      /shop add <Material> <Preis> <Bestand>");
            return true;
        }

        shopManager.addOrUpdateItem(itemKey, basePrice, stock);

        String msg = getMessage("item-added")
                .replace("{item}", itemKey)
                .replace("{price}", String.valueOf(basePrice))
                .replace("{stock}", String.valueOf(stock));
        sender.sendMessage(msg);

        return true;
    }
}