package de.viper.survivalplus.listeners;

import de.viper.survivalplus.Manager.Warp;
import de.viper.survivalplus.Manager.WarpManager;
import de.viper.survivalplus.SurvivalPlus;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryDragEvent;
import org.bukkit.inventory.ItemStack;

public class WarpInventoryListener implements Listener {

    private final SurvivalPlus plugin;
    private final WarpManager warpManager;
    private final String inventoryTitle;

    public WarpInventoryListener(SurvivalPlus plugin, WarpManager warpManager) {
        this.plugin = plugin;
        this.warpManager = warpManager;
        this.inventoryTitle = ChatColor.GOLD + "Player Warps";
    }

    @EventHandler
    public void onInventoryClick(InventoryClickEvent event) {
        if (event.getView() == null) return;
        if (event.getView().getTitle() == null) return;
        if (!event.getView().getTitle().equalsIgnoreCase(inventoryTitle)) return;

        // Verhindert Verschieben und Entnehmen
        event.setCancelled(true);

        ItemStack clickedItem = event.getCurrentItem();
        if (clickedItem == null || clickedItem.getType().isAir()) return;
        if (!(event.getWhoClicked() instanceof Player)) return;
        Player player = (Player) event.getWhoClicked();

        if (!clickedItem.hasItemMeta() || !clickedItem.getItemMeta().hasDisplayName()) return;
        String warpName = ChatColor.stripColor(clickedItem.getItemMeta().getDisplayName());

        Warp warpFound = null;
        for (Warp warp : warpManager.getWarps().values()) {
            if (warp.getName().equalsIgnoreCase(warpName)) {
                warpFound = warp;
                break;
            }
        }

        if (warpFound == null) {
            player.sendMessage(ChatColor.RED + "Warp nicht gefunden.");
            player.closeInventory();
            return;
        }

        if (Bukkit.getWorld(warpFound.getWorldName()) == null) {
            player.sendMessage(ChatColor.RED + "Die Welt dieses Warps existiert nicht.");
            player.closeInventory();
            return;
        }

        player.teleport(
                Bukkit.getWorld(warpFound.getWorldName())
                        .getBlockAt((int) warpFound.getX(), (int) warpFound.getY(), (int) warpFound.getZ())
                        .getLocation().add(0.5, 1.0, 0.5) // leicht über dem Block für sicheren Spawn
        );
        player.sendMessage(ChatColor.GREEN + "Teleportiert zu Warp: " + warpName);
        player.closeInventory();
    }

    @EventHandler
    public void onInventoryDrag(InventoryDragEvent event) {
        if (event.getView() == null) return;
        if (event.getView().getTitle() == null) return;
        if (!event.getView().getTitle().equalsIgnoreCase(inventoryTitle)) return;
        event.setCancelled(true);
    }
}
