package eu.mhsl.minenet.minigames.instance.room.inventory;

import eu.mhsl.minenet.minigames.instance.game.GameList;
import eu.mhsl.minenet.minigames.instance.game.GameType;
import eu.mhsl.minenet.minigames.instance.game.stateless.config.GameConfigurationInventory;
import eu.mhsl.minenet.minigames.instance.game.stateless.config.GameFactory;
import eu.mhsl.minenet.minigames.instance.room.Room;
import eu.mhsl.minenet.minigames.message.component.TranslatedComponent;
import eu.mhsl.minenet.minigames.shared.inventory.InteractableInventory;
import eu.mhsl.minenet.minigames.util.InventoryItemAlignment;
import net.minestom.server.entity.Player;
import net.minestom.server.inventory.InventoryType;
import net.minestom.server.item.ItemStack;
import net.minestom.server.item.Material;

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

public class MinigameSelectInventory extends InteractableInventory {
    final private Room room;
    final private Player p;
    public MinigameSelectInventory(Room room, Player p) {
        super(InventoryType.CHEST_6_ROW, TranslatedComponent.assemble("room#invTitle", p));
        this.room = room;
        this.p = p;

        InventoryItemAlignment itemAlignment = new InventoryItemAlignment(GameType.values().length, 1);
        for (GameType type : GameType.values()) {
            setClickableItem(
                    ItemStack.builder(type.getIcon())
                            .customName(type.getTitle().getAssembled(p))
                            .lore(type.getDescription().addWrap().getWrappedAssembled(p))
                            .build(),
                    itemAlignment.next().get(),
                    itemClick -> drawGames(type)
            );
        }

        for(int i = 9; i <= 17; i++) {
            setDummyItem(Material.CYAN_STAINED_GLASS_PANE, i);
        }


    }

    private void drawGames(GameType type) {
        for(int i = 18; i <= 53; i++) {
            setDummyItem(Material.AIR, i);
        }
        int offset = 18;

        List<GameList> games = Arrays.stream(GameList.values()).filter(gameList -> gameList.getType().equals(type)).toList();

        InventoryItemAlignment itemAlignment = new InventoryItemAlignment(games.size(), 4);
        for (GameList game : games) {
            GameFactory gameFactory = game.getFactory();

            setClickableItem(
                    ItemStack.builder(gameFactory.symbol())
                            .customName(gameFactory.name().getAssembled(p))
                            .lore(gameFactory.description().addWrap().getWrappedAssembled(p))
                            .build(),
                    offset + itemAlignment.next().get(),
                    itemClick -> itemClick.getPlayer().openInventory(new GameConfigurationInventory(room, itemClick.getPlayer(), gameFactory))
            );
        }
    }
}
