package com.foxx.androidcast.dev;

import java.util.Locale;

/** Compact structured text report for network self-test. */
final class DevNetworkSelfTestReport {
    enum Level {
        PASS, WARN, FAIL, NA;

        String tag() {
            return name();
        }
    }

    private final StringBuilder sb = new StringBuilder(2048);
    private boolean hasSections;

    void header() {
        sb.append("NETWORK SELF-TEST\n");
    }

    void section(String code, String title) {
        if (hasSections) {
            sb.append('\n');
        }
        hasSections = true;
        sb.append('[').append(code).append("] ").append(title).append('\n');
    }

    void item(Level level, String key, String value) {
        String k = padKey(key);
        sb.append(' ').append(level.tag()).append("  ").append(k).append(' ')
                .append(value != null ? value : "").append('\n');
    }

    void itemPass(String key, String value) {
        item(Level.PASS, key, value);
    }

    void itemWarn(String key, String value) {
        item(Level.WARN, key, value);
    }

    void itemFail(String key, String value) {
        item(Level.FAIL, key, value);
    }

    void itemNa(String key, String value) {
        item(Level.NA, key, value);
    }

    void itemBool(Level yesLevel, String key, boolean yes, String yesText, String noText) {
        item(yesLevel, key, yes ? yesText : noText);
    }

    String build() {
        return sb.toString().trim();
    }

    private static String padKey(String key) {
        if (key == null) {
            return "                ";
        }
        if (key.length() >= 16) {
            return key.substring(0, 16);
        }
        return String.format(Locale.US, "%-16s", key);
    }
}
