/// class PD.Manage.Modules.Entities.EntityDeleteCommand
// PD.Commands.Command

PD.Manage.Modules.Entities.EntityDeleteCommand =
PD.Commands.Command.extend({

    //===================================
    // PUBLIC METHODS
    //===================================

    setDefaults: function () {

        this.props("id", "entity/delete");
        this.props("label", "Send to Recycle Bin");
        this.props("icon", PD.Path.to16x16("garbage.png"));
        this.props("tooltip", "Send the selected Entities to the Recycle Bin");

        this.state("entities", null);

    },

    setState: function (state) {
        this._super(state);
        this.props("enabled", this._isEnabled())
    },

    execute: function (e) {
        this._super(e);

        var entities = this._getEntities();

        this._showConfirmationDialog(_.map(entities, "id"));

    },

    //===================================
    // PRIVATE METHODS
    //===================================

    _isEnabled: function () {
        return this._getEntities().length > 0;
    },

    _getEntities: function () {

        var result = [],
            entities = this.state("entities");

        if (_.isArray(entities) && entities.length > 0) {
            result = entities;
        }

        return result;

    },

    _showConfirmationDialog: function (ids) {

        Jade.UI.MessageDialog.open({
            dialogStyle: Jade.UI.MessageDialogStyle.Warning,
            dialogButtons: Jade.UI.MessageDialogButtonStyle.OKCancel,
            allowClose: true,
            title: "Send to Recycle Bin",
            message: "Send the selected entities to the Recycle Bin?",
            select: (e) => {
                e.close();
                if (e.button === Jade.UI.MessageDialogButton.ok) {
                    this._showTaskDialog(ids);
                }
            }
        });

    },

    _showTaskDialog: function (ids) {

        PDJS.Shared.TaskStatusDialog.open({
            title: "Send to Recycle Bin",
            enableClose: false,
            taskStart: (args) => {

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

            },
            taskSuccess: (e) => {

                PD.context
                    .get_default()
                    .get_events()
                    .publish("entity/delete", {
                        ids: ids
                    });

                e.close();

            }
        });

    }

});