/**
 * @private
 */
Ext.define('Ext.grid.ViewDropZone', {
    extend: 'Ext.view.DropZone',
 
    indicatorHtml: '<div class="' + Ext.baseCSSPrefix +
                   'grid-drop-indicator-left" role="presentation"></div><div class="' +
                   Ext.baseCSSPrefix + 'grid-drop-indicator-right" role="presentation"></div>',
    indicatorCls: Ext.baseCSSPrefix + 'grid-drop-indicator',
 
    handleNodeDrop: function(data, record, position) {
        var view = this.view,
            store = view.getStore(),
            crossView = view !== data.view,
            selectAfter = crossView || data.records.length > 1,
            index, records, i, len;
 
        // If the copy flag is set, create a copy of the models
        if (data.copy) {
            records = data.records;
 
            for (i = 0, len = records.length; i < len; i++) {
                records[i] = records[i].copy();
            }
        }
        else if (selectAfter) {
            /*
             * Remove from the source store only if we are moving to a different store
             * or shifting multiple records.
             */
            data.view.store.remove(data.records);
        }
 
        if (record && position) {
            index = store.indexOf(record);
 
            // 'after', or undefined (meaning a drop at index -1 on an empty View)...
            if (position !== 'before') {
                index++;
            }
 
            // Insert one record at a time rather than passing the whole array to a single
            // store.insert() call. When the store is sorted, a multi-record insert always
            // merges records that compare equal to the end of their run (grouped stores
            // with one group being the extreme case: every record ties, so they all land
            // at the very end no matter where the drop happened), while a single-record
            // insert honours the drop index whenever doing so does not violate sort order.
            // Looping keeps a multi-row drop consistent with a single-row drop: landed
            // wherever the user drops it when the store has nothing to sort by, and
            // correctly re-sorted when it does.
            records = data.records;
 
            for (i = 0, len = records.length; i < len; i++) {
                store.insert(index + i, records[i]);
            }
        }
        // No position specified - append.
        else {
            store.add(data.records);
        }
 
        // Select the dropped nodes if needed
        if (selectAfter) {
            view.getSelectionModel().select(data.records);
        }
 
        // Focus the first dropped node after the view has settled from the refresh
        // caused by the store mutations above. Focusing synchronously can require the
        // BufferedRenderer to teleport-scroll to the dropped record (grouped/sorted
        // stores relocate dropped records) while the view's layout state is still
        // unflushed, silently clamping the scroll and leaving the rendered block
        // inconsistent with the scroll position.
        Ext.asap(function() {
            if (!view.destroyed) {
                view.getNavigationModel().setPosition(data.records[0]);
            }
        });
    }
});