package lnmpro.Listeners;

import lnmpro.Main;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.metadata.FixedMetadataValue;
import net.md_5.bungee.api.chat.ClickEvent;
import net.md_5.bungee.api.chat.ComponentBuilder;
import net.md_5.bungee.api.chat.TextComponent;
import net.md_5.bungee.api.chat.HoverEvent;
import org.bukkit.entity.Player;

import java.util.UUID;

public class DuelRequestListener implements Listener {
    private final Main plugin;

    public DuelRequestListener(Main plugin) {
        this.plugin = plugin;
    }

    @EventHandler
    public void onInventoryClick(InventoryClickEvent event) {
        if (!(event.getWhoClicked() instanceof Player player)) return;

        if (event.getView().getTitle().equals("§9\uD83D\uDDE1 Játékmód Választás")) {
            event.setCancelled(true);
        }
    }

    @EventHandler
    public void onDuelRequest(InventoryClickEvent e) {
        Player player = (Player) e.getWhoClicked();

        if (!e.getView().getTitle().equals("§9\uD83D\uDDE1 Játékmód Választás")) return;

        if (e.getClick() != ClickType.LEFT) return;

        ItemStack clickedItem = e.getCurrentItem();
        if (clickedItem == null || clickedItem.getType() == Material.AIR) return;

        if (clickedItem.getType() == Material.RED_STAINED_GLASS_PANE || clickedItem.getType() == Material.BLUE_STAINED_GLASS_PANE)
            return;

        if (player.hasMetadata("DuelRequest")) {
            e.setCancelled(true);

            UUID targetUUID = UUID.fromString(player.getMetadata("DuelRequest").get(0).asString());
            Player target = Bukkit.getPlayer(targetUUID);

            if (target == null) {
                player.sendMessage("§cA meghívott játékos nincs jelen a szerveren.");
                player.removeMetadata("DuelRequest", plugin);
                return;
            }

            player.removeMetadata("DuelRequest", plugin);

            TextComponent accept = new TextComponent("§a[✔ Elfogadás]");
            accept.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/acceptduel"));
            accept.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT,
                    new ComponentBuilder("§a✔ Nyomj rá az elfogadáshoz!").create()));

            TextComponent decline = new TextComponent("§c[❌ Elutasítás]");
            decline.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/denyduel"));
            decline.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT,
                    new ComponentBuilder("§c❌ Nyomj rá az elutasításhoz!").create()));

            TextComponent info = new TextComponent("§7ℹ");
            info.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT,
                    new ComponentBuilder("§fJátékmód: " + ChatColor.stripColor(clickedItem.getItemMeta().getDisplayName().toLowerCase())).create()));

            TextComponent fullMessage = new TextComponent();
            fullMessage.addExtra(accept);
            fullMessage.addExtra(" ");
            fullMessage.addExtra(decline);
            fullMessage.addExtra(" ");
            fullMessage.addExtra(info);

            target.sendMessage("§8-----------------------------------------------------");
            target.sendMessage("§c\uD83D\uDDE1 §e" + player.getName() + " §7küldött egy " + clickedItem.getItemMeta().getDisplayName().toLowerCase() + " §7párbaj kérést!");
            target.sendMessage(" ");
            target.spigot().sendMessage(fullMessage);
            target.sendMessage("§8-----------------------------------------------------");

            target.setMetadata("DueledBy", new FixedMetadataValue(plugin, player.getUniqueId().toString()));
            target.setMetadata("DueledWithGamemode", new FixedMetadataValue(plugin, ChatColor.stripColor(clickedItem.getItemMeta().getDisplayName().toLowerCase())));

            player.closeInventory();
        }
    }
}