﻿Ext.ux.PlayerGridPanel = Ext.extend(Ext.ux.ContentPanelGridPanel, {

    // internal fields
    m_playClickCallback: null,


    // standard initializer
    initComponent: function() {
        Ext.ux.PlayerGridPanel.superclass.initComponent.apply(this, arguments);

        // args have to go here as base class initComponent doesn't get called for some reason
        var config = {
            view: new Ext.ux.PlayerGridView({ scrollOffset: g_gridScrollOffset })
        };

        Ext.apply(this, config);
        Ext.apply(this.initialConfig, config);

        this.on('rowcontextmenu', this.onRowContextMenu, this);
    },


    onRowContextMenu: function(grid, rowIndex, e) {

        if (!this.getSelectionModel().isSelected(rowIndex)) {
            this.getSelectionModel().selectRow(rowIndex);
        }

        var selectionCount = this.getSelectionModel().getCount();

        // create the context menu appropriate to the grid display
        this.contextMenu = this.createPlayerActionsContextMenu(grid, selectionCount);

        Ext.ux.PlayerGridPanel.superclass.onRowContextMenu.call(this, grid, rowIndex, e);
    },

    // configure drop target
    configureDragDrop: function(ddGroup) {
        var dropTargetEl = this.body;
        var grid = this;

        var dropTarget = new Ext.ux.PlayerDropTarget(dropTargetEl, {
            ddGroup: ddGroup,
            grid: grid,

            // called when data is dropped
            notifyDrop: function(ddSource, e, data) {
                var positionInfo = this.getPositionInfo(e);
                grid.handleDrop(positionInfo.positionQualifierEnum, positionInfo.targetElementId, data);

                // don't call base class
            }

            // see PlayerDropTarget below for other functions
        });
    },

    // handle the dropping of some records
    handleDrop: function(positionQualifierEnum, targetEntryId, data) {

        // extract the type from the first item
        var type = data.selections[0].data.type;

        // build up array of ids
        var idArray = this.createIdArray(type, data.selections);

        // process items
        if (type == g_extJsGrid_dataTypePlaylistEntry) {
            // reorder in playlist
            ajax_reorderStream(positionQualifierEnum, targetEntryId, idArray, delegate(this, this.reload));
        }
    },

    // ------ callback helper functions ------ //

    // set the callback to be called to get the stream url
    // called when play is clicked from within the 
    setPlayClickCallback: function(theCallback) {
        this.m_playClickCallback = theCallback;
    },

    // ------ context menu creation helper functions ------ //

    // create a context menu for a record in the cloud
    createPlayerActionsContextMenu: function(grid, selectionCount) {

        var contextMenu = this.createEmptyActionsContextMenu();

        var selectAllClickedWrapper = function() {
            grid.getSelectionModel().selectAll();
        };

        var handlePlayClickedWrapper = function() {

            var selections = grid.getSelectionModel().getSelections();
            var rowIndex = grid.store.indexOf(selections[0]);

            if (grid.m_playClickCallback) {
                grid.m_playClickCallback(rowIndex);
            }
        };

        var handleRemoveClickedWrapper = function() {
            var selections  = grid.getSelectionModel().getSelections();
            var idArray     = grid.createIdArray(g_extJsGrid_dataTypePlaylistEntry, selections);

            ajax_removeFromStream(idArray, delegate(grid, grid.reload)); // note scope is 'grid', not 'this'
        };

        // add actions to actions context menu (shown by listener in parent class)
        contextMenu.add(new Ext.menu.Item({
            text: g_resourceStrings['Js_Grid_PopupMenu_PlayNow'],
            handler: handlePlayClickedWrapper,
            disabled: selectionCount > 1
        }));

        contextMenu.add(new Ext.menu.Item({
            text: g_resourceStrings['Js_PlayerGrid_PopupMenu_RemoveFromPlaylist'],
            handler: handleRemoveClickedWrapper
        }));

        contextMenu.add(new Ext.menu.Item({
            text: g_resourceStrings['Js_Grid_PopupMenu_SelectAll'],
            handler: selectAllClickedWrapper
        }));

        return contextMenu;
    }
});

Ext.ux.PlayerGridView = Ext.extend(Ext.ux.ContentPanelGridView, {

    afterRender: function() {
        Ext.ux.PlayerGridView.superclass.afterRender.call(this, arguments);
        this.dragZone = new Ext.ux.PlayerGridDragZone(this.grid, { ddGroup: "" });
    }
});

// configure drag & drop
Ext.ux.PlayerGridDragZone = Ext.extend(Ext.ux.ContentPanelGridDragZone, {
});

Ext.ux.PlayerDropTarget = Ext.extend(Ext.ux.ContentPanelDropTarget, {
});