/// class ProspectDirector.Modules.Personalization.Favorites
/// using ProspectDirector.Types.Core.FeaturedObject
/// using ProspectDirector.Types.Core.ContextMenu

ProspectDirector.Modules.Personalization.Favorites = (function (options) {

    var 

        PRIVATE = {

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

            init: function () {

                options = $.extend({
                    data: {
                        viewStateId: null
                    }
                }, options);

                PRIVATE.init_outerSplitter();
                PRIVATE.init_innerSplitter();
                PRIVATE.init_listMenu();
                PRIVATE.init_itemMenu();
                PRIVATE.init_listGrid();
                PRIVATE.init_itemGrid();

                PRIVATE.resize();

            },

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

            get_outerSplitter: function () {
                return $("div[data-id=outer][data-role=splitter]");
            },

            get_innerSplitter: function () {
                return $("div[data-id=inner][data-role=splitter]");
            },

            get_listMenu: function () {
                return $("ul[data-id=lists-menu]");
            },

            get_itemMenu: function () {
                return $("ul[data-id=favorites-menu]");
            },

            get_listGrid: function () {
                return $("DIV[data-id=lists-grid]");
            },

            get_itemGrid: function () {
                return $("DIV[data-id=items-grid]");
            },

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

            newListDialog_onCreate: function (sender, e) {

                var listGrid = PRIVATE.get_listGrid().data("kendoGrid"),
                    itemGrid = PRIVATE.get_itemGrid().data("viewGrid"),
                    selection = PRIVATE.get_listGrid().data("selection");

                listGrid.dataSource.add({ id: e.general.id, name: e.general.name });
                listGrid.dataSource.sort({ field: "name", dir: "asc" })
                listGrid.refresh();
                selection.selectRowsByPK(e.general.id);
                itemGrid.refresh();

            },

            listPropertiesDialog_onOk: function (e) {
                PRIVATE.get_listGrid().data("kendoGrid").refresh();
            },

            listMenu_onSelect: function (e) {

                var command = $(e.item).data("command"),
                    tree = PRIVATE.get_listMenu().data("kendoMenu");

                switch (command) {

                    case "new":

                        ProspectDirector.Modules.Personalization.Favorites.NewListDialog.show({
                            callbacks: {
                                ok: $.proxy(PRIVATE.newListDialog_onCreate, PRIVATE)
                            }
                        });

                        break;

                    case "delete":

                        PRIVATE.deleteList();

                        break;

                }

            },

            listGrid_onRowClick: function (sender, e) {

                PRIVATE.get_itemGrid().data("viewGrid").refresh();

            },

            listGrid_onRowDblClick: function (sender, e) {

                var selections = PRIVATE.get_listGrid().data("selection").get_selections();

                if (selections.length > 0) {
                    PRIVATE.showListPropertiesDialog(selections[0]);
                }

            },

            itemMenu_onSelect: function (e) {

                var command = $(e.item).data("command"),
                    tree = PRIVATE.get_itemMenu().data("kendoMenu");

                switch (command) {

                    case "delete":

                        PRIVATE.deleteItem();

                        break;

                }

            },

            itemGrid_onNeedContext: function (sender, e) {

                var selections = PRIVATE.get_listGrid().data("selection").get_selections();

                e.filters = [];

                if (selections.length > 0) {
                    e.filters[e.filters.length] = { id: "ListFilter", params: { listId: selections[0]} };
                }
                else {
                    e.filters = [];
                    e.filters[e.filters.length] = { id: "ListFilter", params: { listId: -1} };
                }

            },

            itemGrid_onDblClick: function (sender, e) {
                window.location = ProspectDirector.RUNTIME.resolveUrl("~/explore/") + e.data[0].EntityId;
            },

            innerSplitter_onResize: function () {

                setTimeout(function () {
                    PRIVATE.get_listGrid().data("filler").size();
                    PRIVATE.get_itemGrid().data("filler").size();
                }, 20);

            },

            //---------------------------
            //--- INITIALIZERS
            //---------------------------

            init_outerSplitter: function () {

                PRIVATE.get_outerSplitter().data("layout",
                    new ProspectDirector.Types.Core.ViewportSplitter({
                        selector: PRIVATE.get_outerSplitter().selector
                    }));

            },

            init_innerSplitter: function () {

                PRIVATE.get_innerSplitter().kendoSplitter({
                    orientation: "horizontal",
                    panes: [
                        { size: "300px", scrollable: false },
                        { scrollable: false }
                    ],
                    resize: PRIVATE.innerSplitter_onResize
                });

            },

            init_listMenu: function () {

                PRIVATE.get_listMenu().kendoMenu({
                    select: $.proxy(PRIVATE.listMenu_onSelect, PRIVATE)
                });

            },

            init_itemMenu: function () {

                PRIVATE.get_itemMenu().kendoMenu({
                    select: $.proxy(PRIVATE.itemMenu_onSelect, PRIVATE)
                });

            },

            init_listGrid: function () {

                var grid = PRIVATE.get_listGrid().kendoGrid({
                    dataSource: options.data.lists,
                    columns: [
                        { field: "name", title: "Name" }
                    ],
                    sort: { field: "name", dir: "asc" }
                });

                PRIVATE.get_listGrid().disableTextSelection();

                PRIVATE.get_listGrid().data("selection", new ProspectDirector.Types.Core.GridSelectionManager({
                    grid: PRIVATE.get_listGrid(),
                    pk: "id",
                    keys: ["id", "name"],
                    selectionMode: "single",
                    click: $.proxy(PRIVATE.listGrid_onRowClick, PRIVATE)
                }));

                PRIVATE.get_listGrid().data("selection").selectFirst();

                PRIVATE.get_listGrid().on("dblclick", "TR[data-uid]", $.proxy(PRIVATE.listGrid_onRowDblClick, PRIVATE));

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

                PRIVATE.get_listGrid().data("contextMenu", new ProspectDirector.Modules.Personalization.Favorites.ListContextMenu({
                    module: PRIVATE
                }));

            },

            init_itemGrid: function () {

                PRIVATE.get_itemGrid().data("viewGrid", new ProspectDirector.Modules.Views.ViewGrid({
                    selector: PRIVATE.get_itemGrid().selector,
                    viewStateId: options.data.viewStateId,
                    needContext: $.proxy(PRIVATE.itemGrid_onNeedContext, PRIVATE),
                    rowDblClick: $.proxy(PRIVATE.itemGrid_onDblClick, PRIVATE),
                    selectionMode: "single",
                    search: false,
                    summary: false
                }));

                PRIVATE.get_itemGrid().disableTextSelection();

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

                PRIVATE.get_itemGrid().data("contextMenu", new ProspectDirector.Modules.Personalization.Favorites.ItemContextMenu({
                    module: PRIVATE
                }));

            },

            showListPropertiesDialog: function (id) {

                ProspectDirector.Modules.Personalization.Favorites.ListPropertiesDialog.show({
                    params: {
                        id: id
                    },
                    callbacks: {
                        ok: $.proxy(PRIVATE.listPropertiesDialog_onOk, PRIVATE)
                    }
                });

            },

            deleteList: function (id) {

                var listGrid = PRIVATE.get_listGrid().data("kendoGrid"),
                    itemGrid = PRIVATE.get_itemGrid().data("viewGrid"),
                    listSelection = PRIVATE.get_listGrid().data("selection"),
                    itemSelection = PRIVATE.get_itemGrid().data("selection"),
                    list = null;

                if (id === undefined && listSelection.get_selections().length > 0) {
                    id = listSelection.get_selections()[0];
                }

                list = listGrid.dataSource.get(id)

                if (list === undefined) {
                    return;
                }

                if (confirm("Delete the \"" + list.name + "\" list?")) {

                    PD.ajax({
                        url: ProspectDirector.RUNTIME.resolveUrl("~/Personalization/DeleteFavoriteList"),
                        data: {
                            id: list.id
                        },
                        success: function () {

                            listSelection.clear();
                            listGrid.dataSource.remove(list);
                            listGrid.refresh();

                            itemSelection.clear();
                            itemGrid.refresh();

                        }
                    });

                }

            },

            deleteItem: function (id) {

                var itemGrid = PRIVATE.get_itemGrid().data("kendoGrid"),
                    itemSelection = PRIVATE.get_itemGrid().data("selection"),
                    item = null;

                if (id === undefined && itemSelection.get_selections().length > 0) {
                    id = itemSelection.get_selections()[0];
                }

                item = itemGrid.dataSource.get(id)

                if (item === undefined) {
                    return;
                }

                if (confirm("Remove the favorite \"" + item.EntityName + "\" from this list?")) {

                    PD.ajax({
                        url: ProspectDirector.RUNTIME.resolveUrl("~/Personalization/DeleteFavorite"),
                        data: {
                            id: item.ObjectId
                        },
                        success: function () {

                            itemSelection.clear();
                            PRIVATE.get_itemGrid().data("viewGrid").refresh();

                        }
                    });

                }

            },

            resize: function () {

                setTimeout(function () {
                    PRIVATE.get_outerSplitter().data("kendoSplitter").resize();
                }, 100);

                setTimeout(function () {
                    PRIVATE.get_innerSplitter().data("kendoSplitter").resize();
                }, 200);

            }

        },

        PUBLIC = {

        };

    PRIVATE.init();

    return PUBLIC;

});

ProspectDirector.Modules.Personalization.Favorites.NewListDialog =
ProspectDirector.Types.Core.FeaturedObject.Dialog("NewFavoritesList", "New Favorites List", "~/Personalization/FavoriteListEditor");

ProspectDirector.Modules.Personalization.Favorites.ListPropertiesDialog =
ProspectDirector.Types.Core.FeaturedObject.Dialog("FavoritesListPropertiesList", "Favorites List Properties", "~/Personalization/FavoriteListEditor");

ProspectDirector.Modules.Personalization.Favorites.ListContextMenu = ProspectDirector.Types.Core.ContextMenu.extend({

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

    init: function (options) {

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

    },

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

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

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

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

    _executeCommand: function (command, e) {

        var id = this.getID(e.$trigger);

        switch (command) {

            case "deletes":
            
                this.get_module().deleteList(id);
                break;

            case "properties":

                this.get_module().showListPropertiesDialog(id);
                break;

        }

    },

    _configureContextMenu: function (sender, e) {

        e.config.items.deletes = {
            name: "Delete"
        };

        e.config.items.properties = {
            name: "Properties..."
        };

    },

    getID: function (row) {

        var uid = row.data().uid,
            grid = this.get_module().get_listGrid().data("kendoGrid");

        return grid.dataSource.getByUid(uid).id;

    }

});

ProspectDirector.Modules.Personalization.Favorites.ItemContextMenu = 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_itemGrid()
    },

    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;
            entityId = this.get_selectionsData()[0].EntityId,
            entityTypeId = this.get_selectionsData()[0].EntityId,
            favoriteId = this.get_selections()[0];

        switch (command) {

            case "openEntity":

                window.location = ProspectDirector.RUNTIME.resolveUrl("~/explore/") + entityId;
                break;

            case "openEntityInNewWindow":

                window.open(ProspectDirector.RUNTIME.resolveUrl("~/explore/") + entityId);
                break;

            case "removeFromList":

                this.get_module().deleteItem(favoriteId);
                break;

            case "reports":

                ProspectDirector.Modules.Reports.ReportSelectorDialog.Dialog.show({
                    params: {
                        entityTypeId: entityTypeId,
                        entityId: entityId
                    }
                });

                break;

            case "properties":

                var that = this;

                PDJS.Entities.EntityDialogs.showProperties({
                    entityId: entityId,
                    onSave: function (sender, e) {
                        that.get_grid().data("viewGrid").refresh();
                    }
                });
                
                break;

        }

    },

    _configureContextMenu: function (sender, e) {

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

        e.config.items.openEntityInNewWindow = {
            name: "Open In New Window",
            disabled: this.get_isMultiSelect()
        };
        
        e.config.items.sep1 = "";
        
        e.config.items.removeFromList = {
            name: "Remove from List",
            disabled: this.get_isMultiSelect()
        };

        e.config.items.sep2 = "";

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

    }

});
