// resources\landingpages\js\lp_external.js

// #region TODO:
// - Create array variables and function for Product/ProductAbrev. This would allows us to have all products listed and then
//      we simply uncomment the desired product
// - Create array variables and function for productSummary. This would allows us to have all product summaries listed and then
//      we simply uncomment the desired summary
// #endregion


//#region FUNCTIONS - CHANGE NOTHING BELOW THIS LINE ///////////////////////////////////////////////////
// Function to include HTML
function includeHTML() {
    var z, i, elmnt, file, xhttp;
    /*loop through a collection of all HTML elements:*/
    z = document.getElementsByTagName("*");
    for (i = 0; i < z.length; i++) {
        elmnt = z[i];
        /*search for elements with a certain attribute:*/
        file = elmnt.getAttribute("w3-include-html");
        if (file) {
            /*make an HTTP request using the attribute value as the file name:*/
            xhttp = new XMLHttpRequest();
            xhttp.onreadystatechange = function () {
                if (this.readyState == 4) {
                    if (this.status == 200) {
                        elmnt.innerHTML = this.responseText;
                    }
                    if (this.status == 404) {
                        elmnt.innerHTML = "Page not found.";
                    }
                    /*remove the attribute, and call this function once more:*/
                    elmnt.removeAttribute("w3-include-html");
                    includeHTML();
                }
            }
            xhttp.open("GET", file, true);
            xhttp.send();
            /*exit the function:*/
            return;
        }
    }
}

// Function to show/hide announcement based on date
function announcementTime() {
    if (announcementStart <= today && today >= announcementEnd) {
        // current date is within range
        $('#announcement').show();
        console.log("Announcement is shown.");
    } else {
        // current date is out of range
        $('div#announcement').hide();
        console.log("Announcement is hidden.");
    }
}

// Function to populate Getting Started dynamically
function populateGettingStarted() {
    var gettingStartedList = $("#gettingStartedList");
    var gettingStartedSection = $("#gettingStartedSection");

    gettingStarted.forEach(function (item) {
        if (item.title.trim() !== "" && item.link.trim() !== "") {
            var listItem = $("<li></li>");
            var link = $("<a></a>").attr("href", item.link).attr("target", "_blank").text(item.title);
            listItem.append(link);
            gettingStartedList.append(listItem);
        }
    });

    // Hide the section if there are no valid items
    if (gettingStartedList.children("li").length === 0) {
        gettingStartedSection.remove();
    }
}

// Function to populate Release Notes dynamically
function populateReleaseNotes() {
    var releaseNotesList = $("#releaseNotesList");
    var releaseNotesSection = $("#releaseNotesSection");

    // Check if there are items in releaseNotes
    if (releaseNotes.length > 0) {
        releaseNotesSection.show(); // Show the section if there are items
        releaseNotes.forEach(function (item) {
            var listItem = $("<li></li>");
            var link = $("<a></a>").attr("href", item.link).attr("target", "_blank").text(item.title);
            listItem.append(link);
            releaseNotesList.append(listItem);
        });
    }

    // Hide the section if there are no valid items
    if (releaseNotesList.children("li").length === 0) {
        releaseNotesSection.remove();
    }
}

// Function to populate Release Notes dynamically
function populateReleaseNotesArchives() {
    var releaseNotesArchivesList = $("#releaseNotesArchivesList");
    var releaseNotesArchivesSection = $("#releaseNotesArchivesSection");

    // Check if there are items in releaseNotes
    if (releaseNotesArchives.length > 0) {
        releaseNotesArchivesSection.show(); // Show the section if there are items
        releaseNotesArchives.forEach(function (item) {
            var listItem = $("<li></li>");
            var link = $("<a></a>").attr("href", item.link).attr("target", "_blank").text(item.title);
            listItem.append(link);
            releaseNotesArchivesList.append(listItem);
        });
    }

    // Hide the section if there are no valid items
    if (releaseNotesArchivesList.children("li").length === 0) {
        releaseNotesArchivesSection.remove();
    }
}

// Function to populate User Guides dynamically
function populateUserGuides() {
    var userGuidesList = $("#userGuidesList");
    var userGuidesSection = $("#userGuidesSection");

    // Check if there are items in userGuides
    if (userGuides.length > 0) {
        userGuidesSection.show(); // Show the section if there are items

        userGuides.forEach(function (item) {
            var listItem = $("<li></li>");
            var link = $("<a></a>").attr("href", item.link).attr("target", "_blank").text(item.title);
            listItem.append(link);
            userGuidesList.append(listItem);
        });
    } else {
        userGuidesSection.remove(); // Hide the section if there are no items
    }
}

// Function to populate Install and Upgrade Guides dynamically
function populateInstallUpgradeGuides() {
    var installUpgradeGuidesList = $("#installUpgradeGuidesList");
    var installUpgradeGuidesSection = $("#installUpgradeGuidesSection");

    // Check if there are items in userGuides
    if (installUpgradeGuides.length > 0) {
        installUpgradeGuidesSection.show(); // Show the section if there are items

        installUpgradeGuides.forEach(function (item) {
            var listItem = $("<li></li>");
            var link = $("<a></a>").attr("href", item.link).attr("target", "_blank").text(item.title);
            listItem.append(link);
            installUpgradeGuidesList.append(listItem);
        });
    } else {
        installUpgradeGuidesSection.remove(); // Hide the section if there are no items
    }
}

// Function to populate OD-Related Install Guides dynamically
function populateODRelatedInstallGuides() {
    var odRelatedInstallGuidesList = $("#odRelatedInstallGuidesList");
    var odRelatedInstallGuidesSection = $("#odRelatedInstallGuidesSection");

    // Check if there are items in userGuides
    if (odRelatedInstallGuides.length > 0) {
        odRelatedInstallGuidesSection.show(); // Show the section if there are items

        odRelatedInstallGuides.forEach(function (item) {
            var listItem = $("<li></li>");
            var link = $("<a></a>").attr("href", item.link).attr("target", "_blank").text(item.title);
            listItem.append(link);
            odRelatedInstallGuidesList.append(listItem);
        });
    } else {
        odRelatedInstallGuidesSection.remove(); // Hide the section if there are no items
    }
}

// Function to populate Feature Guides dynamically
function populateFeatureGuides() {
    var featureGuidesList = $("#featureGuidesList");
    var featureGuidesSection = $("#featureGuidesSection");

    featureGuides.forEach(function (item) {
        var listItem = $("<li></li>");
        var link = $("<a></a>").attr("href", item.link).text(item.title);
        listItem.append(link);
        featureGuidesList.append(listItem);
    });

    // Check if there are items in Feature Guides
    if (featureGuidesList.children().length === 0) {
        featureGuidesSection.remove(); // Hide the entire section if no items
    }
}

// Function to populate Related Documents dynamically
function populateRelatedDocuments() {
    var relatedDocumentsList = $("#relatedDocumentsList");
    var relatedDocumentsSection = $("#relatedDocumentsSection");

    relatedDocuments.forEach(function (item) {
        var listItem = $("<li></li>");
        var link = $("<a></a>").attr("href", item.link).text(item.title);
        listItem.append(link);
        relatedDocumentsList.append(listItem);
    });

    // Check if there are items in Related Documents
    if (relatedDocumentsList.children().length === 0) {
        relatedDocumentsSection.remove(); // Hide the entire section if no items
    }
}

// Function to populate Landing Pages dynamically
function populateLandingPages() {
    var landingPagesList = $("#landingPagesList");
    var landingPagesSection = $("#landingPagesSection");

    landingPages.forEach(function (item) {
        var listItem = $("<li></li>");
        var link = $("<a></a>").attr("href", item.link).attr("target", item.target || "_self").text(item.title);
        listItem.append(link);
        landingPagesList.append(listItem);
    });

    // Check if there are items in Related Documents
    if (landingPagesList.children().length === 0) {
        landingPagesSection.remove(); // Hide the entire section if no items
    }
}

// Function to populate Release Calendars dynamically
function populateReleaseCalendars() {
    var releaseCalendarList = $("#releaseCalendarList");
    var releaseCalendarSection = $("#releaseCalendarSection");

    releaseCalendars.forEach(function (item) {
        var listItem = $("<li></li>");
        var link = $("<a></a>").attr("href", item.link).text(item.title);
        listItem.append(link);
        releaseCalendarList.append(listItem);
    });

    // Check if there are items in Release Calendars
    if (releaseCalendarList.children().length === 0) {
        releaseCalendarSection.remove(); // Hide the entire section if no items
    }
}

// Function to populate the Panels dynamically
function populatePanels() {
    // Assuming announcementContents and cardContents are arrays with panel data
    // Check if announcementContents array is not empty
    if (announcementContents.length > 0) {
        announcementContents.forEach(function (panelData) {
            var panelElement = createPanelElement(panelData);
            // Append the panel to the content row
            $("#messageCards").append(panelElement);
        });
    }

    // Check if cardContents array is not empty
    if (cardContents.length > 0) {
        cardContents.forEach(function (panelData) {
            var panelElement = createPanelElement(panelData);
            // Append the panel to the content row
            $("#messageCards").append(panelElement);
        });
    }
}

function createPanelElement(panelData) {
    // Check if panelData is undefined or null
    if (!panelData) {
        // Handle the case where panelData is not defined
        console.error("Panel data is undefined or null");
        return null; // or return an empty panel or handle it in a way that makes sense for your application
    }

    // Create panel element with a custom class
    var panelElement = $("<div></div>").addClass("panel panel-primary");

    // Check if panelData is defined and has the 'type' property
    if (panelData && panelData.type) {
        // Set border color based on panel type (announcement or others)
        if (panelData.type === "announcement") {
            panelElement.css("border-color", "#85bc20");            
        }
    }

    // Create panel heading and body
    var panelHeading = $("<div></div>").addClass("panel-heading");

    // Prepend Bootstrap icon based on title conditions
    var titleIcon;
    if (panelData && panelData.title && panelData.title.toLowerCase().includes("announcement")) {
        titleIcon = "megaphone";
    } else if (panelData && panelData.title && panelData.title.toLowerCase().includes("contact")) {
        titleIcon = "telephone";
    } else {
        titleIcon = "envelope";
    }

    // Check if panelData is defined and has the 'type' property
    if (panelData && panelData.type) {
        // Set background color based on panel type (announcement or others)
        if (panelData.type === "announcement") {
            panelHeading.css("background-color", "#85bc20");
            panelElement.addClass("announcementCard");
        } else {
            panelHeading.css("background-color", "#337ab7");
            panelElement.addClass("messageCard");
        }
        panelHeading.css("color", "#ffffff")
    }

    var iconElement = $("<span></span>").addClass("bi").addClass("bi-" + titleIcon);
    panelHeading.append(iconElement);


    // Append title text to panel heading
    panelHeading.append(" " + (panelData.title || "Untitled")); // Use "Untitled" as a default if title is not defined
    

    var panelBody = $("<div></div>").addClass("panel-body").html("<p>" + (panelData.mainContent || "") + "</p>");

    // Append bullet points if available
    if (panelData && panelData.bulletPoints && panelData.bulletPoints.length > 0) {
        var bulletList = $("<ul></ul>").addClass("no_bullets");
        panelData.bulletPoints.forEach(function (bullet) {
            var bulletItem = $("<li></li>").html(bullet);
            bulletList.append(bulletItem);
        });
        panelBody.append(bulletList);
    }

    // Append panel heading and body to panel element
    panelElement.append(panelHeading);
    panelElement.append(panelBody);

    return panelElement;
}


// Document ready function
$(document).ready(function () {
    // Set document title
    document.title = myProduct + " Documentation";

    // Populate product name
    $(".productname").text(myProduct);

    // Populate product Summary
    $(".productSummary").text(productSummary);

    // Set documentation feedback URL
    $("#product_docfdbkurl").attr("href", DocFeedbackUrl);

    // Include HTML
    includeHTML();

    // Trigger population of Getting Started
    populateGettingStarted();

    // Trigger population of Release Notes
    populateReleaseNotes();

    // Trigger population of Release Notes Archives
    populateReleaseNotesArchives();

    // Trigger population of User Guides
    populateUserGuides();

    // Trigger population of Install and Upgrade Guides
    populateInstallUpgradeGuides();

    // Trigger population of OD-Related Install Guides
    populateODRelatedInstallGuides();

    // Trigger population of Feature Guides
    populateFeatureGuides();

    // Trigger population of Related Documents
    populateRelatedDocuments();

    // Trigger population of Release Calendars
    populateReleaseCalendars();

    // Trigger population of Landing Pages
    populateLandingPages();

    // Trigger population of the Panels
    populatePanels();

    // Trigger population of the Panels
    createPanelElement(announcementContents[1]);  // Change index based on your data structure
    createPanelElement(cardContents[1]);  // Change index based on your data structure

    // Trigger announcement logic
    announcementTime();
});
// #endregion