﻿var selectsManager =
{
    maagarId: 0,
    selects: ['mainCategorySelect', 'childCategorySelect', 'grandchildCategorySelect'],
    selectedCategoryHidId: '',

    selectedCategoryHid:
    {
        Get: function()
        {
            return document.getElementById(selectsManager.selectedCategoryHidId).value;
        },
        Set: function(value)
        {
            document.getElementById(selectsManager.selectedCategoryHidId).value = value;
        }
    },

    Init: function(selects, selectedCategoryHidId)
    {
        this.selects = selects;
        this.selectedCategoryHidId = selectedCategoryHidId;
    },

    // param "pairs": an array of KeyValuePairs whose values contain an array of KeyValuePairs which are the options for the 
    //      matching select (match by index), and whose keys are the ids of the selected value within each array.
    GetCategoryHeirarchy_Complete: function(pairs)
    {
        if (pairs != null && pairs.length > 0)
        {
            for (i = 0; i < selectsManager.selects.length; i++)
            {
                if (pairs.length >= i + 1 && pairs[i] != null)
                {
                    // enable select
                    var select = document.getElementById(selectsManager.selects[i]);
                    select.disabled = false;

                    // Load options
                    selectsManager.ReplaceOptions(select, pairs[i].Value);

                    // Set selected Index
                    select.value = pairs[i].Key;
                }
            }
        }
        else
        {
            // error occurred: Load top-level categories
            selectsManager.GetCategories(null, 0, selectsManager.selects[0]);
        }
    },

    SetPool: function(maagarId)
    {
        selectsManager.maagarId = maagarId;

        var categoryId = selectsManager.selectedCategoryHid.Get();

        if (categoryId != '' && categoryId != '0')
        {
            // CategoryId was already selected: Reload last category tree
            SearchEngineService.GetCategoryHeirarchy
            (
                maagarId,
                selectsManager.selectedCategoryHid.Get(),
                selectsManager.GetCategoryHeirarchy_Complete
            );
        }
        else
        {
            // Load top-level categories select for current maagar
            selectsManager.GetCategories(null, 0, selectsManager.selects[0]);
        }
    },

    // Resets the selects to their starting state
    Reset: function()
    {
        // Set the selected category and disable all selects but the first
        selectsManager.SetCategory(0);
        selectsManager.ToggleSelects(selectsManager.selects[0]);
    },

    // set selected category ID to hidden
    SetCategory: function(id)
    {
        selectsManager.selectedCategoryHid.Set(id > 0 ? id : selectsManager.GetSelectedCategory());
    },

    // Run through categories and find bottom-most selected category
    GetSelectedCategory: function()
    {
        for (var i = selectsManager.selects.length - 1; i >= 0; i--)
        {
            var select = document.getElementById(selectsManager.selects[i]);

            if (select.selectedIndex > 0)
            {
                var value = select.options[select.selectedIndex].value;

                if (value > 0)
                    return value;
            }
        }

        // No options selected: return 0
        return 0;
    },

    // Get all categories with the requested parentId
    // param "select": The select whose selected item's id to use as the parent id, or null
    // param "parentId": The parent ID to get categories for or 0 to get all categories, or null to param "select"'s selected ID
    // param "target": The ID of the target select into which to load the resultant categories
    GetCategories: function(select, parentId, target)
    {
        if (select != null)
        {
            parentId = select.options[select.selectedIndex].value;
        }

        // Set selected ID
        selectsManager.SetCategory(parentId);

        if (parentId < 0)
        {
            // all selected (-1): disable selects below this select
            selectsManager.ToggleSelects(select.id);
            return;
        }

        SearchEngineService.GetCategoriesByMaagarAndParent
        (
            selectsManager.maagarId,
            parentId,
            target,
            selectsManager.GetCategories_Complete
        );
    },

    GetCategories_Complete: function(args)
    {
        if (args != null && args.Result != null)
        {
            var target = document.getElementById(args.Target);

            // disable the target select if there are no results
            target.disabled = args.Result.length == 0;

            selectsManager.ToggleSelects(args.Target);
            selectsManager.ReplaceOptions(target, args.Result);
        }
    },

    // Removes all of target select's options except the first and then adds the given array of pairs as options.
    // param "pairs": an array of objects with "Value" and "Key" properties: "Value" will become the option's displayed text and key
    //          will become the option's value.
    ReplaceOptions: function(select, pairs)
    {
        // Clear options
        select.options.length = 1;

        // Add categories options
        for (var i = 0; i < pairs.length; i++)
        {
            select.options[i + 1] = new Option(pairs[i].Value, pairs[i].Key);
        }
    },

    // Disables all selects below target select; Enables all selects above target select.
    ToggleSelects: function(targetId)
    {
        var found = false;

        // Enable/Disable selects
        for (var i = selectsManager.selects.length - 1; i >= 0; i--)
        {
            var select = selectsManager.selects[i];

            if (select == targetId)
                found = true;
            else if (found)
            // above target: enable
                document.getElementById(select).disabled = false;
            else
            {
                // below target: disable
                select = document.getElementById(select);
                select.disabled = true;
                select.selectedIndex = 0;
            }
        }
    }
};
