/// class ProspectDirector.Modules.Explorer.Contents.DesignableListContent
/// using ProspectDirector.Modules.Explorer.Contents.ExplorerContent
/// using ProspectDirector.Types.Core.Dialog

ProspectDirector.Modules.Explorer.Contents.DesignableListContent = ProspectDirector.Modules.Explorer.Contents.ExplorerContent.extend({

    init: function (options) {
        this._super($.extend({
            data: {
                entityId: null,
                folderId: null
            }
        }, options));

        this.init_splitter();
        this.init_grid();
        this.init_menu();

    },

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

    get_splitter: function () {
        return this.get_container().find("[data-id=explorer-content-splitter]");
    },

    get_menu: function () {
        return this.get_container().find("[data-id=menu]");
    },

    get_grid: function () {
        return this.get_container().find("[data-role=grid]");
    },

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

    init_splitter: function () {

        new ProspectDirector.Types.Core.ViewportSplitter({
            selector: this.get_splitter().selector,
            resize: $.proxy(this.splitter_onResize, this)
        });

    },

    init_menu: function () {

        this.get_menu().data("menu", new ProspectDirector.Types.Core.Menu({
            selector: this.get_menu().selector,
            select: $.proxy(this.menu_onSelect, this)
        }));

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

    },

    init_grid: function () {

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

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

        this.get_grid().disableTextSelection();

        this.get_grid().data("rowContextMenu", new ProspectDirector.Modules.Explorer.Contents.DesignableListContent.ContextMenu({
            module: this
        }));

    },

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

    menu_onSelect: function (e) {

        var role = $(e.item).data("role"),
            command = $(e.item).data("command");

        if (role === "main") {

            switch (command) {
                case "new":
                    this.showListItemDialog();
                    break;
                case "edit":
                    this.showListItemDialog(this.get_grid().data("selection").get_selections());
                    break;
                case "delete":
                    this.deleteSelections(this.get_grid().data("selection").get_selections());
                    break;
            }

        }

    },

    gridSelection_onDblClick: function (sender, e) {

        this.showListItemDialog(this.get_grid().data("selection").get_selections());

    },

    grid_onNeedContext: function (sender, e) {

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

    },

    splitter_onResize: function (e) {

        var filler = this.get_grid().data("filler");

        setTimeout(function () {
            if (filler) {
                filler.size();
            }
        }, 20);

    },

    //---------------------------
    //--- METHODS
    //---------------------------

    unload: function () {

        var splitter = this.get_splitter().data("viewportSplitter"),
            menu = this.get_menu().data("kendoMenu");

        if (menu) {
            menu.destroy();
        }

        if (splitter) {
            splitter.destroy();
        }

    },

    showListItemDialog: function (items, isCopy) {

        var that = this;

        if (_.isArray(items) && items.length == 0) {
            return;
        }

        PDJS.Hub.Explorer.DesignableListDialog.open({
            documentId: this.options.data.documentId,
            entityId: this.options.data.entityId,
            extensionId: this.options.data.extensionId,
            rowIds: items,
            isCopy: isCopy ? true : false,
            callbacks: {
                ok: function (sender, e) {
                    that.get_grid().data("viewGrid").refresh();
                }
            }
        });
        
    },

    deleteSelections: function (items) {

        var that = this;

        if (items === undefined ||
            items === null ||
            items.length === 0) {
            return;
        }

        if (!confirm("Delete the selected items?")) {
            return;
        }

        PD.Shared.Dialogs.TaskStatus.open({
            config: {
                title: "Delete List Items"
            },
            showDetail: true,
            enableClose: false,
            taskStart: function (args) {

                PD.ajax({
                    type: "POST",
                    url: PD.Path.resolve("~/ExplorerContent/DesignableListItemDelete"),
                    data: {
                        entityId: that.options.data.entityId,
                        folderId: that.options.data.folderId,
                        items: JSON.stringify(items)
                    },
                    success: function (e) {
                        args.startPolling(e);
                    },
                    error: function (e) {
                        args.abort(e.responseJSON);
                        e.errorHandled = true;
                    }
                });

            },
            taskSuccess: function (e) {
                that.get_grid().data("viewGrid").refresh();
                e.enableClose(true);
            }
        });

    }

});

ProspectDirector.Modules.Explorer.Contents.DesignableListContent.ContextMenu = 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_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 "edit":

                this.get_module().showListItemDialog(this.get_selections());
                break;

            case "copy":

                this.get_module().showListItemDialog(this.get_selections(), true);
                break;

            case "deletes":

                this.get_module().deleteSelections(this.get_selections());
                break;

        }

    },

    _configureContextMenu: function (sender, e) {

        e.config.items.edit = {
            name: "Edit..."
        };

        e.config.items.copy = {
            name: "Copy...",
            disabled: this.get_selections().length > 1
        };

        e.config.items.sep1 = "";

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

    }

});