let errorWrittenToPage = false;

document.addEventListener("DOMContentLoaded", function() {
    setTimeout(function() {
        if(errorWrittenToPage != true)
            setErrorDisp(500, "Internal Server Error", "Error while finding the error message - oh the irony");
    }, 5000);

    try
    {
        let errorInfo = JSON.parse(atob(Cookies.get("errorInfo"))); // eslint-disable-line no-undef

        let statusCode = Number(errorInfo ? errorInfo.code : NaN);
        if(isNaN(statusCode))
            statusCode = 500;
        let errorReasonMsg = "";
        let errorSubtext = "";

        switch(statusCode)
        {
            case 404:
                errorReasonMsg = "Not Found";
                errorSubtext = "<!--%#INSERT:NAME#%--> couldn't find a resource that corresponds to the URL you have entered.<br>Please make sure the URL is correct or <a href=\"<!--%#INSERT:DOCSURL#%-->\">visit the documentation by clicking here</a>.";
            break;
            case 429:
                errorReasonMsg = "Too Many Requests";
                errorSubtext = "You have sent too many requests to the server in a short amount of time.<br>Please wait a few minutes and try again.";
            break;
            case 500: default:
                errorReasonMsg = "Internal Server Error";
                errorSubtext = "<!--%#INSERT:NAME#%--> encountered an unexpected internal error.<br>If this error persists and error details were provided on this page, please <a href=\"<!--%#INSERT:AUTHORWEBSITEURL#%-->\">contact me</a> with the error details and I will try to fix it and/or help you.<br>Alternatively, <a href=\"<!--%#INSERT:DOCSURL#%-->\">visit the documentation by clicking here</a>.";
            break;
        }

        setErrorDisp(statusCode, errorReasonMsg, errorInfo.message, errorSubtext);
    }
    catch(err)
    {
        setErrorDisp(500, "Internal Server Error", "Error while finding the error message - oh the irony");
    }
});

/**
 * Sets the error display of the page
 * @param {Number} code 
 * @param {String} summary
 * @param {String} details 
 * @param {String} subText
 */
function setErrorDisp(code = 500, summary = "Internal Server Error", details = "No details provided", subText = "")
{
    errorWrittenToPage = true;
    document.title = ("<!--%#INSERT:NAME#%--> - Error " + code.toString());
    document.getElementById("errCodeDisplay").innerText = (code.toString() + " - " + summary);
    document.getElementById("errDetailDisplay").innerText = "Details: " + details;

    if(subText)
        document.getElementById("errSubText").innerText = subText;
}
