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

ProspectDirector.Modules.Explorer.Contents.FileExplorerContent = 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]");
    },

    get_fileReplaceForm: function () {
        return this.get_container().find("[data-role=file-replace-form]");
    },

    //---------------------------
    //--- 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"),
            exportTitle: this.options.data.folderName
        });

    },

    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.FileExplorerContent.ContextMenu({
            module: this
        }));

    },

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

    menu_onSelect: function (e) {

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

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

    },

    gridSelection_onDblClick: function (sender, e) {

        this.downloadFile(e.id);

    },

    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();
        }

    },

    downloadFile: function (id) {

        ProspectDirector.RUNTIME.iframeRequest("~/file/download/" + id);

    },

    downloadFiles: function (ids) {

        PD.Shared.Dialogs.TaskStatus.open({
            config: {
                title: "Download Multiple Files"
            },
            showDetail: true,
            taskStart: function (args) {

                args.ajax({
                    type: "POST",
                    url: PD.Path.resolve("~/Hub/File/DownloadMultipleFiles"),
                    data: {
                        ids: ids
                    }
                });

            },
            taskSuccess: function (args) {
                if (args.tags.path != null) {
                    ProspectDirector.RUNTIME.iframeRequest("~/file/DownloadMultipleFilesZip?path=" + args.tags.path);
                    args.close();
                }
            }
        });

    }

});

ProspectDirector.Modules.Explorer.Contents.FileExplorerContent.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_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;

        e.$layer.css({ display: "none" });
        e.$menu.css({ display: "none" });

        switch (command) {

            case "download":

                this.downloadFiles();
                break;

            case "properties":

                this.properties();
                break;

        }

    },

    _configureContextMenu: function (sender, e) {

        var moduleOptions = this.get_module().options;

        e.config.items.download = {
            name: "Download"
        };

        e.config.items.sep3 = "";

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

    },

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

    properties: function () {

        var that = this;
        
        PDJS.Shared.Files.EditFiles.showProperties({
            entityId: this.get_selections()[0],
            onSave: function () {
                that.get_module().get_grid().data("viewGrid").refresh();
            }
        });

    },

    downloadFiles: function () {

        var ids = this.get_selections();

        if (this.get_isMultiSelect()) {
            this.get_module().downloadFiles(ids);
        }
        else {
            this.get_module().downloadFile(_.first(ids));
        }

    }

});