
//\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\//
// Favorites dialogs and filtering
//\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\//


var LastFilterTypeText = null;
var LastFilterTypeValue = null;

function OpenFavoritesDialog(FilterTypeText, FilterTypeValue) {

    LastFilterTypeText = FilterTypeText;
    LastFilterTypeValue = FilterTypeValue;

    var nDlgWidth = 500;
    var nDlgHeight = 700;
    var nWindowWidth = $(window).width();
    var nWindowHeight = $(window).height();
    if (nWindowWidth < nDlgWidth)
        nDlgWidth = nWindowWidth * 0.95;
    if (nWindowHeight < nDlgHeight)
        nDlgHeight = nWindowHeight * 0.95;

    // Build favorites tree from Ajax
    AjaxLoadFavoritesTree($("#igDialogFilterFavoritesTreeDiv"), FilterTypeText, FilterTypeValue);

    // Open the dialog
    // igniteui.com/help/igdialog-property-reference
    $("#igDialogFilterFavorites").igDialog({ modal: true, headerText: "Filter By Favorites", width: nDlgWidth, height: nDlgHeight, resizable: true, closeOnEscape: true, openAnimation: "", closeAnimation: "fade"/*, openAnimation: "fade"*/ });
    $("#igDialogFilterFavorites").igDialog("open");

    CenterDialog("igDialogFilterFavorites");
}

function OpenProjectsFavoritesDialog() {
    OpenFavoritesDialog("Projects", "Projects");
}


var bFilterFavoritesDialogIsOpen = false;
function CloseFilterFavoritesDialog() {
    $("#igDialogFilterFavorites").igDialog("close");
}

function IsTaskLinkDetailsDialogOpen() {
    return bFilterFavoritesDialogIsOpen;
}


function ApplyFilterFavoritesDialog() {

    CloseFilterFavoritesDialog();

    // Get all checked nodes
    var CheckedNodes = $("#FavoritesFilterTree").igTree("checkedNodes");
    if (!CheckedNodes)
        return;

    // Collect all the node values into a CSV list
    // data-value="AD450FF2-54FC-4847-8ED0-EF2F388C37D2"
    var sNodeGuids = "";
    for (i = 0; i < CheckedNodes.length; ++i) {
        var node = CheckedNodes[i];
        if (!node)
            continue;
        if (!node.data)
            continue;
        if (!node.data.Value)
            continue;
        if (node.data.Value == LastFilterTypeText)
            continue;

        if (sNodeGuids)
            sNodeGuids += ",";
        sNodeGuids += node.data.Value;
    }

    // Save to cookie using LastFilterTypeText
    SetFilterFavoritedIdsCookie(LastFilterTypeText, sNodeGuids);

    // Apply the filter
    ProcessFilter(LastFilterTypeText/*FilterText*/, sNodeGuids/*FilterValue*/, "Favorites-"+LastFilterTypeText/*FilterType*/);
}

/**
 * Favorites filter tree has finished rendering
 * Check all the personal favorites this user has checked in the past
 * Load personal favorites from a cookie using LastFilterTypeText
 * */
function FavoritesFilterTreeRendered() {

    CheckFilterFavoritesNodesFromCookie();
}

function CheckFilterFavoritesNodesFromCookie() {

    try {
        if (!LastFilterTypeText)
            return;

        // Load record ids from cookie using LastFilterTypeText
        var sNodeGuids = GetFilterFavoriteIdsCookie(LastFilterTypeText);
        if (!sNodeGuids)
            return;
        var NodeGuids = sNodeGuids.split(',');
        if (!NodeGuids)
            return;
        if (!NodeGuids.length)
            return;

        // data-value="AD450FF2-54FC-4847-8ED0-EF2F388C37D2"
        // var nodes = $(".selector").igTree("nodesByValue", "NY");
        // Check each node from the cookie id list
        for (i = 0; i < NodeGuids.length; ++i) {
            var sGuid = NodeGuids[i];
            if (!sGuid)
                continue;

            var nodes = $("#FavoritesFilterTree").igTree("nodesByValue", sGuid);
            if (!nodes)
                continue;
            if (!nodes.length)
                continue;
            var node = nodes[0];
            if (!node)
                continue;

            $.each(nodes, function (innerIndex, toCheckNode) {
                $("#FavoritesFilterTree").igTree("toggleCheckstate", $(toCheckNode));
            });

            //var NodeObj = $("#FavoritesFilterTree").igTree("nodeFromElement", node);
            //if (!NodeObj)
            //    continue;
            //if (!NodeObj.length)
            //    continue;

            // Check this node
            //$("#FavoritesFilterTree").igTree("checkNode", node);
            //$("#FavoritesFilterTree").checkNode(NodeObj);
        }

        // Check results of checks above
        var CheckedNodes = $("#FavoritesFilterTree").igTree("checkedNodes");
    }
    catch (e) {
        console.log(e);
    }
}





// https://www.igniteui.com/help/api/2021.2/ui.igtree#events:nodeClick
// https://www.igniteui.com/help/api/2021.1/ui.igtree
//Fired on node click.
//    evt
//Type: Event
//JQuery event object.

//    ui
//Type: Object
//owner
//Type: Object
//Gets a reference to the tree.

//    node
//Type: Object
//Gets a reference to the node object being clicked.

function FavoritesFilterTreeNodeClicked(evt, ui) {

    var ParentNode = GetParentTreeNode($("#FilterTree"), ui.node);
    var TopLevelNode = GetTopLevelTreeNode($("#FilterTree"), ui.node);
}







function AjaxLoadFavoritesTree(divElement, FilterTypeText, FilterTypeValue) {

    if (!FilterTypeText)
        return;

    var params = {
        "FilterTypeText": FilterTypeText,
        "FilterTypeValue": FilterTypeValue,
    };

    $.ajax({
        url: VirtualDirectory + "/Home/AjaxLoadFavoritesTree/",
        type: "POST",
        data: JSON.stringify(params),
        contentType: "application/json", /*type of data sent to server*/
    }).done(function (data) {
        $(divElement).html(data);
    });
}




