/// class ProspectDirector.Modules.Explorer.Contents.ContentListContent 
/// using ProspectDirector.Modules.Explorer.Contents.ExplorerContent

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

    _data: null,

    init: function (options) {
        this._super(options);

        this._data = options.data;

        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]");
    },

    get_promptAfterCreate: function () {
        return this._data.promptAfterCreate;
    },

    get_promptAfterCreate: function () {
        return this._data.promptAfterCreate;
    },

    get_doubleClickMode: function () {
        return this._data.doubleClickMode;
    },

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

    init_splitter: function () {

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

    },

    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.Views.EntityViewContextMenu({
            module: 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")
        });


    },

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

    splitter_onResize: function () {

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

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

    },

    gridSelection_onDblClick: function (e, data) {

        var that = this;
        var selections = this.get_grid().data("selection").get_selections();
        var id = selections[0];

        if (this.get_doubleClickMode() === 0) {

            if (ProspectDirector.RUNTIME.ENVIRONMENT.CONTENT_LIST_DOUBLE_CLICK_BEHAVIOR === 1) {
                window.open(ProspectDirector.RUNTIME.resolveUrl("~/explore/") + id);
            }
            else {
                window.location = ProspectDirector.RUNTIME.resolveUrl("~/explore/") + id;
            }

        }
        else {

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

        }

    },

    grid_onNeedContext: function (sender, e) {

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

    },

    menu_onSelect: function (e) {

        var category = $(e.item).data("category"),
                id = $(e.item).data("value"),
                that = this;

        if (id !== undefined && id !== null && id !== "") {
            this.get_menu().data("kendoMenu").close();
        }

        switch (category) {

            case "new-entity":
                
                if (id !== undefined && id !== null && id !== "") {
                    
                    PDJS.Entities.EntityDialogs.showCreate({
                        entityTypeId: id,
                        parentId: this.options.data.entityId,
                        promptToOpen: this.get_promptAfterCreate(),
                        onCreate: function (sender, e) {
                            that.get_grid().data("viewGrid").refresh();
                        }
                    });
                    
                }

                break;

        };

    },

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

    unload: function () {

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

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

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

    }

});