package de.viper.survivalplus.listeners;

import de.viper.survivalplus.SurvivalPlus;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.entity.EntityDamageByEntityEvent;

public class SpawnProtectionListener implements Listener {

    private final SurvivalPlus plugin;
    private final int radius;
    private final boolean protectBlockBreak;
    private final boolean protectBlockPlace;
    private final boolean protectPvP;
    private final String bypassPermission;

    public SpawnProtectionListener(SurvivalPlus plugin) {
        this.plugin = plugin;
        this.radius = plugin.getConfig().getInt("spawnprotection.radius", 20);
        this.protectBlockBreak = plugin.getConfig().getBoolean("spawnprotection.protect-block-break", true);
        this.protectBlockPlace = plugin.getConfig().getBoolean("spawnprotection.protect-block-place", true);
        this.protectPvP = plugin.getConfig().getBoolean("spawnprotection.protect-pvp", true);
        this.bypassPermission = plugin.getConfig().getString("spawnprotection.bypass-permission", "survivalplus.spawnprotection.bypass");
    }

    private boolean isInSpawnProtection(Location loc) {
        if (loc == null) return false;
        Location spawn = loc.getWorld().getSpawnLocation();
        return spawn.distance(loc) <= radius;
    }

    private boolean canBypass(Player player) {
        // Nur OPs oder Spieler mit spezieller Berechtigung dürfen bypassen
        return player.isOp() || player.hasPermission(bypassPermission);
    }

    @EventHandler
    public void onBlockBreak(BlockBreakEvent event) {
        if (!protectBlockBreak) return;
        Player player = event.getPlayer();
        if (isInSpawnProtection(event.getBlock().getLocation()) && !canBypass(player)) {
            player.sendMessage(plugin.getMessage("spawnprotection.blockbreak-denied"));
            event.setCancelled(true);
        }
    }

    @EventHandler
    public void onBlockPlace(BlockPlaceEvent event) {
        if (!protectBlockPlace) return;
        Player player = event.getPlayer();
        if (isInSpawnProtection(event.getBlock().getLocation()) && !canBypass(player)) {
            player.sendMessage(plugin.getMessage("spawnprotection.blockplace-denied"));
            event.setCancelled(true);
        }
    }

    @EventHandler
    public void onEntityDamageByEntity(EntityDamageByEntityEvent event) {
        if (!protectPvP) return;
        if (!(event.getEntity() instanceof Player)) return;
        if (!(event.getDamager() instanceof Player)) return;

        Player damaged = (Player) event.getEntity();
        Player damager = (Player) event.getDamager();

        if (isInSpawnProtection(damaged.getLocation()) && !canBypass(damager)) {
            damager.sendMessage(plugin.getMessage("spawnprotection.pvp-denied"));
            event.setCancelled(true);
        }
    }
}
