/// class PD.Manage.Modules.Resources.ResourceSelectionBrowser
// using PD.Web.UI.Controls.Control

PD.Manage.Modules.Resources.ResourceSelectionBrowser =
PD.Web.UI.Controls.Control.extend({

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

    _grid: null,
    _selectedFolderId: null,

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

    onActivate: function (e) {

        var that = this;
        var folder_open = function (e) {
            that._selectedFolderId = e.folderId;
            that._grid.refresh({
                clear: true
            });
        };

        this._initGrid();

        this.subscriptions.add("folder/open", folder_open);

        this.subscriptions.add("location/changed", function (e) {
            PD.context.get_default().get_events().publish("folder/open", {
                folderId: 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,
            selectMode = this.get_options()["selectMode"],
            selectType = selectMode.type ? "multiple" : "single",
            multiple = selectMode.multiple ? "multiple" : "single";

        this._grid = new PD.Web.UI.Controls.ViewGrid({
            insertAt: "[data-id=grid]",
            viewStateId: this.get_options()["viewStateId"],
            selection: {
                map: function (e) {
                    return _.map(e.selections.toArray(), function (item, index) {
                        return {
                            id: item.ObjectId,
                            name: item.ObjectName,
                            type: item.ObjectType,
                            mimeType: item.ObjectMimeType
                        };
                    });
                }
            },
            selectionMode: multiple,
            needContext: function (e) {
                that._addFilterByParent(e);
                that._addFilterByExtension(e);
            },
            change: function (e) {

                PD.context.get_default().get_commands().setState({
                    "pd/resources": {
                        targetFolderId: that._selectedFolderId,
                        resources: e.selections.toArray()
                    }
                });

                PD.context
                    .get_default()
                    .get_events()
                    .publish("resource/highlight", e.selections.toArray());

            },
            dblclick: function (e) {

                if (e.selection.type === "folder") {

                    PD.context
                         .get_default()
                         .get_events()
                         .publish("location/change", {
                             folderId: e.selection.id,
                             source: "browser"
                         });

                }
                else {

                    PD.context
                        .get_default()
                        .get_events()
                        .publish("resource/select", e.selections.toArray());

                }

            }
        });

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

    },

    _addFilterByParent: function (context) {

        var rootFolderId = "00000000-0000-0000-0000-000000000000",
            selectedFolderId = null;

        if (this._selectedFolderId != rootFolderId) {
            selectedFolderId = this._selectedFolderId;
        }

        context.filters[context.filters.length] = {
            id: "Parent",
            params: {
                id: selectedFolderId
            }
        };

    },

    _addFilterByExtension: function (context) {

        var extensions = this.get_options()["extensions"];

        if (_.isArray(extensions)) {

            context.filters[context.filters.length] = {
                id: "Extension",
                params: {
                    ext: extensions.join("|")
                }
            };

        }

    }

});