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 {
    final List<ComponentLike> chain = new ArrayList<>();
    protected int indention = 0;

    public TranslatableMessage() {

    }

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

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

    public TranslatableMessage appendSpace() {
        this.chain.add(Component.text(" "));
        return this;
    }

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

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

    public TranslatableMessage appendTranslated(TranslatedComponent component) {
        this.chain.add(component);
        return this;
    }

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

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

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

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

    public TranslatableMessage newLine() {
        this.chain.add(Component.text("\n"));
        this.appendStatic("  ".repeat(this.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() {
        this.indention = 0;
        return this;
    }

    public Component build(Player p) {
        ComponentBuilder<TextComponent, TextComponent.Builder> out = Component.text();
        this.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();
    }
}
