/// class PD.Manage.Modules.Actions.ActionSelectionBrowser
// using PD.Web.UI.Controls.Control

PD.Manage.Modules.Actions.ActionSelectionBrowser =
PD.Web.UI.Controls.Control.extend({

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

    _grid: null,

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

    onInit: function () {
        this._initGrid();
    },

    onActivate: function (e) {
        this.subscriptions.add("folder/select", _.bind(this._folder_select, this));
    },

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

    _folder_select: function (e) {
    
        this._grid.refresh({
            clear: true,
            selection: null
        });

        this._grid.get_dataSource().data(this.getActions(e.folderId));
        
    },

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

    template: function () {
        return [
            "<div>",
                "<div data-id='grid' style='position:absolute;top:0;left:0;bottom:0;right:0;border:1px solid #DDDDDD;overflow:auto;'></div>",
            "</div>"
        ];
    },

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

    _initGrid: function () {

        var that = this;

        this._grid = new PD.Web.UI.Controls.Grid({
            insertAt: "[data-id=grid]",
            showToolbar: false,
            defaultSelectionEnabled: false,
            footer: {
                visible: false
            },
            dataSource: {
                data: []
            },
            selection: {
                map: function (e) {
                    return _.map(e.selections.toArray(), function (item, index) {
                        return {
                            id: item.id,
                            pluginId: item.pluginId
                        };
                    });
                }
            },
            messaging: {
                noRecordsMessage: "No actions found."
            },
            selectionMode: "single",
            columns: [
                new PD.Web.UI.Controls.GridIconColumn({
                    field: "id",
                    width: "25px",
                    icon: "FLASH_YELLOW.PNG"
                }),
                new PD.Web.UI.Controls.GridColumn({
                    field: "name",
                    title: "Action",
                    width: "150px"
                }),
                new PD.Web.UI.Controls.GridColumn({
                    field: "description",
                    title: "Description",
                    width: "250px"
                }),
                new PD.Web.UI.Controls.GridColumn({
                    field: "id",
                    title: "id",
                    width: "10px",
                    hidden: true
                }),
                new PD.Web.UI.Controls.GridFillColumn()
            ],
            change: function (e) {

                if (!e.isUserInitiatedEvent) {
                    return;
                }

                var item = _.first(e.selections.toArray());

                PD.context
                    .get_default()
                    .get_events()
                    .publish("action/select", item);

            },
            dblclick: function (e) {

                var item = _.first(e.selections.toArray());

                PD.context
                    .get_default()
                    .get_events()
                    .publish("action/selected", item);

            }
        });

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

    },

    getActions: function (pluginId) {

        var plugin = PD.Plugins.PluginManager.getPlugin(pluginId);
        var result = [];

        if (plugin != null) {

            result = _.map(plugin.get_actions().getActionDefinitions(), function (action) {
                return action.toLiteral();
            });

        }

        return result;

    }

});