package me.nvus.nvus_prison_setup.Listeners;

import me.nvus.nvus_prison_setup.PrisonSetup;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.event.player.PlayerItemHeldEvent;
import org.bukkit.inventory.ItemStack;

import java.util.List;

public class PlayerItems implements Listener {

    private final PrisonSetup plugin;

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

    @EventHandler(priority = EventPriority.LOWEST)
    public void onPlayerItemHeld(PlayerItemHeldEvent event) {
        Player player = event.getPlayer();
        ItemStack item = player.getInventory().getItem(event.getNewSlot());

        if (item != null && isBannedItem(item.getType())) {
            if (player.hasPermission("nvus.prisoner")) {
                event.setCancelled(true);
                player.sendMessage(ChatColor.translateAlternateColorCodes('&',"&c&lSorry inmate! &cYou're a &6&lprisoner &cand cannot use this tool!"));
            }
        }
    }


    @EventHandler(priority = EventPriority.LOWEST)
    public void onInventoryClick(InventoryClickEvent event) {
        Player player = (Player) event.getWhoClicked();
        ItemStack clickedItem = event.getCurrentItem();

        // Check if the clicked item is a banned item
        if (clickedItem != null && isBannedItem(clickedItem.getType())) {
            // Check if the player is a prisoner
            if (player.hasPermission("nvus.prisoner")) {
                // Cancel the event to prevent interaction with banned items
                event.setCancelled(true);
                player.sendMessage(ChatColor.translateAlternateColorCodes('&',"&c&lSorry inmate! &cYou're a &6&lprisoner &cand cannot use this tool!"));
            }
        }
    }

    private boolean isBannedItem(Material itemType) {
        FileConfiguration config = plugin.getBannedItemsConfig().getConfig();
        List<String> bannedItems = config.getStringList("BannedItems");
        return bannedItems.contains(itemType.toString());
    }




    /*
    @EventHandler(priority = EventPriority.LOWEST)
    public void onInventoryClick(InventoryClickEvent event) {
        if (event.getClickedInventory() != null && event.getClickedInventory().getType() == InventoryType.PLAYER) {
            if (event.getSlotType() == InventoryType.SlotType.QUICKBAR) {
                ItemStack item = event.getCurrentItem();
                Player player = (Player) event.getWhoClicked();

                // Check if the clicked item is a banned item
                if (item != null && isBannedItem(item.getType())) {
                    // Check if the player is a prisoner
                    if (player.hasPermission("nvus.prisoner")) {
                        // Cancel the event to prevent the banned item from being moved to the quickbar
                        event.setCancelled(true);
                        player.sendMessage(ChatColor.translateAlternateColorCodes('&',"&c&lSorry inmate! &cYou're a prisoner and cannot use this tool!"));
                    }
                }
            }
        }
    }


    private boolean isBannedItem(Material itemType) {
        FileConfiguration config = plugin.getBannedItemsConfig().getConfig();
        List<String> bannedItems = config.getStringList("BannedItems");
        return bannedItems.contains(itemType.toString());
    }
    */

}
