// ========== changelog-toc.js ==========

// #region Define Variables
var IsChangelog                 = window.location.href.indexOf("changelog.html") != -1;
var IsAuthoringChangelog        = window.location.href.indexOf("Expere/Authoring/ReleaseNotes") != -1;
var IsAuthDDVChangelog          = window.location.href.indexOf("exp_DDV_changelog") != -1;
var IsAuthPrevCL                = window.location.href.indexOf("exp_Changelogs.html") != -1;
var IsChangelogPage             = window.location.href.indexOf("change_log_entries.html") != -1;
var IsDDChangelogPage           = window.location.href.indexOf("exp_defaultdata_changelogentries.html") != -1;
var IsDefaultDataChangelog      = window.location.href.indexOf("DefaultData/Changelogs") != -1;
var IsDGSChangelog              = window.location.href.indexOf("DGS/Change-Log") != -1;
var IsDGSCLPage                 = window.location.href.indexOf("change-log-entries") != -1;
var IsDocMgmt                   = window.location.href.indexOf("eOriginal/DocumentMgmt") > 1;
var IsDocumentPortal            = window.location.href.indexOf("edp_change-log-entries") != -1;
var IsDocViewer                 = window.location.href.indexOf("DocViewer/ReleaseNotes") != -1;
var IsDocViewerCLPage           = window.location.href.indexOf("/dv_release") != -1;
var IsEZGatewayChangelogPage    = window.location.href.indexOf("exp_ezgateway_changelogentries.html") != -1;
var IsFFMgmt                    = window.location.href.indexOf("/FulfillmentManager") != -1;
var IsFFMgmtCL                  = window.location.href.indexOf("-Changelog.html") != -1;
var IsI32Changlog               = window.location.href.indexOf("I-32/Changelog") != -1;
var IsOPPSA                     = window.location.href.indexOf("OPPSA/Changelog") != -1;
var IsPackAndPackChangelog      = window.location.href.indexOf("PackagesAndPackets/ChangeLog") != -1;
var IsWKHostedChangelog         = window.location.href.indexOf("WK-Hosted-Expere/Change-Log") != -1;
var IsWKHostedChangelogPage     = window.location.href.indexOf("eds_change-log-entries") != -1;

var revLevel                    = sessionStorage.getItem('revision');
// #endregion

if (IsI32Changlog) {
    PrependClToc();
}


// #region Main
if (IsI32Changlog || 
    IsAuthoringChangelog || 
    IsAuthDDVChangelog || 
    IsDDChangelogPage || 
    IsDocMgmt || 
    IsDocumentPortal || 
    IsEZGatewayChangelogPage || 
    (IsFFMgmt && IsFFMgmtCL) ||
    IsPackAndPackChangelog ||
    (IsOPPSA && IsChangelogPage) || 
    (IsWKHostedChangelogPage) || 
    (IsChangelog && IsDGSChangelog) || 
    (IsDocViewerCLPage)
    ) {
    //remove existing TOC
    $('[id*="_ChangelogNavEnhancements"]').empty();
    $('[id*="_ChangelogNavDefects"]').empty();


    // #region Create Changelog TOC
    if (IsAuthoringChangelog) {
        $('.conbody').prepend('<div class="clToc" style="float: right; width: 40%;">' +
            '<section class="section ChangelogNav" style="background-color: #C5C5C5; border-radius: 10px; padding: 10px 20px;"></section></div>');
        if (IsAuthPrevCL) {
            $('.clToc').remove();
        }
    }

    if (IsAuthDDVChangelog) {
        $('.cl_section').before('<div class="clToc" style="float: right; width: 40%;">' +
            '<section class="section ChangelogNav" style="background-color: #C5C5C5; border-radius: 10px; padding: 10px 20px;"></section></div>');
    }

    if (IsDDChangelogPage || IsEZGatewayChangelogPage) {
        $('.conbody').before('<div class="clToc" style="float: right; width: 40%;">' +
            '<section class="section ChangelogNav" style="background-color: #C5C5C5; border-radius: 10px; padding: 10px 20px;"></section></div>');
        $('article').css('width', '105%');
        $('#exp_defaultdata_changelogentries__section_cwl_vcc_j2b').css('display', 'table-cell');
        $('#exp_defaultdata_changelogentries__section_cwl_vcc_j2b').css('padding-right', '10px');
    }
    
    if (IsDGSChangelog && IsChangelog) {
        $('.conbody').prepend('<div class="clToc" style="float: right; width: 40%;">' +
            '<section class="section ChangelogNav" style="background-color: #C5C5C5; border-radius: 10px; padding: 10px 20px;"></section></div>');
        $('#wh_topic_toc').remove();
        $('.cl_section:eq(0)').css('display', 'table-cell');
        $('.cl_section:eq(0)').css('padding-right', '20px');
        $('.col-lg-7').css('width', '100%');
    }

    if (IsDocumentPortal) {
        $('#wh_publication_toc').remove();
        $('#wh_topic_body').removeClass();
        $('.conbody').prepend('<div class="clToc" style="float: right; width: 40%;">' +
            '<section class="section ChangelogNav" style="background-color: #C5C5C5; border-radius: 10px; padding: 10px 20px;"></section></div>');
        $('.cl_section:eq(0)').css('display', 'table-cell');
        $('.cl_section:eq(0)').css('padding-right', '20px');
        $('.col-lg-7').css('width', '100%');
    }

    if ((IsDocViewerCLPage) || 
        (IsFFMgmt && IsFFMgmtCL)) {
        $('#wh_publication_toc').remove();
        $('#wh_topic_content body').css('width','100%');
        $('.conbody').before('<div class="clToc" style="float: right; width: 40%;">' +
            '<section class="section ChangelogNav" style="background-color: #C5C5C5; border-radius: 10px; padding: 10px 20px;"></section></div>');
    }

    if (IsOPPSA && IsChangelog) {
        $('.conbody').before('<div class="clToc" style="float: right; width: 40%;">' +
            '<section class="section ChangelogNav" style="background-color: #C5C5C5; border-radius: 10px; padding: 10px 20px;"></section></div>');
        $('#wh_topic_body').css('width', '70%');
        $('#c13030483__section_ikz_gcn_n2b').css('display', 'table-cell');
        $('#c13030483__section_ikz_gcn_n2b').css('padding-right', '10px');
        $('article').css('width', '110%');
    }

    if (IsPackAndPackChangelog) {
        $('.conbody').prepend('<div class="clToc" style="float: right; width: 40%;">' +
            '<section class="section ChangelogNav" style="background-color: #C5C5C5; border-radius: 10px; padding: 10px 20px;"></section></div>');
        $('#wh_topic_body').css('width', '70%');
        $('#ezconfig_changelog-entries__section_cwl_vcc_j2b').css('display', 'table-cell');
        $('#ezconfig_changelog-entries__section_cwl_vcc_j2b').css('padding-right', '10px');
    }

    if (IsWKHostedChangelogPage) {
        $('.conbody').prepend('<div class="clToc" style="float: right; width: 40%;">' +
            '<section class="section ChangelogNav" style="background-color: #C5C5C5; border-radius: 10px; padding: 10px 20px;"></section></div>');
        // $('#concept_vw1_sxj_xnb__section_hdv_3ld_1kb').remove();
        // $('#concept_vw1_sxj_xnb__section_cjg_wxj_xnb').remove();
        // $('#concept_vw1_sxj_xnb__section_ej1_xxj_xnb').remove();
        $('#wh_topic_toc').remove();
        // $('.wh_content_area').css('width', '100%');
        // $('#wh_topic_body').css('width', '70%');
        $('.cl_section:eq(0)').css('display', 'table-cell');
        $('.cl_section:eq(0)').css('padding-right', '20px');
        // $('#ezconfig_changelog-entries__section_cwl_vcc_j2b').css('padding-right', '10px');
        // $('.col-lg-7').css('width', '100%');
    }

    // InsertCurrentReleaseTitle();

    CreateHeadingParasAndLists();

    CreateNewToc();

    ModifyCSS();

    if (IsAuthoringChangelog) {
        sortChangelogNewFeatures();
        sortChangelogNavEnhancements();
        sortChangelogNavDefects();
    }


}
// #endregion Main


function CreateHeadingParasAndLists(){
    $('.ChangelogNav').append('<p style="color: black"><b>New Features</b></p>' +
    '<ul id="NewFeaturesList"></ul>' +
    '<p style="color: black"><b>Enhancements</b></p>' +
    '<ul id="EnhancementsList"></ul>' +
    '<p style="color: black"><b>Defect Corrections</b></p>' +
    '<ul id="DefectsList"></ul>');
}

function CreateNewToc(){
    $(".simpletable").each(function () {
        // var content=$(this).attr("class").replace("page-", "");
        //get IDs of headings
        var EnhHeadingID = $('[id*="_ChangelogNavEnhancements"]').attr('id');
        var DefHeadingID = $('[id*="_ChangelogNavDefects"]').attr('id');
        var ReleasenoteEqualsDatarev = false;
        if (IsDDChangelogPage || 
            IsEZGatewayChangelogPage || 
            IsPackAndPackChangelog || 
            IsWKHostedChangelog || 
            // ((IsDGSChangelog) && (!(IsDGSCLPage))) || 
            (IsDGSChangelog) || 
            IsDocumentPortal || 
            (IsDocViewerCLPage)
            ) {
            var curRev = $(this).closest('section').attr('data-rev');
            if (curRev && revLevel){
                var EqualRevs = versionCompare(revLevel, curRev, true);
            }
            if (curRev === revLevel) {
                ReleasenoteEqualsDatarev = true;
            } else {
                ReleasenoteEqualsDatarev = false;
            }
            if (EqualRevs > -1) {
                //get title of release note
                var title = $(this).find("th").text();
                var id = $(this).attr("id");
                //is it a new feature, enhancement or defect
                var TypeFullText = $(this).find("li").first().text();
                var trimType = $.trim(TypeFullText);
                var type = trimType.split(" ").pop();
            }
        } else {
            //get title of release note
            var title = $(this).find("th").text();
            var id = $(this).attr("id");
            //is it a new feature, enhancement or defect
            var TypeFullText = $(this).find("li").first().text();
            var trimType = $.trim(TypeFullText);
            var type = trimType.split(" ").pop();
        }

        if (!IsWKHostedChangelog || 
            (IsWKHostedChangelog && ReleasenoteEqualsDatarev)){
            if (type == "Feature") {
                //move to the Enhancements list
                $('#NewFeaturesList').append("<li>" +
                "<a style='color: black' href='#" + id + "'>" + title + "</a></li>");
            }
    
            if (type == "Enhancement") {
                //move to the Enhancements list
                // var div = document.querySelector('section')
                // if($(this).parent().attr('rev') == revLevel){
                $('#EnhancementsList').append("<li>" +
                    "<a style='color: black' href='#" + id + "'>" + title + "</a></li>");
                // }
                // if (div.closest(section[data-rev]) == revLevel){
                //     $('#EnhancementsList').append("<li>"+
                //         "<a style='color: black' href='#" + id + "'>"+title+"</a></li>");
                // }
            }
    
            if (type == "Defect" || type == "Correction") {
                //move to the Defects list
                $('#DefectsList').append("<li>" +
                    "<a style='color: black' href='#" + id + "'>" + title + "</a></li>");
            }
    
        }
    });
}

function InsertCurrentReleaseTitle(){
    // Insert "Current Release" title in toc for
    if (IsPackAndPackChangelog || IsWKHostedChangelog) {
        if (revLevel !== null) {
            $('.ChangelogNav').append('<p><b>Current Release:</b>  ' + revLevel + '</p>');
        }
        else {
            $('.ChangelogNav').append('<p><b>Current Release:</b>  </p>');
        }
    }
}

function PrependClToc(){
    $('.conbody').prepend('<div class="clToc" style="float: right; width: 40%;"><section class="section ChangelogNav" style="background-color: #C5C5C5; border-radius: 10px; padding: 10px 20px;"></section></div>');
}

function sortChangelogNavDefects() {
    var myDeflist = $('#DefectsList');
    var DefListItems = myDeflist.children('li').get();
    DefListItems.sort(function (a, b) {
        return $(a).text().toUpperCase().localeCompare($(b).text().toUpperCase());
    })
    $.each(DefListItems, function (idx, itm) {
        myDeflist.append(itm);
    });
}

function sortChangelogNavEnhancements() {
    var myEnhlist = $('#EnhancementsList');
    var EnhListItems = myEnhlist.children('li').get();
    EnhListItems.sort(function (a, b) {
        return $(a).text().toUpperCase().localeCompare($(b).text().toUpperCase());
    })
    $.each(EnhListItems, function (idx, itm) {
        myEnhlist.append(itm);
    });
}

function sortChangelogNewFeatures() {
    var myFeatlist = $('#NewFeaturesList');
    var NewFeatureListItems = myFeatlist.children('li').get();
    NewFeatureListItems.sort(function (a, b) {
        return $(a).text().toUpperCase().localeCompare($(b).text().toUpperCase());
    })
    $.each(NewFeatureListItems, function (idx, itm) {
        myFeatlist.append(itm);
    });
}

function ModifyCSS(){
    $('.ChangelogLink').css({
        // 'display':'inline-block',
        // 'position':'absolute',
        // 'position':'sticky',
        // 'text-align':'end',
        // 'right':'-25px',
        // 'top':'22px',
        // 'font-size':'1.5rem'
    });
}