package de.viper.survivalplus.commands;

import de.viper.survivalplus.SurvivalPlus;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location; // <--- WICHTIG: Das war der Fehler
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.event.player.PlayerQuitEvent;

import java.util.HashSet;
import java.util.Set;
import java.util.UUID;

public class AdminToolsCommand implements CommandExecutor, Listener {

    private final SurvivalPlus plugin;
    private final Set<UUID> vanishedPlayers = new HashSet<>();
    private final Set<UUID> frozenPlayers = new HashSet<>();

    public AdminToolsCommand(SurvivalPlus plugin) {
        this.plugin = plugin;
        plugin.getServer().getPluginManager().registerEvents(this, plugin);
    }

    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        if (!(sender instanceof Player)) {
            sender.sendMessage("Nur Spieler können diesen Befehl nutzen!");
            return true;
        }

        Player player = (Player) sender;
        FileConfiguration lang = plugin.getLangConfig();

        // --- VANISH ---
        if (command.getName().equalsIgnoreCase("vanish")) {
            if (!player.hasPermission("survivalplus.vanish")) {
                player.sendMessage(ChatColor.RED + "Keine Berechtigung!");
                return true;
            }

            if (vanishedPlayers.contains(player.getUniqueId())) {
                // Ent-vanish
                for (Player all : Bukkit.getOnlinePlayers()) {
                    all.showPlayer(plugin, player);
                }
                player.setPlayerListName(player.getName()); // Name wieder in Tab-Liste anzeigen
                vanishedPlayers.remove(player.getUniqueId());
                player.sendMessage(ChatColor.GREEN + "Du bist nun sichtbar.");
            } else {
                // Vanish
                for (Player all : Bukkit.getOnlinePlayers()) {
                    all.hidePlayer(plugin, player);
                }
                vanishedPlayers.add(player.getUniqueId());
                player.sendMessage(ChatColor.GREEN + "Du bist nun unsichtbar.");
            }
            return true;
        }

        // --- FREEZE ---
        if (command.getName().equalsIgnoreCase("freeze")) {
            if (!player.hasPermission("survivalplus.freeze")) {
                player.sendMessage(ChatColor.RED + "Keine Berechtigung!");
                return true;
            }

            if (args.length == 0) {
                player.sendMessage(ChatColor.RED + "Verwendung: /freeze <Spieler>");
                return true;
            }

            Player target = Bukkit.getPlayer(args[0]);
            if (target == null) {
                player.sendMessage(ChatColor.RED + "Spieler nicht gefunden!");
                return true;
            }

            if (frozenPlayers.contains(target.getUniqueId())) {
                // Ent-freezen
                frozenPlayers.remove(target.getUniqueId());
                player.sendMessage(ChatColor.GREEN + target.getName() + " wurde aufgetaut.");
                target.sendMessage(ChatColor.GREEN + "Du bist nicht mehr eingefroren.");
            } else {
                // Einfrieren
                frozenPlayers.add(target.getUniqueId());
                player.sendMessage(ChatColor.GREEN + target.getName() + " wurde eingefroren.");
                target.sendMessage(ChatColor.RED + "Du wurdest von einem Admin eingefroren! " + ChatColor.BOLD + "Keine Bewegung möglich!");
            }
            return true;
        }

        return false;
    }

    // --- EVENTS ---

    // Verhindert Bewegung wenn eingefroren
    @EventHandler
    public void onPlayerMove(PlayerMoveEvent event) {
        Player player = event.getPlayer();
        // Kopf drehen (YAW/PITCH) ist okay, Blocken wenn Position (X/Y/Z) ändert
        if (frozenPlayers.contains(player.getUniqueId())) {
            Location from = event.getFrom();
            Location to = event.getTo();
            // Wenn die Koordinaten sich ändern
            if (to.getX() != from.getX() || to.getY() != from.getY() || to.getZ() != from.getZ()) {
                event.setCancelled(true);
                // Optional: Kleine Meldung beim Versuch zu laufen
                // player.sendMessage(ChatColor.RED + "Du bist eingefroren!");
            }
        }
    }

    // Wenn ein Spieler joint: Hide vanished players for him
    @EventHandler
    public void onPlayerJoin(PlayerJoinEvent event) {
        Player joiner = event.getPlayer();
        for (UUID vanishedUUID : vanishedPlayers) {
            Player vanishedPlayer = Bukkit.getPlayer(vanishedUUID);
            if (vanishedPlayer != null && vanishedPlayer.isOnline()) {
                joiner.hidePlayer(plugin, vanishedPlayer);
            }
        }
    }

    // Cleanup wenn ein Spieler geht
    @EventHandler
    public void onPlayerQuit(PlayerQuitEvent event) {
        vanishedPlayers.remove(event.getPlayer().getUniqueId());
        frozenPlayers.remove(event.getPlayer().getUniqueId());
    }
}