package xyz.ignatz.antlr.utils;

import org.antlr.v4.runtime.ParserRuleContext;
import org.antlr.v4.runtime.misc.Interval;
import xyz.ignatz.flowchart.module.Block;
import xyz.ignatz.flowchart.module.Node;

import java.util.*;

public class Blocks {
    public static String getFullText(ParserRuleContext ctx) {
        return ctx.children == null ? ""
                : ctx.start.getInputStream().getText(
                        new Interval(ctx.start.getStartIndex(), ctx.stop.getStopIndex()));
    }

    public static Block build(ParserRuleContext ctx) {
        return build(ctx, "");
    }

    public static Block build(ParserRuleContext ctx, String tpDsc) {
        return build(ctx, Arrays.asList(tpDsc));
    }

    public static Block buildJudgement(ParserRuleContext ctx) {
        Objects.requireNonNull(ctx);
        return build(ctx, Arrays.asList("true", "false"));
    }

    public static Block buildJudgement(ParserRuleContext ctx, Integer line) {
        return ctx == null ? build("true", line, "true") : buildJudgement(ctx);
    }

    public static Block build(ParserRuleContext ctx, Collection<String> tpDscs) {
        Map<String, Node> tps = new HashMap<>();
        tpDscs.forEach(tpDsc -> tps.put(tpDsc, null));
        return build(ctx, tps);
    }

    public static Block build(ParserRuleContext ctx, Map<String, Node> tps) {
        return ctx == null ? null : new Block(new Node(getFullText(ctx), ctx.getStart().getLine(), tps));
    }

    public static Block build(String dsc, Integer line) {
        return build(dsc, line, "");
    }

    public static Block build(String dsc, Integer line, String tpDsc) {
        return build(dsc, line, Arrays.asList(tpDsc));
    }

    public static Block build(String dsc, Integer line, Collection<String> tpDscs) {
        Map<String, Node> tps = new HashMap<>();
        tpDscs.forEach(tpDsc -> tps.put(tpDsc, null));
        return build(dsc, line, tps);
    }

    public static Block build(String dsc, Integer line, Map<String, Node> tps) {
        return new Block(new Node(dsc, line, tps));
    }
}
