package thb.jeanluc.adventure.io.text;

import java.util.List;

/** Default, layout-free rendering of high-level views into {@link StyledText}. */
public final class Renderings {

    private Renderings() {
    }

    /** Generic room rendering used by non-overriding {@code GameIO}s (e.g. tests). */
    public static StyledText roomToStyledText(RoomView r) {
        StyledText.Builder b = StyledText.builder();
        b.heading(r.name()).plain("\n");
        b.plain(r.description().stripTrailing());
        if (!r.items().isEmpty()) {
            b.plain("\nYou see: ");
            appendList(b, r.items(), Style.ITEM);
            b.plain(".");
        }
        if (!r.npcs().isEmpty()) {
            b.plain("\nHere is: ");
            appendList(b, r.npcs(), Style.NPC);
            b.plain(".");
        }
        if (r.exits().isEmpty()) {
            b.plain("\nThere are no obvious exits.");
        } else {
            b.plain("\nExits: ");
            appendList(b, r.exits(), Style.EXIT);
            b.plain(".");
        }
        return b.build();
    }

    private static void appendList(StyledText.Builder b, List<String> xs, Style st) {
        for (int i = 0; i < xs.size(); i++) {
            if (i > 0) {
                b.plain(", ");
            }
            switch (st) {
                case ITEM -> b.item(xs.get(i));
                case NPC -> b.npc(xs.get(i));
                case EXIT -> b.exit(xs.get(i));
                default -> b.plain(xs.get(i));
            }
        }
    }
}
