package de.hessj.environmentex;

import java.util.Arrays;
import java.util.List;

import org.bukkit.Material;
import org.bukkit.NamespacedKey;
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.event.server.ServerLoadEvent;
import org.bukkit.inventory.InventoryView;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.MenuType;
import org.bukkit.inventory.ShapedRecipe;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.components.CustomModelDataComponent;
import org.bukkit.persistence.PersistentDataType;

import net.kyori.adventure.text.Component;
import net.md_5.bungee.api.ChatColor;

public class WorkbenchToGo implements Listener {
    private Component nameWorkbench = Component.text(ChatColor.WHITE + "Portable Werkbank");
    private NamespacedKey nskWorkbench = new NamespacedKey(App.main, "R3S_WORKBENCH");

    @EventHandler
    public void onLoad(ServerLoadEvent e) {
        ItemStack isWorkbench = new ItemStack(Material.BARRIER);
        ItemMeta mWorkbench = isWorkbench.getItemMeta();
        mWorkbench.getPersistentDataContainer().set(nskWorkbench, PersistentDataType.STRING, "R3S_WORKBENCH");
        mWorkbench.displayName(nameWorkbench);
        mWorkbench.setCustomModelData(1000020);
        CustomModelDataComponent cmdc = mWorkbench.getCustomModelDataComponent();
        List<String> list = Arrays.asList("crafting_table_to_go");
        cmdc.setStrings(list);
        mWorkbench.setCustomModelDataComponent(cmdc);
        isWorkbench.setItemMeta(mWorkbench);
        ShapedRecipe srWorkbench = new ShapedRecipe(new NamespacedKey(App.main, "R3S_SR_Workbench"), isWorkbench);
        srWorkbench.shape("DDD", "DWD", "DDD");
        srWorkbench.setIngredient('D', Material.DIAMOND);
        srWorkbench.setIngredient('W', Material.CRAFTING_TABLE);
        App.main.getServer().addRecipe(srWorkbench);
        App.main.recipeKeys.add(srWorkbench.getKey());
    }

    @EventHandler
    public void onInteract(PlayerInteractEvent e) {
        if (e.getPlayer().getInventory().getItemInMainHand() != null
                && e.getPlayer().getInventory().getItemInMainHand().getType().equals(Material.BARRIER)) {
            if (e.getPlayer().getInventory().getItemInMainHand().hasItemMeta()
                    && e.getPlayer().getInventory().getItemInMainHand().getItemMeta()
                            .getPersistentDataContainer() != null
                    && e.getPlayer().getInventory().getItemInMainHand().getItemMeta().getPersistentDataContainer()
                            .has(nskWorkbench, PersistentDataType.STRING)) {

                if (e.getAction() == Action.LEFT_CLICK_AIR || e.getAction() == Action.LEFT_CLICK_BLOCK
                        || e.getAction() == Action.PHYSICAL
                        || (e.getClickedBlock() != null && App.helper.isInteractable(e.getClickedBlock()))) {
                    return;
                }
                e.setCancelled(true);
                MenuType.Typed<InventoryView, ?> craftingMenuType = MenuType.CRAFTING;
                InventoryView craftingView = craftingMenuType.builder()
                        .title(Component.text("Custom Crafting"))
                        .build(e.getPlayer());
                e.getPlayer().openInventory(craftingView);
            }
        }
    }
}