package de.viper.survivalplus.listeners;

import org.bukkit.entity.ArmorStand;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerInteractAtEntityEvent;
import org.bukkit.event.entity.EntityDamageByEntityEvent;

public class DebugArmorStandListener implements Listener {

    private static final double MAX_DISTANCE = 3.0;

    @EventHandler
    public void onPlayerInteractAtEntity(PlayerInteractAtEntityEvent event) {
        if (event.getRightClicked() instanceof ArmorStand armorStand) {
            Player player = event.getPlayer();
            if (armorStand.getScoreboardTags().contains("debugArmorStand")) {
                if (armorStand.getLocation().distance(player.getLocation()) <= MAX_DISTANCE) {
                    armorStand.remove();
                    player.sendMessage("§aDebug-ArmorStand wurde entfernt.");
                    event.setCancelled(true);
                } else {
                    player.sendMessage("§cDieser Debug-ArmorStand ist zu weit entfernt.");
                }
            }
        }
    }

    @EventHandler
    public void onEntityDamageByEntity(EntityDamageByEntityEvent event) {
        if (event.getEntity() instanceof ArmorStand armorStand) {
            if (armorStand.getScoreboardTags().contains("debugArmorStand")) {
                if (event.getDamager() instanceof Player player) {
                    if (armorStand.getLocation().distance(player.getLocation()) <= MAX_DISTANCE) {
                        armorStand.remove();
                        player.sendMessage("§aDebug-ArmorStand wurde zerstört.");
                        event.setCancelled(true);
                    } else {
                        player.sendMessage("§cDieser Debug-ArmorStand ist zu weit entfernt.");
                    }
                }
            }
        }
    }
}
