package me.nvus.nvus_prison_setup.Listeners;

import me.nvus.nvus_prison_setup.Configs.ConfigManager;
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.block.Action;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;
import java.util.List;
import java.util.stream.Collectors;

public class ToolSwitchListener implements Listener {
    private ConfigManager configManager;

    public ToolSwitchListener(ConfigManager configManager) {
        this.configManager = configManager;
    }

    @EventHandler
    public void onPlayerInteract(PlayerInteractEvent event) {
        if (event.getAction() != Action.LEFT_CLICK_BLOCK) return;

        Player player = event.getPlayer();
        FileConfiguration config = configManager.getConfig("config.yml");
        if (!config.getBoolean("AutoSwitch", true)) return;
        if (!player.hasPermission("nvus.prisoner")) return;

        FileConfiguration autoSwitchConfig = configManager.getConfig("auto_switch.yml");
        Material blockType = event.getClickedBlock().getType();

        // Determine the correct tool based on block type, pulled from auto_switch.yml
        Material bestTool = determineBestTool(blockType, player, autoSwitchConfig);
        if (bestTool != null) {
            switchToTool(player, bestTool);
        }
    }

    private Material determineBestTool(Material blockType, Player player, FileConfiguration autoSwitchConfig) {
        List<String> autoSwitchTools = autoSwitchConfig.getStringList("AutoSwitchTools");
        List<Material> pickaxeMaterials = convertStringListToMaterial(autoSwitchConfig.getStringList("PickaxeMaterials"));
        List<Material> axeMaterials = convertStringListToMaterial(autoSwitchConfig.getStringList("AxeMaterials"));
        List<Material> shovelMaterials = convertStringListToMaterial(autoSwitchConfig.getStringList("ShovelMaterials"));

        Material requiredTool = null;
        if (pickaxeMaterials.contains(blockType)) {
            requiredTool = Material.valueOf("IRON_PICKAXE");
        } else if (axeMaterials.contains(blockType)) {
            requiredTool = Material.valueOf("IRON_AXE");
        } else if (shovelMaterials.contains(blockType)) {
            requiredTool = Material.valueOf("IRON_SHOVEL");
        }

        // Ensure the tool is in the list of tools that should auto switch
        if (requiredTool != null && autoSwitchTools.contains(requiredTool.name())) {
            return findBestToolInInventory(requiredTool, player);
        }

        return null;
    }

    private Material findBestToolInInventory(Material toolType, Player player) {
        PlayerInventory inventory = player.getInventory();
        for (ItemStack item : inventory.getContents()) {
            if (item != null && item.getType() == toolType) {
                return toolType; // Further logic can be added to select the best tool if multiple are found
            }
        }
        return null;
    }

    private List<Material> convertStringListToMaterial(List<String> stringList) {
        return stringList.stream().map(Material::valueOf).collect(Collectors.toList());
    }

    private void switchToTool(Player player, Material tool) {
        PlayerInventory inventory = player.getInventory();
        int toolSlot = inventory.first(tool);
        if (toolSlot >= 0 && toolSlot < 9) { // If the tool is in the quickbar
            inventory.setHeldItemSlot(toolSlot);
        }
    }
}
