/// class PD.Manage.Modules.SystemSettings

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

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

    _grid: null,

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

    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>"
        ];
    },

    onInit: function () {
        
        var that = this;

        this.subscriptions.add("system-setting/edit", function (data) {
            that._grid.refresh();
        });

        this._toolbar_Init();

    },

    onActivate: function (e) {

        var that = this;

        this._grid = new PD.Web.UI.Controls.ViewGrid({
            insertAt: "[data-id=grid]",
            viewStateId: this.data("viewStateId"),
            selectionMode: "single",
            selectOnRightClick: true,
            change: function (e) {

                var selection = e.selection;

                PD.context.get_default().get_commands().setState({
                    "pd/system-setting": {
                        systemSettingId: selection ? selection.id : null,
                        systemSettingUi: selection ? selection.UiSettings : null
                    }
                });

            },
            dblclick: function () {
                PD.context.get_default().get_commands().execute("pd/system-setting:system-setting/edit");
            },
            contextMenu: {
                type: "PD.Manage.Modules.SystemSettingsContextMenuSegment",
                context: function () {
                    return {
                        viewGrid: that._grid,
                        setting: that._grid.getSelection()
                    };
                }
            }
        });

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

    },

    prepare: function () {

        var that = this;

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

    },

    _toolbar_Init: function () {

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

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

    },

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

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

});