/// class PD.Manage.Modules.Users

PD.Manage.Modules.Users = PD.Manage.Module.extend({

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

    _grid: null,
    _toolbar: null,
    _pane: null,

    //===================================
    // PROPERTIES
    //===================================

    get_paneNode: function () {
        return this.get_node().find("div[data-id=pane]");
    },

    //===================================
    // EVENT HANDLERS
    //===================================

    onInit: function () {

        var that = this;

        this.subscriptions.add("user/create", function (e) {
            that._grid.refresh({
                clear: true,
                selections: [e.id]
            });
        });

        this.subscriptions.add("user/edit", function (e) {
            that._grid.refresh();
        });

        this.subscriptions.add("user/delete", function (e) {
            that._grid.refresh({
                clear: true
            });
        });

        this.subscriptions.add("sync/refresh", function (e) {
            that._grid.refresh();
        });

        this.subscriptions.add("grid/refresh", function (e) {
            that._grid.refresh();
        });

    },

    onActivate: function () {

        this._grid_init();
        this._toolbar_init();
        //this._pane_init();

    },

    prepare: function () {

        var that = this;

        PD.ajax({
            url: PD.Path.resolve("~/Manage/Users"),
            success: function (data) {
                that.data("viewStateId", data.viewStateId);
                that.ready();
            },
            error: function (e) {
                that.error();
                e.errorHandled = true;
            }
        });

    },

    //===================================
    // PROTECTED METHODS
    //===================================

    template: function (e) {
        return [
            "<div style='position:absolute;top:0;left:0;bottom:0;right:0;overflow:hidden;'>",
                "<div data-id='toolbar' style='position:absolute;height:30px;top:10px;left:10px;right:10px;'></div>",
                "<div data-id='grid' style='position:absolute;top:50px;left:10px;bottom:10px;right:10px;border:1px solid #DDDDDD;'></div>",
                // "<div data-id='pane' style='position:absolute;top:50px;width:400px;bottom:10px;right:10px;'></div>",
            "</div>"
        ];
    },

    focus: function () {
        this._super();
        this._grid.redraw();
    },

    //===================================
    // PRIVATE METHODS
    //===================================
    
    _grid_init: function () {

        var that = this;

        this._grid = new PD.Web.UI.Controls.ViewGrid({
            insertAt: "[data-id=grid]",
            viewStateId: this.data("viewStateId"),
            selectionMode: "multiple",
            selectOnRightClick: true,
            selection: {
                map: function (e) {
                    return _.map(e.selections.toArray(), function (item, index) {
                        return {
                            id: item.ObjectId,
                            name: item.ObjectName
                        };
                    });
                }
            },
            change: function (e) {

                PD.context
                    .get_default()
                    .get_commands()
                    .setState({
                    "pd/users": {
                        users: e.selections.toArray()
                }});

            },
            dblclick: function () {
                PD.context
                    .get_default()
                    .get_commands()
                    .execute("pd/users:user/edit-dialog");
            },
            contextMenu: {
                type: "PD.Manage.Modules.UsersContextMenuSegment",
                context: function () {
                    return {
                        viewGrid: that._grid
                    };
                }
            }
        });

        this.get_children().add(this._grid);

    },

    _toolbar_init: function () {

        var grid = this._grid;

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

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

    },

    _pane_init: function () {

        this._pane = new PDJS.Manage.Users.ChartPane({
            container: this.get_paneNode()
        });

    }

});