package eu.mhsl.minenet.minigames.message;

import eu.mhsl.minenet.minigames.message.component.Translatable;
import eu.mhsl.minenet.minigames.message.component.TranslatedComponent;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.ComponentBuilder;
import net.kyori.adventure.text.ComponentLike;
import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.format.TextDecoration;
import net.minestom.server.entity.Player;

import java.util.ArrayList;
import java.util.List;

public abstract class TranslatableMessage implements Sendable {
    protected int indention = 0;
    final List<ComponentLike> chain = new ArrayList<>();

    public TranslatableMessage() {

    }

    public TranslatableMessage appendStatic(Component component) {
        chain.add(component);
        return this;
    }

    public TranslatableMessage appendStatic(String text) {
        chain.add(Component.text(text));
        return this;
    }

    public TranslatableMessage appendTranslated(String mapId) {
        chain.add(TranslatedComponent.byId(mapId));
        return this;
    }

    public TranslatableMessage list(List<String> list) {
        newLine();
        list.forEach(s -> {
            chain.add(Component.text(s));
            newLine();
        });
        return this;
    }

    public TranslatableMessage numberedList(List<String> list) {
        newLine();
        for (int i = 0; i < list.size(); i++) {
            appendStatic(i+1 + ". ");
            appendStatic(list.get(i));
            newLine();
        }
        return this;
    }

    public TranslatableMessage pipe() {
        chain.add(Component.text(" | ", NamedTextColor.DARK_GRAY));
        return this;
    }

    public TranslatableMessage quote(String text) {
        chain.add(Component.text("'" + text + "'"));
        return this;
    }

    public TranslatableMessage newLine() {
        chain.add(Component.text("\n"));
        this.appendStatic("  ".repeat(indention));
        return this;
    }

    public TranslatableMessage indent(int indention) {
        this.indention += indention;
        return this;
    }

    public TranslatableMessage indent() {
        this.indent(1);
        return this;
    }

    public TranslatableMessage undent() {
        this.indent(-1);
        return this;
    }

    public TranslatableMessage resetIndention() {
        indention = 0;
        return this;
    }


    public Component build(Player p) {
        ComponentBuilder<TextComponent, TextComponent.Builder> out = Component.text();
        chain.forEach(componentLike -> {
            if(componentLike == null) componentLike = Component.text("NULL", NamedTextColor.RED, TextDecoration.ITALIC);
            if(componentLike instanceof Translatable t) t.assemble(p);
            out.append(componentLike);
        });
        return out.build();
    }
}
