package de.viper.survivalplus.listeners;

import de.viper.survivalplus.SurvivalPlus;
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.block.BlockPlaceEvent;
import org.bukkit.event.entity.EntityPickupItemEvent;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;

public class BlockDetectionListener implements Listener {

    private final SurvivalPlus plugin;

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

    @EventHandler
    public void onPlayerJoin(PlayerJoinEvent event) {
        Player player = event.getPlayer();
        scanAndHandle(player, false);
    }

    @EventHandler
    public void onPickUp(EntityPickupItemEvent event) {
        if (!(event.getEntity() instanceof Player)) return;
        Player player = (Player) event.getEntity();
        scanAndHandle(player, true);
    }

    @EventHandler
    public void onInventoryClick(InventoryClickEvent event) {
        if (!(event.getWhoClicked() instanceof Player)) return;
        Player player = (Player) event.getWhoClicked();
        scanAndHandle(player, true);
    }

    @EventHandler
    public void onBlockPlace(BlockPlaceEvent event) {
        Player player = event.getPlayer();
        if (player.hasPermission("survivalplus.notify")) {
            return; // Admins dürfen Blöcke platzieren
        }
        Material material = event.getBlock().getType();
        boolean cmdAllowed = plugin.getConfig().getBoolean("blocks.command-blocks.enabled", true);
        boolean structAllowed = plugin.getConfig().getBoolean("blocks.structure-blocks.enabled", true);

        if ((!cmdAllowed && material == Material.COMMAND_BLOCK) || 
            (!structAllowed && material == Material.STRUCTURE_BLOCK)) {
            event.setCancelled(true);
            player.sendMessage(ChatColor.RED + "[SurvivalPlus] Du darfst diesen Block hier nicht platzieren!");
        }
    }

    private void scanAndHandle(Player player, boolean notifyPlayer) {
        if (player.hasPermission("survivalplus.notify")) {
            return; // Admins dürfen Blöcke behalten
        }
        Inventory inventory = player.getInventory();
        boolean hasCommandBlock = false;
        boolean hasStructureBlock = false;
        boolean cmdAllowed = plugin.getConfig().getBoolean("blocks.command-blocks.enabled", true);
        boolean structAllowed = plugin.getConfig().getBoolean("blocks.structure-blocks.enabled", true);
        boolean notifyAdmins = plugin.getConfig().getBoolean("blocks.notify-admins-on-possession", true);

        for (ItemStack item : inventory.getContents()) {
            if (item == null) continue;
            Material material = item.getType();
            if (material == Material.COMMAND_BLOCK) hasCommandBlock = true;
            if (material == Material.STRUCTURE_BLOCK) hasStructureBlock = true;
        }

        if (!cmdAllowed && hasCommandBlock) {
            inventory.remove(Material.COMMAND_BLOCK);
            if (notifyPlayer) {
                player.sendMessage(ChatColor.RED + "[SurvivalPlus] Command-Blöcke wurden aus deinem Inventar entfernt, da sie deaktiviert sind.");
            }
        }
        if (!structAllowed && hasStructureBlock) {
            inventory.remove(Material.STRUCTURE_BLOCK);
            if (notifyPlayer) {
                player.sendMessage(ChatColor.RED + "[SurvivalPlus] Structure-Blöcke wurden aus deinem Inventar entfernt, da sie deaktiviert sind.");
            }
        }

        if (notifyAdmins && (hasCommandBlock || hasStructureBlock)) {
            notifyOnlineAdmins(player.getName(), hasCommandBlock, hasStructureBlock, cmdAllowed, structAllowed);
        }
    }

    private void notifyOnlineAdmins(String playerName, boolean hadCommandBlock, boolean hadStructureBlock, boolean cmdAllowed, boolean structAllowed) {
        String baseTag = ChatColor.GRAY + "[" + ChatColor.GREEN + "SurvivalPlus" + ChatColor.GRAY + "] ";
        for (Player admin : Bukkit.getOnlinePlayers()) {
            if (admin.hasPermission("survivalplus.notify")) {
                if (hadCommandBlock && !cmdAllowed) {
                    String message = baseTag + ChatColor.RED + "Spieler " + ChatColor.YELLOW + playerName +
                            ChatColor.RED + " hatte einen Command-Block im Inventar, der entfernt wurde.";
                    admin.sendMessage(message);
                }
                if (hadStructureBlock && !structAllowed) {
                    String message = baseTag + ChatColor.RED + "Spieler " + ChatColor.YELLOW + playerName +
                            ChatColor.RED + " hatte einen Structure-Block im Inventar, der entfernt wurde.";
                    admin.sendMessage(message);
                }
            }
        }
    }
}