/**
 * Info screen
 * Renders pre-formatted strings produced by the Python backend
 * (see info_formatting.build_info_variables). All unit, precision, and
 * label decisions live in Python — this file only writes strings to nodes.
 */
var Info = (function () {

    var FIELD_MAP = {
        "info-prog-starts-total": "programStartsTotal",
        "info-prog-starts":       "programStarts",
        "info-prog-duration":     "lastProgramDuration",
        "info-parts-good":        "partsGood",
        "info-parts-bad":         "partsBad",
        "info-op-time":           "operatingTime",
        "info-op-time-total":     "operatingTimeTotal",
        "info-op-time-enabled":   "operatingTimeEnabled",
        "info-op-time-motion":    "operatingTimeMotion"
    };

    function update(state) {
        if (state.variables) {
            var v = state.variables;
            Object.keys(FIELD_MAP).forEach(function (id) {
                setField(id, v[FIELD_MAP[id]] || "--");
            });
            renderAxisChanges(v.robotAxisDirectionChanges, v.externalAxisDirectionChanges);
        }

        if (state.robot && state.robot.tcp) {
            var tcp = state.robot.tcp;
            setField("info-tcp-x", formatCoord(tcp.x, " mm"));
            setField("info-tcp-y", formatCoord(tcp.y, " mm"));
            setField("info-tcp-z", formatCoord(tcp.z, " mm"));
            setField("info-tcp-a", formatCoord(tcp.a, "°"));
            setField("info-tcp-b", formatCoord(tcp.b, "°"));
            setField("info-tcp-c", formatCoord(tcp.c, "°"));
        }
    }

    function formatCoord(val, suffix) {
        if (val === undefined || val === null) return "--";
        return val.toFixed(1) + suffix;
    }

    function setField(id, value) {
        var el = document.getElementById(id);
        if (el) el.textContent = value;
    }

    function renderAxisChanges(robotArr, extArr) {
        var container = document.getElementById("info-axis-changes");
        if (!container) return;
        var robot = Array.isArray(robotArr) ? robotArr : [];
        var ext = Array.isArray(extArr) ? extArr : [];
        if (robot.length === 0 && ext.length === 0) {
            if (!container.firstChild) {
                container.appendChild(makeField("--", "--"));
            }
            return;
        }
        var rows = robot.map(function (val, i) {
            return { label: "Axis " + (i + 1), value: val };
        }).concat(ext.map(function (val, i) {
            return { label: "Ext " + (i + 1), value: val };
        }));
        if (container.childElementCount === rows.length) {
            for (var i = 0; i < rows.length; i++) {
                updateField(container.children[i], rows[i].label, rows[i].value);
            }
        } else {
            container.textContent = "";
            for (var j = 0; j < rows.length; j++) {
                container.appendChild(makeField(rows[j].label, rows[j].value));
            }
        }
    }

    function makeField(label, value) {
        var row = document.createElement("div");
        row.className = "tile-field";
        var labelEl = document.createElement("span");
        labelEl.className = "tile-field-label";
        labelEl.textContent = label;
        var valueEl = document.createElement("span");
        valueEl.className = "tile-field-value val-dim";
        valueEl.textContent = value;
        row.appendChild(labelEl);
        row.appendChild(valueEl);
        return row;
    }

    function updateField(row, label, value) {
        row.children[0].textContent = label;
        row.children[1].textContent = value;
    }

    return {
        update: update
    };
})();
