/// class PD.Manage.Modules.GroupMembersAddCommand

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

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

    setDefaults: function () {
        this.props("id", "group/members-add");
        this.props("enabled", true);
        this.props("icon", PD.Path.to16x16("plus.png"));
        this.props("label", "Add Member");
        this.props("tooltip", "Add Member");
        this.state("membership", null);
    },

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

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

        this._showDialog(function (e) {

            var selections = e;
            var members = [];
            var membership = this.state("membership");

            _.each(selections, function (member) {
                members[members.length] = member.id;
                membership.remove = _.without(membership.remove, member.id);
                if (!_.includes(membership.add, member.id)) {
                    membership.add[membership.add.length] = member.id;
                }
            });

            PD.context.get_default().get_events().publish("group/members-added", {
                members: members
            });

        });

    },

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

    _isEnabled: function () {
        var membership = this.state("membership");
        return membership != null
            && _.isArray(membership.add)
            && _.isArray(membership.remove);
    },

    _showDialog: function (callback) {

        callback = _.bind(callback, this);

        PDJS.Shared.Security.IdentityBrowserDialog.open({
            title: "Find Users",
            filters: [PDJS.Core.Views.IdentityFilter.usersOnly()],
            callbacks: {
                ok: function (sender, e) {
                    callback(e);
                }
            }
        });
        
    }

});