/// 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) {

        var that = this;

        ProspectDirector.Web.UI.MessageDialog.confirm({
            message: "Delete the selected users?",
            buttons: {
                ok: {
                    label: "OK",
                    callback: function (e) {
                        that._startTask(ids);
                    }
                },
                cancel: {
                    label: "Cancel"
                }
            }
        });

    },

    _startTask: function (ids) {

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

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

            },
            close: function (e) {
                PD.context.get_default().get_events().publish("user/delete");
            }
        });

    }

});