package eu.mhsl.minenet.minigames.instance.game.stateless.config;

import eu.mhsl.minenet.minigames.instance.game.stateless.config.restriction.RestrictionHandler;
import eu.mhsl.minenet.minigames.message.component.TranslatedComponent;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import net.minestom.server.entity.Player;
import net.minestom.server.item.ItemStack;
import net.minestom.server.item.Material;

import java.util.List;
import java.util.Objects;

public abstract class Option<T> {
    private RestrictionHandler restrictionHandler;
    private final Material item;
    private final TranslatedComponent name;
    private final String id;
    protected T currentValue;
    private final List<T> options;
    private int pointer = 0;

    public Option(String id, Material item, TranslatedComponent name, List<T> options) {
        this.id = id;
        this.item = item;
        this.name = name;
        this.options = options;

        currentValue = options.getFirst();
    }

    public void setRestrictionHandler(RestrictionHandler restrictionHandler) {
        this.restrictionHandler = restrictionHandler;
    }

    public RestrictionHandler getRestrictionHandler() {
        return restrictionHandler;
    }


    public ItemStack getNext(Player p) {
        if(++pointer >= options.size()) pointer = 0;
        currentValue = options.get(pointer);
        return getCurrent(p);
    }

    public ItemStack getCurrent(Player p) {
        String value = options.get(pointer).toString();
        return ItemStack.builder(item)
                .customName(name.getAssembled(p))
                .lore(TranslatedComponent.byId("optionCommon#value").setColor(NamedTextColor.GOLD).getAssembled(p)
                        .append(Component.text(": ")).append(Component.text(value)))
                .build();
    }

    public int getAsInt() {
        return Integer.parseInt(getAsString());
    }

    public boolean getAsBoolean() {
        return Objects.equals(getAsString(), "true") || Objects.equals(getAsString(), "1");
    }

    public String getAsString() {
        return currentValue.toString();
    }

    public String getId() {
        return id;
    }
}
