package me.nvus.nvus_prison_setup.Configs;

import org.bukkit.Bukkit;
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.Listener;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.plugin.java.JavaPlugin;

import java.util.HashMap;
import java.util.UUID;

public class SettingsMenu implements Listener {
    private final JavaPlugin plugin;
    private final ConfigManager configManager;
    private final HashMap<UUID, Long> lastClickTime = new HashMap<>();

    public SettingsMenu(JavaPlugin plugin, ConfigManager configManager) {
        this.plugin = plugin;
        this.configManager = configManager;
        Bukkit.getServer().getPluginManager().registerEvents(this, plugin);
    }

    public void openSettingsMenu(Player player) {
        Inventory inv = Bukkit.createInventory(null, 9, ChatColor.DARK_GREEN + "Settings Menu");

        // Retrieve configurations directly using ConfigManager
        FileConfiguration config = configManager.getConfig("config.yml");

        // Toggle items with dynamic status from config
        inv.setItem(3, createToggleItem(Material.HOPPER, "Toggle AutoPickup", config.getBoolean("AutoPickup", false)));
        inv.setItem(4, createToggleItem(Material.LEVER, "Toggle AutoSwitch", config.getBoolean("AutoSwitch", false)));
        inv.setItem(5, createItem(Material.BOOK, ChatColor.GREEN + "Reload Configs"));

        player.openInventory(inv);
    }

    private ItemStack createToggleItem(Material material, String name, boolean isEnabled) {
        ItemStack item = new ItemStack(material);
        ItemMeta meta = item.getItemMeta();
        meta.setDisplayName(ChatColor.GREEN + name + ": " + (isEnabled ? ChatColor.BLUE + "Enabled" : ChatColor.RED + "Disabled"));
        item.setItemMeta(meta);
        return item;
    }

    private ItemStack createItem(Material material, String name) {
        ItemStack item = new ItemStack(material);
        ItemMeta meta = item.getItemMeta();
        meta.setDisplayName(name);
        item.setItemMeta(meta);
        return item;
    }

    @EventHandler
    public void onInventoryClick(InventoryClickEvent event) {
        if (!event.getView().getTitle().equals(ChatColor.DARK_RED + "NVus Prison Settings")) return;
        event.setCancelled(true);

        Player player = (Player) event.getWhoClicked();
        UUID playerUUID = player.getUniqueId();

        // Implement cooldown check
        long lastClick = lastClickTime.getOrDefault(playerUUID, 0L);
        long currentTime = System.currentTimeMillis();
        if (currentTime - lastClick < 500) return; // 500 ms cooldown
        lastClickTime.put(playerUUID, currentTime);

        ItemStack clickedItem = event.getCurrentItem();
        if (clickedItem == null || !clickedItem.hasItemMeta()) return;

        String displayName = clickedItem.getItemMeta().getDisplayName();

        if (displayName.contains("Toggle AutoPickup")) {
            toggleConfigOption(player, "AutoPickup");
        } else if (displayName.contains("Toggle AutoSwitch")) {
            toggleConfigOption(player, "AutoSwitch");
        } else if (displayName.contains("Reload Configs")) {
            reloadConfigs(player);
        }
    }

    private void toggleConfigOption(Player player, String configKey) {
        FileConfiguration config = configManager.getConfig("config.yml");
        boolean currentValue = config.getBoolean(configKey, false);
        config.set(configKey, !currentValue);
        configManager.saveConfig("config.yml");
        player.sendMessage(ChatColor.GREEN + configKey + " " + (!currentValue ? "enabled" : "disabled"));
        openSettingsMenu(player); // Refresh the inventory to reflect the change
    }

    private void reloadConfigs(Player player) {
        configManager.reloadConfig("config.yml");
        configManager.reloadConfig("auto_switch.yml");
        configManager.reloadConfig("banned_items.yml");
        configManager.saveConfig("config.yml"); // Ensure any changes are saved before reload
        player.sendMessage(ChatColor.GREEN + "Configuration files reloaded.");
        openSettingsMenu(player); // Refresh the inventory
    }
}
