/*
 * FlatRender — After Effects template SCANNER (read-only / ExtendScript).
 *
 * Walks an opened .aep project and emits its structure as JSON for the content
 * service to import (scenes, frl_/frd_ elements, frd_/frshare colours).
 *
 * Launch (headless), with the node agent setting env vars first:
 *     SET FR_SCAN_AEP=C:\work\templates\cache\<md5>\extracted\proj\template.aep
 *     SET FR_SCAN_OUT=C:\work\scans\<job>\scan.json
 *     "...\Adobe After Effects <ver>\Support Files\afterfx.exe" -r "...\scan.jsx"
 *
 * Conventions (mirrors the legacy NewBrain/JSX binder, in reverse):
 *   comp "frfinal"  → final render comp (recorded, not a scene)
 *   comp "frshare"  → shared colours: each layer name = key, its "Source Text" = colour value
 *   layer  frl_*    → editable element: TextLayer→Text (font/size/justify/text), else Media/Audio
 *   layer  frd_*    → data/direction layer; if its text looks like a colour → per-scene colour zone
 *   any comp containing frl_/frd_ layers → a scene (key = comp name, duration = comp.duration)
 *
 * Output shape == content-svc ScanResult:
 *   { source, render_comp, scenes:[{key,title,scene_type,default_duration_sec,sort,
 *       elements:[{key,title,type,default_value,font_face,font_size,justify,width,height,video_support,is_hidden,sort}],
 *       colors:[{element_key,title,attr_value,default_color,sort}] }],
 *     shared_colors:[{element_key,title,attr_value,default_color,sort}] }
 */
(function () {
    function getenv(name) { try { return $.getenv(name); } catch (e) { return null; } }

    var aepPath = getenv("FR_SCAN_AEP");
    var outPath = getenv("FR_SCAN_OUT") || (Folder.temp.fsName + "/fr_scan.json");

    // ── minimal JSON serializer (older AE has no JSON.stringify) ──────────────
    function esc(s) {
        s = String(s); var out = "", i, c;
        for (i = 0; i < s.length; i++) {
            c = s.charAt(i);
            if (c === '"') out += '\\"';
            else if (c === '\\') out += '\\\\';
            else if (c === '\n') out += '\\n';
            else if (c === '\r') out += '\\r';
            else if (c === '\t') out += '\\t';
            else if (c < ' ') out += '\\u' + ('0000' + c.charCodeAt(0).toString(16)).slice(-4);
            else out += c;
        }
        return out;
    }
    function jstr(v) {
        if (v === null || v === undefined) return "null";
        var t = typeof v;
        if (t === "number") return isFinite(v) ? String(v) : "null";
        if (t === "boolean") return v ? "true" : "false";
        if (t === "string") return '"' + esc(v) + '"';
        if (v instanceof Array) {
            var a = [], i; for (i = 0; i < v.length; i++) a.push(jstr(v[i]));
            return "[" + a.join(",") + "]";
        }
        var props = [], k;
        for (k in v) { if (v.hasOwnProperty(k)) props.push('"' + esc(k) + '":' + jstr(v[k])); }
        return "{" + props.join(",") + "}";
    }

    // ── colour helpers ────────────────────────────────────────────────────────
    function trim(s) { return String(s).replace(/^\s+|\s+$/g, ""); }
    function isColor(s) {
        if (!s) return false; s = trim(s);
        return /^#?[0-9a-fA-F]{6}$/.test(s) || /^\d{1,3}\s*,\s*\d{1,3}\s*,\s*\d{1,3}$/.test(s);
    }
    function normColor(s) {
        s = trim(s);
        if (/^[0-9a-fA-F]{6}$/.test(s)) return "#" + s;               // bare hex → add #
        var m = s.match(/^(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})$/); // r,g,b → #hex
        if (m) {
            function h(n) { n = Math.max(0, Math.min(255, parseInt(n, 10))); var x = n.toString(16); return x.length === 1 ? "0" + x : x; }
            return "#" + h(m[1]) + h(m[2]) + h(m[3]);
        }
        return s;
    }

    function justifyName(j) {
        try {
            if (j === ParagraphJustification.LEFT_JUSTIFY) return "LEFT_JUSTIFY";
            if (j === ParagraphJustification.RIGHT_JUSTIFY) return "RIGHT_JUSTIFY";
            if (j === ParagraphJustification.FULL_JUSTIFY) return "FULL_JUSTIFY";
        } catch (e) {}
        return "CENTER_JUSTIFY";
    }

    function readText(layer) {
        try {
            var st = layer.property("Source Text");
            if (!st) return null;
            var td = st.value; // TextDocument
            return { text: td.text, font: td.font, fontSize: Math.round(td.fontSize), justify: justifyName(td.justification) };
        } catch (e) { return null; }
    }

    function pre3(name) { return String(name).substring(0, 3); }

    function scanScene(comp) {
        var elements = [], colors = [];
        for (var i = 1; i <= comp.numLayers; i++) {
            var layer = comp.layer(i), name = layer.name, p = pre3(name);
            if (p === "frl") {
                var el = { key: name, title: name, sort: i };
                var txt = readText(layer);
                if (txt) {
                    el.type = "Text"; el.default_value = txt.text;
                    el.font_face = txt.font; el.font_size = txt.fontSize; el.justify = txt.justify;
                } else {
                    var src = null; try { src = layer.source; } catch (e) {}
                    if (src && src.hasVideo === false && src.hasAudio === true) {
                        el.type = "Audio";
                    } else {
                        el.type = "Media";
                        if (src) { try { el.width = src.width; el.height = src.height; } catch (e) {} }
                        try { el.video_support = !!(src && src.hasVideo); } catch (e) {}
                    }
                }
                elements.push(el);
            } else if (p === "frd") {
                var t2 = readText(layer);
                if (t2 && isColor(t2.text)) {
                    colors.push({ element_key: name, title: name, attr_value: "fill", default_color: normColor(t2.text), sort: i });
                } else {
                    var del = { key: name, title: name, type: "Text", is_hidden: true, sort: i };
                    if (t2) { del.default_value = t2.text; del.font_face = t2.font; del.font_size = t2.fontSize; }
                    elements.push(del);
                }
            }
        }
        return { elements: elements, colors: colors };
    }

    function compHasEditable(comp) {
        for (var k = 1; k <= comp.numLayers; k++) {
            var pp = pre3(comp.layer(k).name);
            if (pp === "frl" || pp === "frd") return true;
        }
        return false;
    }

    function run() {
        if (aepPath) app.open(new File(aepPath));
        var proj = app.project;
        var result = { source: "ae-jsx", render_comp: null, scenes: [], shared_colors: [] };

        for (var i = 1; i <= proj.numItems; i++) {
            var item = proj.item(i);
            if (!(item instanceof CompItem)) continue;
            var nm = item.name;

            if (nm === "frfinal") { result.render_comp = "frfinal"; continue; }
            if (nm === "frshare") {
                for (var j = 1; j <= item.numLayers; j++) {
                    var cl = item.layer(j), ct = readText(cl);
                    result.shared_colors.push({
                        element_key: cl.name, title: cl.name, attr_value: "fill",
                        default_color: (ct && isColor(ct.text)) ? normColor(ct.text) : "#000000", sort: j
                    });
                }
                continue;
            }
            if (!compHasEditable(item)) continue;

            var s = scanScene(item);
            result.scenes.push({
                key: nm, title: nm, scene_type: "Normal",
                default_duration_sec: Math.round(item.duration * 100) / 100,
                sort: result.scenes.length, elements: s.elements, colors: s.colors
            });
        }

        var out = new File(outPath);
        out.encoding = "UTF-8";
        out.open("w");
        out.write(jstr(result));
        out.close();
    }

    try {
        run();
    } catch (e) {
        try { var ef = new File(outPath + ".error"); ef.open("w"); ef.write("scan error: " + e.toString()); ef.close(); } catch (e2) {}
    }
    // Quit AE so the headless afterfx process returns (set FR_SCAN_QUIT=0 to keep open while debugging).
    try { if (getenv("FR_SCAN_QUIT") !== "0") app.quit(); } catch (e) {}
})();
