package de.viper.survivalplus.listeners;

import de.viper.survivalplus.SurvivalPlus;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityPickupItemEvent; // NEU: EntityPickupItemEvent
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.event.player.PlayerTeleportEvent;
import com.comphenix.protocol.ProtocolLibrary;
import com.comphenix.protocol.ProtocolManager;
import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.events.PacketContainer;

public class VanishListener implements Listener {
    private final SurvivalPlus plugin;

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

    @EventHandler(priority = EventPriority.HIGHEST)
    public void onPlayerJoin(PlayerJoinEvent event) {
        Player player = event.getPlayer();
        if (plugin.isVanished(player.getUniqueId())) {
            // Join Message ausblenden
            event.setJoinMessage(null);
            
            // Silent Join (Teleport-Effekt) via ProtocolLib
            if (player.hasPermission("survivalplus.vanish.silent")) {
                hideJoinParticles(player);
            }
        }
    }

    @EventHandler(priority = EventPriority.HIGHEST)
    public void onPlayerQuit(PlayerQuitEvent event) {
        Player player = event.getPlayer();
        if (plugin.isVanished(player.getUniqueId())) {
            // Quit Message ausblenden
            event.setQuitMessage(null);
        }
    }

    @EventHandler
    public void onTeleport(PlayerTeleportEvent event) {
        Player player = event.getPlayer();
        // Silent Teleport verhindert den Endermann-Teleportations-Effekt
        if (plugin.isVanished(player.getUniqueId()) && player.hasPermission("survivalplus.vanish.silent")) {
            // In 4.8.0 ist das stumme Teleportieren komplex.
            // Wir lassen es hier beim Abschalten der Join-Partikel bewenden.
        }
    }

    @EventHandler
    public void onPickup(EntityPickupItemEvent event) {
        if (!(event.getEntity() instanceof Player)) return; // Vorsichtshalber prüfen

        Player player = (Player) event.getEntity();
        if (plugin.isVanished(player.getUniqueId()) && player.hasPermission("survivalplus.vanish.no-pickup")) {
            event.setCancelled(true); // Item nicht aufheben
        }
    }

    private void hideJoinParticles(Player player) {
        if (!plugin.getServer().getPluginManager().isPluginEnabled("ProtocolLib")) return;
        try {
            // Direkter Cast auf ProtocolManager ist in 4.8.0 sicherer
            ProtocolManager manager = (ProtocolManager) plugin.getServer().getPluginManager().getPlugin("ProtocolLib");
            if (manager == null) return;

            // Packet Container erstellen
            PacketContainer packet = manager.createPacket(PacketType.Play.Server.ENTITY_STATUS);
            packet.getIntegers().write(0, player.getEntityId());
            packet.getBytes().write(0, (byte) 0x20); // 32 = Invisibel/Unsichtbar Status Client-Side
            
            for (Player online : plugin.getServer().getOnlinePlayers()) {
                if (online.equals(player)) continue;
                manager.sendServerPacket(online, packet);
            }
        } catch (Exception e) {
            // Ignorieren Fehler bei ProtocolLib
        }
    }
}