/*
    Created: 2025-12-04
    Author: Kārlis Grīnvalds
    Last edited: 2026-05-13
    Edited by: Kārlis Grīnvalds
*/

// Used for entry.html

// Executes all the needed code
async function Load() {
    // Gets the url parameter
    const entryName = window.location.href.split(Topic() + "/")[1].split("?a=")[0];
    await $.ajax({
        url: "/entry",
        method: "GET",
        data: {
            topic: Topic(),
            entry_name: entryName
        },
        success: async function(response) {
            $("#EntryTitle").append(response[0].title);
            $("footer").append("Created at: " + response[0].created_at);
            if (response[0].edited_at != null) {
                // If the entry has been edited then this will also get displayed
                $("footer").append("<br>Last edited: " + response[0].edited_at);
            }
            if (response[0].content.trim().length <= 0) {
                $("#EntryContent").append("<p style=\"color: rgb(100, 100, 100); font-size: 80%;\">No content added..</p>");
            }
            else {
                $("#EntryContent").append(LoadContent(response[0].content));
            }
            CreateContentList();
            $("table.side-content").insertBefore("#EntryContent");
        }
    });
    await $.ajax({
        url: "/user/permissions",
        method: "GET",
        data: {
            topic: Topic(),
            select: ["edit_entries"]
        },
        success: function(permissions) {
            if (Object.keys(permissions).length > 0) {
                if (permissions.edit_entries) {
                    CreateDropDownMenu(permissions);
                    $("#DropDownMenu").append("<li><a href=\"?a=edit\">Edit entry</a></li>");
                }
            }
        }
    });
}

// Fills the content table with a list of links and gives all headings IDs
function CreateContentList() {
    // Checks if content list table exist
    if (!($("#ContentList").length == 1)) {
        return "";
    }
    // Gets all headings and prepares data for the list
    let htmlText = $("#EntryContent")[0].innerHTML;
    let headingIndexes = [];
    for (let _ = 0; _ < htmlText.length; _++) {
        let _indexes = [];
        _indexes.push(htmlText.indexOf("<h2>", _));
        _indexes.push(htmlText.indexOf("<h3>", _));
        _indexes.push(htmlText.indexOf("<h4>", _));
        _indexes.push(htmlText.indexOf("<h5>", _));
        _indexes.push(htmlText.indexOf("<h6>", _));
        let _index = -1;
        for (let h in _indexes) {
            if (_indexes[h] == -1) {
                continue;
            }
            else {
                if (_index == -1 || _index > _indexes[h]) {
                    _index = _indexes[h];
                }
            }
        }
        if (_index == -1) {
            break;
        }
        headingIndexes.push(_index);
        _ = _index;
    }
    // Creates the list
    let theList = "<ol>";
    let previousIndent = 1;
    let allValues = [];
    for (let _ in headingIndexes) {
        let heading = htmlText.substring(headingIndexes[_], htmlText.indexOf("<hr>", headingIndexes[_]));
        let indent = heading[2];
        let innerValue = RemoveHTML(heading);
        allValues.push(innerValue);
        let _headings = $("#EntryContent").find(heading[1] + heading[2]);
        for (let h in _headings) {
            if (_headings[h].outerHTML == heading) {
                $(_headings[h]).attr("id", innerValue.toLowerCase());
                break;
            }
        }
        indent--;
        if (previousIndent > indent) {
            for (let _index = previousIndent; _index > indent; _index--) {
                theList += "</ol>";
            }
        }
        else {
            for (let _index = previousIndent; _index < indent; _index++) {
                theList += "<ol>";
            }
        }
        if (_ != 0) {
            theList += "</li>";
        }
        theList += "<li><a href=\"#" + innerValue.toLowerCase() + "\"></a>";
        previousIndent = indent;
    }
    theList += "</ol>";
    $("#ContentList").append(theList);
    let allAs = $("#ContentList a");
    for (let _ = 0; _ < allValues.length; _++) {
        $(allAs[_]).text(allValues[_]);
    }
}