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

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

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

    _grid: null,
    _location: null,
    _selectedFolderId: null,

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

    onInit: function (e) {

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

        this._initGrid();
        this._location_init();

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

        this.subscriptions.add("folder/edit", function (e) {
            if (e.parentId = that._selectedFolderId) {
                that._grid.refresh();
            }
        });

        this.subscriptions.add("file/edit", function (e) {
            that._grid.refresh();
        });

        this.subscriptions.add("location/changed", function (e) {
            PD.context.get_default().get_events().publish("folder/open", {
                folderId: e.folderId
            });
        });

        this.subscriptions.add("folder/open", folder_open);
        this.subscriptions.add("resources/delete", refresh_grid);
        this.subscriptions.add("resources/add", refresh_grid);
        this.subscriptions.add("resource/move", refresh_grid);

        PD.context
            .get_global()
            .get_events()
            .subscribe("Window:Resized", function () {
                if (that._location != null) {
                    that._location.resize();
                }
            });
       
    },

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

    template: function () {
        return [
            "<div>",
                "<div data-id='location' style='position:absolute;box-sizing:border-box;top:0;height:30px;right:0;left:0;'></div>",
                "<div data-id='grid' style='position:absolute;top:40px;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.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
                        };
                    });
                }
            },
            defaultSelectionEnabled: false,
            selectionMode: "multiple",
            selectOnRightClick: true,
            needContext: function (e) {
                e.filters[e.filters.length] = {
                    id: "Parent",
                    params: {
                        id: that._selectedFolderId == "00000000-0000-0000-0000-000000000000" ? null : that._selectedFolderId
                    }
                };
            },
            dropOnTree: {
                drop: function () {
                }
            },
            change: function (e) {
                PD.context.get_default().get_commands().setState({
                    "pd/resources": {
                        targetFolderId: that._selectedFolderId,
                        resources: e.selections.toArray()
                    }
                });
            },
            dblclick: function (e) {

                if (e.selection.type === "file") {
                    PD.context.get_default().get_commands().execute("pd/resources:resource/open");
                }
                else {

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

                }
                
            },
            contextMenu: {
                type: "PD.Manage.Modules.Resources.BrowserContextMenu",
                context: function () {
                    return {
                        grid: that._grid
                    };
                }
            },
            modules: [
                "PD.Web.UI.Controls.GridDragModule",
                "PD.Manage.Modules.Resources.BrowserDropOnSelf",
                "PD.Manage.Modules.Resources.BrowserDropOnTree"
            ]
        });

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

    },

    _location_init: function () {

        this._location = new PD.Manage.Modules.Resources.LocationBar({
            insertAt: "DIV[data-id=location]"
        });

        this.get_children().add(this._location);

    }

});