/// class PD.Manage.Modules.UserMembershipRemoveCommand

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

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

    setDefaults: function () {
        this.props("id", "user/memberships-remove");
        this.props("enabled", true);
        this.props("icon", PD.Path.to16x16("delete.png"));
        this.props("label", "Remove Membership");
        this.props("tooltip", "Remove Membership");
        this.state("groups", null);
        this.state("membership", null);
    },

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

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

        var groups = this.state("groups"),
            membership = this.state("membership");

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

        PD.context.get_default().get_events().publish("user/memberships-removed", function (e) {
            members: membership.remove
        });

    },

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

    _isEnabled: function () {

        var groups = this.state("groups"),
            membership = this.state("membership");

        return _.isArray(groups)
            && groups.length > 0
            && membership != null
            && _.isArray(membership.add)
            && _.isArray(membership.remove);

    }

});