/// class PD.Manage.Modules.UserBatchEditCommand

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

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

    setDefaults: function () {
        this.props("id", "user/bulk-edit");
        this.props("enabled", true);
        this.props("icon", PD.Path.to16x16("pencil.png"));
        this.props("label", "Bulk Edit");
        this.props("tooltip", "Bulk Edit properties of 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");

        if (_.isArray(users)) {
            this._showDialog(_.map(users, "id"));
        }

    },

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

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

    _showDialog: function (ids) {

        var that = this;

        PD.Manage.Modules.UserBatchEditDialog.open({
            callbacks: {
                ok: function (sender, e) {
                    that._startTask(_.extend(e, {
                        ids: ids
                    }));
                }
            }
        });

    },

    _startTask: function (data) {

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

                PD.ajax({
                    type: "POST",
                    url: PD.Path.resolve("~/Manage/Users/BulkEdit"),
                    data: data,
                    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("grid/refresh");

            }
        });

    }

});