package moe.oko.opennaw.util;

import moe.oko.opennaw.OpenNAW;
import moe.oko.opennaw.model.Nation;
import net.kyori.adventure.text.Component;
import org.apache.commons.lang.enums.EnumUtils;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.ItemStack;
import xyz.janboerman.guilib.api.menu.CycleButton;
import xyz.janboerman.guilib.api.menu.ItemButton;
import xyz.janboerman.guilib.api.menu.MenuHolder;

import java.util.ArrayList;
import java.util.Collection;

public class GuiHelper {
    public static MenuHolder<OpenNAW> getJoinMenu() {
        var menu = new MenuHolder<>(OpenNAW.getInstance(), 9, "Join a Nation!");

        int i = 9;
        int s = 0;
        for(var nation : OpenNAW.getInstance().getNationHandler().getNationList()) {
            var is = new ItemStack(Material.getMaterial(nation.getColour() + "_WOOL"));
            var meta = is.getItemMeta();
            meta.displayName(Component.text(ChatColor.valueOf(nation.getColour()) + nation.getName()));
            is.setItemMeta(meta);
            menu.setButton(s, new ItemButton<>(is) {
                @Override
                public void onClick(MenuHolder<?> menu, InventoryClickEvent event) {
                    if(event.getClick().equals(ClickType.LEFT)) {
                        //Left click, run join logic.
                        var viewer = event.getViewers().get(0);
                        if(!(viewer instanceof Player)) { return; } //fugged
                        var player = (Player) viewer;

                        nation.getPlayerMap().put(player.getUniqueId(), player);
                        OpenNAW.getInstance().getGroupHandler().addPlayerToGroup(player, nation.getGroup());

                        player.sendMessage(ChatColor.GREEN + "Joined " + nation.getName() + " successfully.");
                        event.getView().close(); //Close inv.
                    }
                }
            });
            s++;
        }
        return menu;
    }

    public static MenuHolder<OpenNAW> getNationModMenu(Nation nation) {
        var menu = new MenuHolder<>(OpenNAW.getInstance(), 9, nation.getName() + " Modification");

        var colour = new ItemStack(Material.valueOf(nation.getColour() + "_WOOL"));
        var meta = colour.getItemMeta();
        meta.displayName(Component.text(colour.getType().name().replace("_WOOL", "").toUpperCase()));
        colour.setItemMeta(meta);
        var woolCollection = new ArrayList<>();
        for(var mat : Material.values()) {
            var material = (Material) mat;
            if (material.name().contains("_WOOL")) {
                woolCollection.add(new ItemStack(material));
            }
        }
        /*
        Colour Selection Button
         */
        menu.setButton(0, new CycleButton<>(colour, woolCollection, 0) {
            @Override
            public void updateStateForwards(MenuHolder<?> menu, InventoryClickEvent event) {
                incrementCursor();
                var i = getCursor();
                var player = (Player) event.getViewers().get(0);
                var is = (ItemStack) woolCollection.get(i);
                var meta = is.getItemMeta();
                meta.displayName(Component.text(is.getType().name().replace("_WOOL", "").toUpperCase()));
                is.setItemMeta(meta);
                setIcon(is);
                nation.setColour(this.getIcon().getType().name().replace("_WOOL", ""));
                player.sendMessage(ChatColor.GREEN + "Colour for this nation was changed to " + nation.getColour());
            }

            @Override
            public void updateStateBackwards(MenuHolder<?> menu, InventoryClickEvent event) {
                decrementCursor();
                var i = getCursor();
                var player = (Player) event.getViewers().get(0);
                var is = (ItemStack) woolCollection.get(i);
                var meta = is.getItemMeta();
                meta.displayName(Component.text(is.getType().name().replace("_WOOL", "").toUpperCase()));
                is.setItemMeta(meta);
                setIcon(is);
                nation.setColour(this.getIcon().getType().name().replace("_WOOL", ""));
                player.sendMessage(ChatColor.GREEN + "Colour for this nation was changed to " + nation.getColour());
            }
        });

        /**
         * Spawn Setting
         */
        var is = new ItemStack(Material.BARRIER);
        var meta1 = is.getItemMeta();
        meta1.displayName(Component.text("Spawn Location"));
        var lore = new ArrayList<Component>();
        lore.add(Component.text(ChatColor.GRAY + "Click to set your current location as the spawn for this nation."));
        meta1.lore(lore);
        is.setItemMeta(meta1);
        menu.setButton(1, new ItemButton<>(is) {
            @Override
           public void onClick(MenuHolder<?> menu, InventoryClickEvent event) {
                var player = (Player) event.getViewers().get(0);
                nation.setSpawn(player.getLocation());
                player.sendMessage(ChatColor.GREEN + "Successfully set new spawn location for " + nation.getName() + " to " + player.getLocation());
                player.closeInventory();
            }
        });
        return menu;
    }
}
