<% /*
    $Revision$
    $Id$

    File:           upgrade20253.inc
    Description:    2025.3 conversie
    Parameters:
    Context:        (alleen) vanuit facilitor.inc
    Notes:          Oude styling converteren naar het nieuwe template
*/

%>
<!-- #include file="../shared/FlexFiles.inc" -->
<!-- #include file="../Shared/save2db.inc" -->
<%

function upgrade20253()
{
    var marker = "upgrade20253.inc";
    var sql = "SELECT adm_tracking_revision, adm_tracking_date"
            + "  FROM adm_tracking"
            + " WHERE adm_tracking_name = " + safe.quoted_sql(marker);
    var oRs = Oracle.Execute(sql);
    if (!oRs.Eof) {
        __Log("2025.3 JSON upgrade rev {1} is al uitgevoerd op {0}".format(toDateTimeString(oRs("adm_tracking_date").Value), oRs("adm_tracking_revision").Value));
        oRs.Close();
        return;
    }
    oRs.Close();
    var oldCustTemplate = S("csstemplate") || {};

    var newDefaultTemplate = {
        "main-color":               "#051163",
        "accent-color":             "#086DFB",

        "headerimage":              "banner.png",
        "headerbackgroundcolor":    "#FFFFFF",

        "loginbgimage":             "loginbg.jpg",
        "loginbackgroundcolor":     "#062889",
        "logincolor":               "#FFFFFF",
        "loginiconcolor":           "#FF7F62"
    };

    var newCustTemplate = {};

    /* Hieronder de conversie voor de nieuwe S(csstemplate) die alleen de nieuwe styling variabelen bevat, en de nieuwe goed invult */
    var shortHexRegex = new RegExp("^#[0-9A-F]{3}$", "i"); // Ben ik een korte hex-code?
    var hexRegex = new RegExp("^#[0-9A-F]{6}$", "i"); // Ben ik een hex-code?
    function expandHex(h) {
        return ("#" + h.replace(/^#?([a-f\d])([a-f\d])([a-f\d])$/i, function(_, r, g, b) {
            return r + r + g + g + b + b;
        })).toUpperCase();
    }

    function _getHex(s) {
        if (typeof s !== "string") {
            return null;
        }
        if (shortHexRegex.test(s)) {
            return expandHex(s);
        }
        if (hexRegex.test(s)) {
            return s.toUpperCase();
        }
        return null;
    }

    for (var itm in newDefaultTemplate) {
        if ((itm in oldCustTemplate) && oldCustTemplate[itm]) {
            if (itm.slice(-5) === "image") {
                newCustTemplate[itm] = oldCustTemplate[itm];
            } else {
                var thisColor = _getHex(oldCustTemplate[itm]);
                if (thisColor) { // Alleen geldige hex-codes overnemen (geen kleurnamen of rgb-codes)
                    newCustTemplate[itm] = thisColor;
                }
            }
        }
    }

    // Probeer de juiste nieuwe main-color te bepalen; neem hiervoor de oude buttonbackgroundcolor of headercolor over
    var buttonbackgroundcolor = _getHex(oldCustTemplate["buttonbackgroundcolor"]);
    var headercolor = _getHex(oldCustTemplate["headercolor"]);
    var newMainColor = buttonbackgroundcolor || headercolor;
    if (newMainColor) {
        newCustTemplate["main-color"] = newMainColor;
    }

    // Probeer de juiste nieuwe accent-color te bepalen; neem hiervoor de oude buttonhoverbackgroundcolor of menukophoverbackgroundcolor over
    var buttonhoverbackgroundcolor = _getHex(oldCustTemplate["buttonhoverbackgroundcolor"]);
    var menukophoverbackgroundcolor = _getHex(oldCustTemplate["menukophoverbackgroundcolor"]);
    var newAccentColor = buttonhoverbackgroundcolor || menukophoverbackgroundcolor;
    if (newAccentColor) {
        newCustTemplate["accent-color"] = newAccentColor;
    }

    var fields =  [ { dbs: "fac_setting_pvalue", typ: "varchar", val: JSON.stringify(newCustTemplate) },
                    { dbs: "fac_setting_datum",  typ: "sql",     val: "SYSDATE" }];
    sql = buildUpdate("fac_setting", fields, { "noValidateToken": true })
        + "     fac_setting_name = 'csstemplate'";
    var err = Oracle.Execute(sql, true);
    if (err.friendlyMsg) {
        __DoLog(err.friendlyMsg);
        __DoLog(newCustTemplate);
    }

    settings.loadSET(true); // Forceer caching opnieuw
    FCLTHeader.generateTemplateCss(newCustTemplate);

    // Track dat we ge-upgrade hebben, geen risico voor multi-user omgevingen
    sql = "INSERT INTO adm_tracking"
        + "   (adm_tracking_name, adm_tracking_revision)"
        + " VALUES ({0}, {1})".format(safe.quoted_sql(marker), safe.quoted_sql("$Revision$".substr(11, 5)));
    Oracle.Execute(sql);
}
%>

