/// class PD.Manage.Modules.Rules.Rulesets
/// using PD.Manage.Module

PD.Manage.Modules.Rules.Rulesets =
PD.Manage.Module.extend({

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

    _grid: null,
    _toolbar: null,

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

    onPrepare: function () {
    
        var that = this;

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


    },

    onInit: function (e) {

        var that = this;

        this._initGrid();
        this._initToolbar();

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

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

        this.subscriptions.add("rule-script/update", function (e) {
            that._grid.refresh();
        });

    },

    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;top:10px;left:10px;right:10px;height:30px;'></div>",
                "<div data-id='grid' style='position:absolute;top:50px;left:10px;right:10px;bottom:10px;border:1px solid #DDDDDD;'></div>",
            "</div>"
        ];
    },

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

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

    _initGrid: function () {

        var that = this;

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

                PD.context.get_default().get_commands().setState({
                    "pd/rulesets": {
                        rulescript: e.selections.first()
                    }
                });

            },
            dblclick: function (e) {
                PD.context.get_default().get_commands().execute("pd/rulesets:rule-script/open");
            },
            contextMenu: {
                type: "PD.Manage.Modules.Rules.RulesetsContextMenu",
                context: function () {
                    return {
                        grid: that._grid
                    };
                }
            }
        });

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

    },

    _initToolbar: function () {

        var grid = this._grid;

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

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

    }


});