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 final Material item;
    private final TranslatedComponent name;
    private final String id;
    private final List<T> options;
    protected T currentValue;
    private RestrictionHandler restrictionHandler;
    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;

        this.currentValue = options.getFirst();
    }

    public RestrictionHandler getRestrictionHandler() {
        return this.restrictionHandler;
    }

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

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

    public ItemStack getCurrent(Player p) {
        String value = this.options.get(this.pointer).toString();
        return ItemStack.builder(this.item)
            .customName(this.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(this.getAsString());
    }

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

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

    public String getId() {
        return this.id;
    }
}
