// documentation-links.js

// TODO
// Include: EDS MAin variables section
// Add Documentation link to WK Hosted Expere Changelog


//region Changelog hrefs pulled dynamically
// var myHref = $('#EzConfigChangeLogLink').attr('href');

// https://stackoverflow.com/questions/7567181/jquery-get-different-page-elements
// $('#result').load('/WK-Hosted-Expere/index.html #EzConfigChangeLogLink').attr('href');

// console.log("MyHref = " + myHref);
//endregion
$(document).ready(function() {
    // console.log("Documentation-links.js loading...");
    IsHosted = sessionStorage.getItem("audience");

    // // Ensure Variables is defined
    // if (typeof Variables === 'undefined') {
    //     console.error("Variables is not defined.");
    //     return;
    // }

    // Log the values of Variables
    // console.log("Variables:", Variables);

    // console.log("IsAuthoring = " + Variables.IsAuthoring);
    if (Variables.IsAuthoring){   //if Expere Authoring then
        ProductName             = "Expere Authoring";
        ProductLandingPage      = "/Expere/Authoring/index.html";
        IncRNLink               = "yes";
        ChangelogLink           = "/Expere/Authoring/ReleaseNotes/index.html";
        EngineChangelogLink     = "/Expere/Engine/Change-Log";
        // Changelogs
        IsRN                    = ChangelogLink;
        // User Guides
        var IsReUG 	            = window.location.href.indexOf("Authoring/RequirementsEditor") != -1;
        var IsAdminUG 			= window.location.href.indexOf("Authoring/AdministrationTool/webhelp") != -1;
        var IsDdvUG 	   		= window.location.href.indexOf("Authoring/DDV") != -1;
        var IsAuthInstallUG     = window.location.href.indexOf("Authoring/EngineInstallation") != -1;
    }
    

    if (Variables.IsDefaultData){ //if Default Data then
        // console.log(IsDefaultData);
        ProductName             = "Default Data";
        if(IsHosted){
            console.log("IsHosted = " + IsHosted);
            ProductLandingPage  = "/WK-Hosted-Expere/";
        }
        if(IsInstalled){
            console.log("IsInstalled = " + IsInstalled);
            ProductLandingPage  = "/DGS/";
        }
        IncRNLink               = "yes";
        // ChangelogLink           = DDChangelogLink;
        // alert ("ChangelogLink is " + DDChangelogLink);
        IsRN                    = window.location.href.indexOf("/ExpereEZConfig/Changelog") != -1;
        // User Guides
        var IsDD_InstallGuide   = window.location.href.indexOf("/DefaultData/InstallationGuide") != -1;
        var IsDD_UpgradeGuide   = window.location.href.indexOf("/DefaultData/UpgradeGuide") != -1;
        var IsDD_USerGuide      = window.location.href.indexOf("/DefaultData/UserGuide") != -1;
        var IsDD_WebServGuide   = window.location.href.indexOf("/DefaultData/WebServicesGuide") != -1;
    }

    if (Variables.IsPnP){ //if Packages and Packets then
        ProductName             = "Packages and Packets";
        if(IsHosted){
            console.log("IsHosted = " + IsHosted);
            ProductLandingPage  = "/WK-Hosted-Expere/";
        }
        if(IsInstalled){
            console.log("IsInstalled = " + IsInstalled);
            ProductLandingPage  = "/DGS/";
        }
        IncRNLink               = "yes";
        // ChangelogLink           = "/ExpereEZConfig/Changelog/index.html";
        IsRN                    = window.location.href.indexOf("/ExpereEZConfig/Changelog") != -1;
        // User Guides
        var IsPnP_InstallGuide   = window.location.href.indexOf("/ExpereEZConfig/InstallGuide") != -1;
        var IsPnP_UpgradeGuide   = window.location.href.indexOf("/PackagesAndPackets/UpgradeGuide") != -1;
        var IsPnP_USerGuide      = window.location.href.indexOf("/PackagesAndPackets/UserGuide") != -1;
        var IsPnP_WebServGuide   = window.location.href.indexOf("/ExpereEZConfig//WebservicesGuide") != -1;
    }

    if (Variables.IsDGS){         //if DGS then
        ProductName             = "Document Generation Services";
        ProductLandingPage      = "/DGS/";
        IncRNLink               = "yes";
        ChangelogLink           = "/DGS/Change-Log/index.html";
        // Changelogs
        IsRN                    = window.location.href.indexOf("/DGS/Change-Log") != -1;
        // User Guides
        var IsDGS_Changelog     = window.location.href.indexOf("/DGS/Change-Log/") != -1;
    }

    if (Variables.IsDocViewer){   //if DocViewer then
        ProductName             = "DocViewer";
        if(IsHosted){
            ProductLandingPage  = "/WK-Hosted-Expere/";
        }
        if(IsInstalled){
            ProductLandingPage  = "/DGS/";
        }
        IncRNLink               = "yes";
        ChangelogLink           = "/DocViewer/ReleaseNotes/index.html";
        // Changelogs
        IsRN                    = window.location.href.indexOf("/DocViewer/ReleaseNotes") != -1;
        // User Guides
        
    }

    if (Variables.IsEngine){      //if Expere Engine then
        ProductName             = "Expere Engine";
        ProductLandingPage      = "/Expere/Engine/";
        IncRNLink               = "yes";
        ChangelogLink           = "/Expere/Engine/Change-Log";
        // Changelogs
        IsRN                    = window.location.href.indexOf("Engine/Change-Log") != -1;
        // User Guides
        // Install and Upgrade Guides
        // OD-Related Install Guides
    }

    if (Variables.IseOriginal){   //if eOriginal then
        console.log("IseOriginal = " + IseOriginal);
        ProductName             = "eOriginal";
        if(IsHosted){
            ProductLandingPage  = "/WK-Hosted-Expere/";
        }
        if(IsInstalled){
            ProductLandingPage  = "/DGS/";
        }
        IncRNLink               = "yes";
        ChangelogLink           = "/WK-Hosted-Expere/Change-Log/index.html";
        // Changelogs
        IsRN                    = window.location.href.indexOf("/WK-Hosted-Expere/Change-Log") != -1;
        // User Guides
        var IsQuickGuide     = window.location.href.indexOf("eOriginal/QuickGuide") != -1;

    }

    if (Variables.IsEZGateway){   //if EZGateway then
        ProductName             = "EZGateway";
        ChangelogLink           = "/ExpereEZConfig/Changelog/index.html";
        IsRN                    = window.location.href.indexOf("/ExpereEZConfig/Changelog") != -1;
        if (IsRN){
            IncRNLink = "no";
        }
        else{
            IncRNLink = "yes";
        }
        if(IsHosted){
            // console.log("IsHosted = " + IsHosted);
            ProductLandingPage  = "/WK-Hosted-Expere/";
        }
        if(IsInstalled){
            // console.log("IsInstalled = " + IsInstalled);
            ProductLandingPage  = "/DGS/";
        }
    }

    if (Variables.IsI32){         //if I-32 then
        ProductName             = "I-32";
        ProductLandingPage      = "/I-32/";
        IncRNLink               = "yes";
        ChangelogLink           = "/I-32/Changelog/index.html";
        // Changelogs
        IsRN                    = window.location.href.indexOf("I-32/Changelog") != -1;
        // User Guides
        var IsFormsDesignUG     = window.location.href.indexOf("I-32/FormsDesignUserGuide") != -1;
        var IsImageUpdateUG     = window.location.href.indexOf("I-32/ImageUpdate") != -1;
        // Install and Upgrade Guides
        // OD-Related Install Guides
    }

    if (Variables.IsWKES){       //if WKIS  then
        ProductName             = "ESign";
        ProductLandingPage      = "/WoltersKluwer_E-Sign/index.html";
        IncRNLink               = "yes";
        ChangelogLink           = "/WoltersKluwer_E-Sign/ReleaseNotes/index.html";
        // Changelogs
        IsRN                    = window.location.href.indexOf("/WoltersKluwer_E-Sign/ReleaseNotes") != -1;
        // User Guides
        var IsWKES_UserGuide   = window.location.href.indexOf("//WoltersKluwer_E-Sign/UserGuide/") != -1;
    }

    // if (IsWKIS){       //if WKIS  then
    //     ProductName             = "OPPSA";
    //     ProductLandingPage      = "/DGS/";
    //     IncRNLink               = "yes";
    //     ChangelogLink           = "/OPPSA/ChangeLog/index.html";
    //     // Changelogs
    //     IsRN                    = window.location.href.indexOf("/OPPSA/ChangeLog") != -1;
    //     // User Guides
    //     var IsOPPSA_UserGuide   = window.location.href.indexOf("/OPPSA/UserGuide/") != -1;
    // }


    // console.log("ProductName = " + ProductName + "\n" +
    //             "ProductLandingPage = " + ProductLandingPage + "\n" +
    //             "ChangelogLink = " + ChangelogLink  + "\n" +
    //             "isRN " + IsRN + "\n");

    // if (Variables.IsAuthoring || Variables.IsDefaultData || Variables.IsPnP || Variables.IsDGS || Variables.IsDocViewer ||  
    //     Variables.IsEngine || Variables.IseOriginal || Variables.IsEZGateway || Variables.IsI32 || Variables.IsWKES){
    //     $('.wh_content_area > .row:nth-child(1)').before('<div class="row DocLinks">' + 
    //     '<span class="glyphicon glyphicon-arrow-right landing-page-arrow"></span>' +
    //     '<div class="div-landing-page-btn">' +
    //     '<button type="button" class="btn btn-outline-primary link-landing-page">' +
    //     '<a id="LandingPageLink" class="link-primary" href="' + ProductLandingPage + '" index.html" target="_blank">View ' + ProductName + ' Documentation Page</a></button>' +
    //      '</div>'
    //      );

    //     }


    function prependDocLinks() {
        // console.log("Appending Documentation Links...");
        // console.log("IsAuthoring = " + Variables.IsAuthoring);
        if (Variables.IsAuthoring || 
            Variables.IsDefaultData || 
            Variables.IsPnP || 
            Variables.IsDGS || 
            Variables.IsDocViewer || 
            Variables.IsEngine || 
            Variables.IseOriginal || 
            Variables.IsEZGateway || 
            Variables.IsI32 || 
            Variables.IsWKES) {
            $('.wh_right_tools').prepend('<div class="row DocLinks">' + 
                '<span class="glyphicon glyphicon-arrow-right landing-page-arrow"></span>' +
                '<div class="div-landing-page-btn">' +
                '<button type="button" class="btn btn-outline-primary link-landing-page">' +
                '<a id="LandingPageLink" class="link-primary" href="' + ProductLandingPage + '" target="_blank">View ' + ProductName + ' Documentation Page</a>' +
                '</button>' +
                '</div>'
            );
        }
    }

    function appendChangelogButton() {
        if ((!(Variables.IsDefaultData) && !(Variables.IsPnP)) && (IncRNLink == "yes" && !(Variables.IsChangelogPage))) {
            $('.DocLinks').append(
                '<div class="div-changelog-btn">' +
                '<button type="button" class="btn btn-outline-primary link-landing-page">' +
                '<a class="link-primary" href="' + ChangelogLink + '" target="_blank">View Changelog</a></button></div>'
            );
        } else {
            console.log("Conditions not met for appending button."); // Add this line
        }
    }

    prependDocLinks();
    appendChangelogButton();

    // Modify landing Page link href for DefaultData and PnP
    if (Variables.IsDefaultData || Variables.IsPnP) {
        $('#LandingPageLink').attr('href', ProductLandingPage + 'index.html');
        // Load Landing Page file to access elements
        $.get('/WK-Hosted-Expere/index.html', function (data) {
            const NewChangelogLink = $(data).find('#EzConfigChangeLogLink').attr('href');
            if (IncRNLink == "yes" && !(Variables.IsChangelogPage)) {
                $('.DocLinks').append(
                    '<div class="div-changelog-btn">' +
                    '<button type="button" class="btn btn-outline-primary link-landing-page">' +
                    '<a class="link-primary" href="' + NewChangelogLink + '" target="_blank">View Changelog</a></button></div>'
                );
            }
        });
    }

    if (Variables.IsRN && (!(Variables.IsAuthoring))) {
        $('#searchForm').remove();
    }

    if (Variables.IsEForms) {
        console.log(Variables.IsEForms);
        $('.ChangelogLink').remove();
    }
});