/// class PD.Manage.Modules.UserDeleteCommand

PD.Manage.Modules.UserDeleteCommand =
PD.Commands.Command.extend({

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

    setDefaults: function () {
        this.props("id", "user/delete");
        this.props("enabled", true);
        this.props("icon", PD.Path.to16x16("delete.png"));
        this.props("label", "Delete");
        this.props("tooltip", "Delete the selected Users.");
        this.state("users", null);
    },

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

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

        var users = this.state("users");

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

    },

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

    _isEnabled: function () {
        return _.isArray(this.state("users")) && this.state("users").length > 0;
    },

    _showConfirmationDialog: function (ids) {

        Jade.UI.MessageDialog.open({
            dialogStyle: Jade.UI.MessageDialogStyle.Warning,
            dialogButtons: Jade.UI.MessageDialogButtonStyle.OKCancel,
            allowClose: true,
            title: "Delete Users",
            message: "Delete the selected users?",
            select: (e) => {
                e.close();
                if (e.button === Jade.UI.MessageDialogButton.ok) {
                    this._startTask(ids);
                }
            }
        });

    },

    _startTask: function (ids) {

        PDJS.Shared.TaskStatusDialog.open({
            title: "Delete | Users",
            taskStart: (e) => {

                PD.ajax({
                    type: "POST",
                    url: PD.Path.resolve("~/Manage/Users/Delete"),
                    data: {
                        ids: ids
                    },
                    success: (e2) => {
                        e.startPolling(e2);
                    },
                    error: (e2) => {
                        e.abort(e2.responseJSON);
                        e2.errorHandled = true;
                    }
                });

            },
            taskSuccess: (e) => {
                PD.context
                    .get_default()
                    .get_events()
                    .publish("user/delete");
            }
        });

    }

});