/// class PD.Manage.Modules.LicenseManager
// using PD.Manage.Module

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

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

    _grid: null,
    _toolbar: null,

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

    onActivate: function (e) {

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

        this.subscriptions.add("license/deleted", function (e) {
            that._grid.refresh({
                clear: true
            });
        });
        
        this._grid_init();
        this._toolbar_init();

    },

    prepare: function () {

        var that = this;

        PD.ajax({
            url: PD.Path.resolve("~/Manage/Licenses"),
            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 class='-pd-license-manager'>",
                "<div data-id='toolbar'>",
                "</div>",
                "<div data-id='grid'>",
                "</div>",
            "</div>"
        ];
    },

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

    _grid_init: function () {

        var that = this;

        this._grid = new PD.Web.UI.Controls.ViewGrid({
            insertAt: "[data-id=grid]",
            viewStateId: this.data("viewStateId"),
            selectionMode: "single",
            selectOnRightClick: true,
            selection: {
                map: function (e) {
                    return _.map(e.selections.toArray(), function (item, index) {
                        return {
                            id: item.ObjectId,
                            name: item.ProductName,
                            status: item.Status
                        };
                    });
                }
            },
            dblclick: function (e) {
                PD.context.get_default().get_commands().execute("pd/license-manager:license/view");
            },
            change: function (e) {

                PD.context.get_default().get_commands().setState({
                    "pd/license-manager": {
                        selection: _.first(e.selections.toArray())
                    }
                });

            },
            contextMenu: {
                type: "PD.Manage.Modules.LicenseManagerContextMenu",
                context: function (e) {
                    return {
                        selection: that._grid.getSelections().toArray()
                    };
                }
            }
        });

        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.LicenseManagerToolbarSegment()
            ]
        });

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

    }

});