/// class ProspectDirector.Modules.Views.ViewBrowserList
/// using ProspectDirector.Types.Core.ContextMenu

/**
The module for the administration View ViewBrowserList List form.
@class ViewBrowserList
@namespace ProspectDirector.Modules.Views
*/
ProspectDirector.Modules.Views.ViewBrowserList = (function (options) {

    var 

    //---------------------------
    //--- PRIVATE MEMBERS
    //---------------------------

    PRIVATE = {

        /**
        @method init
        @private
        */
        init: function () {
            
            this.init_grid();
            this.init_menu();

            new ProspectDirector.Types.Core.ViewportSplitter({
                selector: PRIVATE.get_splitter().selector
            });

            PRIVATE.get_grid().data("filler", new ProspectDirector.Types.KendoExtensions.GridFill({
                selector: PRIVATE.get_grid().selector
            }));

            PRIVATE.get_splitter().data("kendoSplitter").bind("resize", function () {
                setTimeout(function () {
                    PRIVATE.get_grid().data("filler").size();
                }, 20);
            });

        },

        //---------------------------
        //--- PRIVATE PROPERTIES
        //---------------------------

        get_splitter: function () {
            return $("[data-id=form-splitter][data-role=splitter]");
        },

        get_grid: function () {
            return $("[data-id=viewgrid][data-role=grid]");
        },

        get_menu: function () {
            return $("[data-id=form-menu][data-role=menu]");
        },

        get_isPersonal: function () {
            return options.data.isPersonalList;
        },

        //---------------------------
        //--- PRIVATE EVENT HANDLERS
        //---------------------------

        /**
        @method gridSelection_onDblClick
        @private
        */
        gridSelection_onDblClick: function (e, data) {

            window.location = ProspectDirector.RUNTIME.resolveUrl("~/viewbrowser?viewId=" + data.data[0].ObjectId);

        },

        /**
        @protected
        @method grid_onNeedContext
        **/
        grid_onNeedContext: function (sender, e) {

            e.filters = options.data.contextualFilters ? options.data.contextualFilters : [];

        },

        //---------------------------
        //--- PRIVATE METHODS
        //---------------------------

        /**
        @method init_grid
        @private
        */
        init_grid: function () {

            PRIVATE.get_grid().data("viewGrid", new ProspectDirector.Modules.Views.ViewGrid({
                selector: PRIVATE.get_grid().selector,
                viewStateId: options.viewStateId,
                height: "100%",
                create: $.proxy(PRIVATE.grid_onCreated, PRIVATE),
                rowDblClick: $.proxy(PRIVATE.gridSelection_onDblClick, PRIVATE),
                needContext: $.proxy(PRIVATE.grid_onNeedContext, PRIVATE),
                selectionMode: "multiple"
            }));

            PRIVATE.get_grid().disableTextSelection();

            PRIVATE.get_grid().data("rowContextMenu", new ProspectDirector.Modules.Views.ViewBrowserList.RowContextMenu({
                module: PRIVATE
            }));

        },

        /**
        @method init_menu
        @private
        */
        init_menu: function () {

            PRIVATE.get_menu().data("menu", new ProspectDirector.Types.Core.Menu({
                selector: PRIVATE.get_menu().selector
            }));

            PRIVATE.get_menu().data("menu").addPlugIn(ProspectDirector.Types.View.Menu, {
                viewStateId: options.viewStateId,
                viewGrid: PRIVATE.get_grid().data("viewGrid")
            });

        }

    },

    //---------------------------
    //--- PUBLIC MEMBERS
    //---------------------------

    PUBLIC = {
    };

    PRIVATE.init();

    return PUBLIC;

});

//---------------------------
//--- CLASSES
//---------------------------

/**
@class RowContextMenu
@namespace ProspectDirector.Modules.ViewBrowserList
*/
ProspectDirector.Modules.Views.ViewBrowserList.RowContextMenu = ProspectDirector.Types.Core.ContextMenu.extend({

    //---------------------------
    //--- CONSTRUCTOR
    //---------------------------

    init: function (options) {

        this._super($.extend({
            module: null,
            width: 300
        }, options));

    },

    //---------------------------
    //--- PROPERTIES
    //---------------------------

    get_module: function () {
        return this.options.module;
    },

    get_grid: function () {
        return this.get_module().get_grid()
    },

    get_contextMenuSelector: function () {
        return this.get_grid().selector + " tr[data-uid]";
    },

    get_isMultiSelect: function () {
        return this.get_selections().length > 1;
    },

    get_selections: function () {
        return this.get_grid().data("selection").get_selections();
    },

    get_selectionsData: function () {
        return this.get_grid().data("selection").getSelectionsData();
    },

    //---------------------------
    //--- PROTECTED METHODS
    //---------------------------

    _executeCommand: function (command, e) {

        var trigger = e.$trigger;

        switch (command) {

            case "open":

                window.location = ProspectDirector.RUNTIME.resolveUrl("~/viewbrowser?viewId=" + this.get_selections()[0]);

                break;

            case "openInNewWindow":

                window.open(ProspectDirector.RUNTIME.resolveUrl("~/viewbrowser?viewId=" + this.get_selections()[0]));

                break;

            case "properties":

                ProspectDirector.Types.Core.Dialogs.ViewProperties.show({
                    params: {
                        viewStateId: this._generateViewStateId()
                    },
                    callbacks: {
                        ok: $.proxy(function () {
                            this.get_grid().data("viewGrid").refresh();
                        }, this)
                    }
                });

                break;

            case "deleteView":

                var data = null;

                if (this.get_selectionsData().length > 0) {

                    data = this.get_selectionsData()[0];

                    if (confirm("Delete the \"" + data.Name + "\" view?")) {
                        this._deleteView(data.ObjectId, false);
                    }

                }

                break;

            case "batchEditCategory":

                this._categorizeViewsDialog.show({
                    params: {
                        selections: this.get_selections()
                    },
                    callbacks: {
                        ok: $.proxy(function () {

                            this.get_grid().data("viewGrid").refresh();

                        }, this)
                    }
                });
                break;


        }

    },

    _configureContextMenu: function (sender, e) {

        e.config.items.open = {
            name: "Open",
            disabled: this.get_isMultiSelect()
        };

        e.config.items.openInNewWindow = {
            name: "Open In New Window",
            disabled: this.get_isMultiSelect()
        };

        if (this.get_module().get_isPersonal()) {

            e.config.items.sep1 = "";

            e.config.items.deleteView = {
                name: "Delete",
                disabled: this.get_isMultiSelect()
            };

        }

        if (this.get_module().get_isPersonal()) {

            e.config.items.sep1 = "";

            e.config.items.batchEditCategory = {
                name: "Categorize..."
            };

        };

        if (this.get_module().get_isPersonal()) {

            e.config.items.sep2 = "";

            e.config.items.properties = {
                name: "Properties...",
                disabled: this.get_isMultiSelect()
            };

        }


    },

    //---------------------------
    //--- PRIVATE METHODS
    //---------------------------

    _generateViewStateId: function () {
        //TODO:   Update this to use the View ID instead of the full ViewStateId.
        return ProspectDirector.RUNTIME.ENVIRONMENT.USER_ID + ".*.*." + this.get_selections()[0] + ".*";
    },

    //TODO: Probably best to move this to the "Dialogs" static class.
    _categorizeViewsDialog: new (ProspectDirector.Types.Core.Dialog.extend({

        init: function () {
            this._super("ViewCategorize",
                    "Categorize Views",
                    ProspectDirector.RUNTIME.resolveUrl("~/view/categorize"));
        }

    })),

    _deleteView: function (id) {

        var that = this;

        PD.ajax({
            url: ProspectDirector.RUNTIME.resolveUrl("~/view/delete/" + id),
            type: "POST",
            success: function () {
                ProspectDirector.Types.Core.Notifications.success("View deleted successfully");
                that.get_grid().data("viewGrid").refresh();
            }
        });

    }

});