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

/**
@class RowContextMenu
@namespace ProspectDirector.Modules.Views.EntityViewContextMenu
*/
ProspectDirector.Modules.Views.EntityViewContextMenu = 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,
            data = this.get_selectionsData()[0];

        if (this.isFileSelection()) {
            this.executeFileOption(command, data);
        }
        else {
            this.executeEntityOption(command, data);
        }

    },

    _configureContextMenu: function (sender, e) {

        if (this.isFileSelection()) {
            this.configureFileOptions(e.config);
        }
        else {
            this.configureEntityOptions(e.config);
        }

    },

    isFileSelection: function () {
        return this.get_selectionsData()[0].ObjectType === "sys.file";
    },

    selectionsContainFile: function () {

        var selection = this.get_selectionsData(),
            index = 0;

        for (index = 0; index < selections.length; index++) {
            if (selection[index].ObjectType === "sys.file") {
                return true;
            }
        }

        return false;

    },

    configureFileOptions: function (config) {

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

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

        config.items.download = {
            name: "Download",
            disabled: this.get_isMultiSelect()
        };

        config.items.sep1 = "";

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

    },

    executeFileOption: function (command, data) {

        var that = this;

        switch (command) {

            case "download":

                ProspectDirector.RUNTIME.iframeRequest("~/file/download/" + data.ObjectId);
                break;

            case "open":

                window.location = ProspectDirector.RUNTIME.resolveUrl("~/explore/") + data.ObjectParentId + "/folder/" + data.d0_general_FolderId;
                break;

            case "openInNewWindow":

                window.open(ProspectDirector.RUNTIME.resolveUrl("~/explore/") + data.ObjectParentId + "/folder/" + data.d0_general_FolderId);
                break;

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

                break;

        }

    },

    configureEntityOptions: function (config) {

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

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

        config.items.sep1 = "";

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

        config.items.addToFavorites = {
            name: "Add to Favorites"
        };

        config.items.sep2 = "";

        config.items.delete = {
            name: "Delete"
        };

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

    },

    executeEntityOption: function (command, data) {

        var that = this,
            viewGrid = this.get_module().get_grid().data("viewGrid"),
            ids = viewGrid.get_selection().get_selections();

        switch (command) {

            case "open":

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

            case "openInNewWindow":

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

            case "reports":

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

                break;

            case "addToFavorites":

                ProspectDirector.Modules.Personalization.AddToFavoritesDialog.Dialog.show({
                    params: {
                        ids: JSON.stringify(this.get_selections())
                    }
                });

                break;

            case "delete":

                PD.Web.UI.MessageDialog.warn({
                    title: "Send to Recycle Bin",
                    message: "Send the " + ids.length + " item(s) to the Recycle Bin?",
                    ok: function (e) {
                        e.close();
                        that._deleteEntities(ids);
                    }
                });

                break;

            case "properties":

                var that = this;

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

        }

    },

    _deleteEntities: function (ids) {

        var viewGrid = this.get_module().get_grid().data("viewGrid");

        PD.Shared.Dialogs.TaskStatus.open({
            config: {
                title: "Send to Recycle Bin"
            },
            showDetail: true,
            enableClose: false,
            taskStart: function (args) {

                PD.ajax({
                    type: "POST",
                    url: PD.Path.resolve("~/Shared/RecycleBin/Send"),
                    data: {
                        ids: ids,
                        status: 4
                    },
                    success: function (e) {
                        args.startPolling(e);
                    },
                    error: function (e) {
                        args.abort(e.responseJSON);
                        e.errorHandled = true;
                    }
                });

            },
            taskSuccess: function (e) {
                viewGrid.refresh();
                e.enableClose(true);
            }
        });

    }

});