/// class PD.Manage.Modules.Resources.BrowserDropOnSelf
// using PD.Web.UI.Controls.GridModule

PD.Manage.Modules.Resources.BrowserDropOnSelf =
PD.Web.UI.Controls.GridModule.extend({

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

    _dropTargetArea: null,

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

    start: function () {
        this._super();

        var $gridContent = this.get_kendo().content;

        this._dropTargetArea = $gridContent.kendoDropTargetArea({
            filter: "TR[data-uid][role=row]",
            drop: $.proxy(this._onDrop, this),
            dragenter: $.proxy(this._onDragEnter, this),
            dragleave: $.proxy(this._onDragLeave, this)
        }).data("kendoDropTargetArea");

    },

    unload: function () {
        if (this._dropTargetArea != null) {
            this._dropTargetArea.destroy();
        }
    },

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

    _onDragEnter: function (e) {

        var data = this._getEventData(e),
            $hint = $(e.draggable.hint),
            $subtext = $hint.find(".subtext");

        if (data.item != null && data.item.ObjectType === "folder") {

            $subtext.show();
            $subtext.html("Move to <b>" + data.item.ObjectName + "</b>");

        }

    },

    _onDragLeave: function (e) {

        var $hint = $(e.draggable.hint),
            $subtext = $hint.find(".subtext");

        $subtext.hide();
        $subtext.html("");

    },

    _onDrop: function (e) {

        var data = this._getEventData(e),
            selections = data.selections.toArray(),
            sourceIds = _.map(selections, "id"),
            isRow = e.draggable.currentTarget.attr("role") === "row";

        if (isRow) {

            PD.context.get_default().get_commands().execute("pd/resources:resource/move", {
                destination: {
                    id: data.item.id,
                    name: data.item.Name,
                    type: data.item.ObjectType,
                    mimeType: data.item.ObjectMimeType
                },
                selections: selections
            });

        }

    },

    _getEventData: function (e) {

        var result = {
                node: $(e.dropTarget),
                item: null,
                selections: this.get_grid().getSelections()
            },
            grid = this.get_kendo();

        result.item = grid.dataItem(result.node);

        return result;

    }

});