package me.nvus.nvus_prison_setup.Listeners;

import me.nvus.nvus_prison_setup.PrisonSetup;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.inventory.ItemStack;

public class BlockListener implements Listener {

    private final PrisonSetup plugin;

    public BlockListener(PrisonSetup plugin) {
        this.plugin = plugin;
    }

    @EventHandler
    public void onBlockBreak(BlockBreakEvent event) {
        Player player = event.getPlayer();
        Block block = event.getBlock();

        // Check if the player is a prisoner and auto-pickup is enabled
        if (player.hasPermission("nvus.prisoner") && plugin.getConfigManager().getConfig("config.yml").getBoolean("AutoPickup")) {
            ItemStack itemStack = new ItemStack(block.getType());
            if (player.getInventory().addItem(itemStack).isEmpty()) {
                // Inventory has enough space, remove the dropped item
                event.setDropItems(false);
            } else {
                // Inventory is full, drop the item on the ground
                block.getWorld().dropItemNaturally(block.getLocation(), itemStack);
                player.sendMessage("Your inventory is currently full. The resource has been dropped on the ground!");
            }
        }
    }
}
