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

// Used for files.html

// Executes all the needed code
async function Load() {
    // Gets all files
    await $.ajax({
        url: "/files/" + Topic(),
        method: "GET",
        success: async function(response) {
            await $.ajax({
                url: "/user/permissions",
                method: "GET",
                data: {
                    topic: Topic(),
                    select: ["create", "edit_entries"]
                },
                success: function(permissions) {
                    for (let _ = 0; _ < response.length; _++) {
                        let appendString = "<td id=\"file" + response[_].id + "\"></td>";
                        if (response[_].given == null) {
                            appendString += "<td>" + response[_].name + "</td>";
                        }
                        else {
                            appendString += "<td>" + response[_].given + "<br><span class=\"small\">" + response[_].name + "</span></td>";
                        }
                        appendString += "<td>" + response[_].created_at + "</td>";
                        if (response[_].edited_at != null) {
                            appendString += "<td>" + response[_].edited_at + "</td>";
                        }
                        else {
                            appendString += "<td> - </td>";
                        }
                        appendString += "<td><a href=\"/" + Topic() + "/File:" + response[_].name + "\">View</a> <a href=\"/" + Topic() + "/File:" + response[_].name + "?a=edit\">Edit</a></td>";
                        $("#TableOfFiles").append("<tr value=\"" + response[_].given + " " + response[_].name + "\">" + appendString + "</tr>");
                        switch (response[_].name.split(".")[1]) {
                            case "gif":
                            case "jpeg":
                            case "jpg":
                            case "png":
                            case "webp":
                                $("#file" + response[_].id).append("<img class=\"image-click\" src=\"load/" + response[_].name + "\">");
                                break;
                            case "mp3":
                            case "wav":
                            case "ogg":
                                $("#file" + response[_].id).append("<audio controls><source src=\"load/" + response[_].name + "\"></audio>");
                                break;
                            default:
                                $("#file" + response[_].id).append("Not supported");
                                break;
                        }
                    }
                    if (response.length <= 0) {
                        $("#TableOfFiles").append("<tr><td class=\"no-entries\" colspan=\"5\">No files are available</td></tr>");
                    }
                    if (Object.keys(permissions).length > 0) {
                        if (permissions.create) {
                            CreateDropDownMenu(permissions);
                            $("#DropDownMenu").append("<li><a href=\"/" + Topic() + "/File:Upload\">Upload file</a></li>");
                        }
                    }
                }
            });
        }
    });
    $("#FileSearchInput").on("input", function() {
        let tableRows = $("#TableOfFiles tr:not(.yippee)");
        let search_ = $("#FileSearchInput").val().toLowerCase();
        for (let _ = 0; _ < tableRows.length; _++) {
            let text_ = $(tableRows[_]).attr("value").toLowerCase();
            if (!text_.includes(search_)) {
                $(tableRows[_]).hide();
            }
            else {
                $(tableRows[_]).show();
            }
        }
    });
}