/// class PD.Manage.Modules.UserMembershipFeature

/**
@class UserMembershipFeature
@constructor
@namespace PD.Manage.Modules
@extends Web.UI.FeatureSets.MacroEditorBase
**/
PD.Manage.Modules.UserMembershipFeature =
ProspectDirector.Web.UI.FeatureSets.MacroEditorBase.extend({

    //==============================
    // FIELDS
    //==============================

    _area: null,
    _viewGrid: null,
    _toolbar: null,
    _membership: null,

    //==============================
    // INITIALIZERS
    //==============================

    _init_controls: function () {

        var that = this;

        this._membership = {
            add: [],
            remove: []
        };

        this._init_area();
        this._init_grid();
        this._init_toolbar();

        PD.context.get_default().get_events().subscribe("user/memberships-added", function (e) {
            that._viewGrid.refresh({
                clear: true,
                selections: e.members
            });
        });

        PD.context.get_default().get_events().subscribe("user/memberships-removed", function (e) {
            that._viewGrid.refresh({
                clear: true
            });
        });

    },

    _init_area: function () {

        this._area = new PD.Web.UI.Controls.Control({
            container: this.get_container(),
            template: [
                "<div style='height:100%;'>",
                    "<div data-id='toolbar-area' style='position:absolute;height:30px;top:10px;left:10px;right:10px;border-width:0px;overflow:hidden;'>",
                    "</div>",
                    "<div data-id='grid-area' style='position:absolute;top:50px;left:10px;bottom:10px;right:10px;border:1px solid #DDDDDD;'></div>",
                "</div>",
            ]
        });

    },

    _init_grid: function () {

        var that = this;

        this._viewGrid = new PD.Web.UI.Controls.ViewGrid({
            insertAt: "[data-id=grid-area]",
            height: "100%",
            viewStateId: that.get_data()["viewStateId"],
            defaultSelectionEnabled: false,
            change: function (e) {

                PD.context.get_default().get_commands().setState({
                    "pd/users": {
                        groups: e.selections.toArray(),
                        membership: that._membership
                    }
                });

            },
            needContext: function (e) {
                e.filters[0] = {
                    id: "Members",
                    params: {
                        targetId: that.get_data()["userId"],
                        include: that._membership.add,
                        exclude: that._membership.remove
                    }
                };
            }
        });

        this._area.get_children().add(this._viewGrid);

    },

    _init_toolbar: function () {

        var that = this,
            grid = this._viewGrid;

        this._toolbar = new PD.Web.UI.Controls.Toolbar({
            insertAt: "[data-id=toolbar-area]",
            backgroundColor: "#FFFFFF",
            segments: [
                new PD.Manage.Modules.UserMembershipToolbarSegment()
            ]
        });

        this._area.get_children().add(this._toolbar);

    },

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

    activate: function () {
        this._init_controls();
    },

    commit: function () {
        if (this._membership && this._membership.add && this._membership.remove) {
            this.setValue("membershipsToAdd", this._membership.add);
            this.setValue("membershipsToRemove", this._membership.remove);
        }
    },

    size: function () {
        if (this._viewGrid) {
            this._viewGrid.redraw();
        }
    },

    render: function () {
        return "<div id='" + this.getId() + "' style='padding:10px;'></div>";
    }

});