﻿Ext.ux.TracksGridPanelBase = Ext.extend(Ext.ux.ContentPanelGridPanel, {

    // determine if one or more of the currently selected tracks can be streamed
    isOneOrMoreSelectedTracksStreamable: function() {

        var isStreamable                = false;
        var selections                  = this.getSelectionModel().getSelections();
        var oneOrMoreTracksStreamable   = this.isOneOrMoreTracksStreamable(selections);

        return oneOrMoreTracksStreamable;
    },

    // determine if one or more tracks (represented by the specified record) can be streamed
    isOneOrMoreTracksStreamable: function(records) {

        var isStreamable = false;

        // check each record in turn
        for (var record = 0; ((record < records.length) && !isStreamable); record++) {
            if (records[record].data.allowstream && (records[record].data.filehashstatus == g_fileHashStatusEnum_recombineComplete)) {
                isStreamable = true;
            }
        }

        return isStreamable;
    },

    // add the menu items for the various track streaming options
    addStreamingMenuItems: function(contextMenu, rowIndex) {

        var selections;

        if (!this.getSelectionModel().isSelected(rowIndex)) {
            this.getSelectionModel().selectRow(rowIndex);
        }
        selections = this.getSelectionModel().getSelections();

        var oneOrMoreTracksStreamable = this.isOneOrMoreTracksStreamable(selections);
        var fileHashIdArray = this.createIdArray(g_extJsGrid_dataTypeTrack, selections);

        // add actions to actions context menu (shown by listener in parent class)
        if (g_showStreamingMenuItems) {
            contextMenu.add(new Ext.menu.Item({
                text: g_resourceStrings['Js_Grid_PopupMenu_PlayNow'],
                handler: function() { tracksGrid_handlePlayClicked(fileHashIdArray); },
                disabled: !oneOrMoreTracksStreamable
            }));

            contextMenu.add(new Ext.menu.Item({
                text: g_resourceStrings['Js_Grid_PopupMenu_PlayNext'],
                handler: function() { tracksGrid_handlePlayNextClicked(fileHashIdArray); },
                disabled: !oneOrMoreTracksStreamable
            }));

            contextMenu.add(new Ext.menu.Item({
                text: g_resourceStrings['Js_Grid_PopupMenu_AddToNowPlaying'],
                handler: function() { tracksGrid_handleAddToNowPlayingClicked(fileHashIdArray); },
                disabled: !oneOrMoreTracksStreamable
            }));
        }

        return contextMenu;
    }
});

Ext.ux.TracksGridViewBase = Ext.extend(Ext.ux.ContentPanelGridView, {

    // get the class for the row
    getRowClass: function(record, rowIndex) {

        var selectionModel = this.grid.getSelectionModel();
        var cssClass = '';

        if (selectionModel.isIdSelected(record.data.id)) {
            // selected
            if (record.data.allowstream && (record.data.filehashstatus == g_fileHashStatusEnum_recombineComplete)) {
                // streamable
                cssClass = 'x-grid3-row-selected';
            }
            else {
                // not streamable
                cssClass = 'x-grid3-row-selected-nostream';
            }
        }
        else if ((rowIndex + 1) % 2 === 0) {
            // alt
            if (record.data.allowstream && (record.data.filehashstatus == g_fileHashStatusEnum_recombineComplete)) {
                // streamable
                cssClass = 'x-grid3-row-alt';
            }
            else {
                // not streamable
                cssClass = 'x-grid3-row-alt-nostream';
            }
        }
        else {
            // normal
            if (record.data.allowstream && (record.data.filehashstatus == g_fileHashStatusEnum_recombineComplete)) {
                // streamable
                cssClass = 'x-grid3-row';
            }
            else {
                // streamable
                cssClass = 'x-grid3-row-nostream';
            }
        }

        // don't call base class

        return cssClass;
    },

    // get the selected class for the row
    onRowSelect: function(iRowIndex) {
        var record = this.grid.store.getAt(iRowIndex);
        if (record.data.allowstream && (record.data.filehashstatus == g_fileHashStatusEnum_recombineComplete)) {
            // streamable
            this.addRowClass(iRowIndex, "x-grid3-row-selected");
        }
        else {
            // not streamable
            this.addRowClass(iRowIndex, "x-grid3-row-selected-nostream");
        }
    },

    // remove the selected class for the row
    onRowDeselect: function(iRowIndex) {
        var record = this.grid.store.getAt(iRowIndex);
        if (record.data.allowstream && (record.data.filehashstatus == g_fileHashStatusEnum_recombineComplete)) {
            // streamable
            this.removeRowClass(iRowIndex, "x-grid3-row-selected");
        }
        else {
            // not streamable
            this.removeRowClass(iRowIndex, "x-grid3-row-selected-nostream");
        }
    },

    // get the mouseover class for the row
    onRowOver: function(e, t) {
        var iRowIndex;
        if ((iRowIndex = this.findRowIndex(t)) !== false) {
            var record = this.grid.store.getAt(iRowIndex);
            if (record.data.allowstream && (record.data.filehashstatus == g_fileHashStatusEnum_recombineComplete)) {
                // streamable
                this.addRowClass(iRowIndex, "x-grid3-row-over");
            }
            else {
                // streamable
                this.addRowClass(iRowIndex, "x-grid3-row-over-nostream");
            }
        }

        Ext.ux.TracksGridViewBase.superclass.onRowOver.call(this, e, t);
    },

    // remove the mouseover class for the row
    onRowOut: function(e, t) {
        var iRowIndex;
        if ((iRowIndex = this.findRowIndex(t)) !== false && !e.within(this.getRow(iRowIndex), true)) {
            var record = this.grid.store.getAt(iRowIndex);
            if (record.data.allowstream && (record.data.filehashstatus == g_fileHashStatusEnum_recombineComplete)) {
                // not streamable
                this.removeRowClass(iRowIndex, "x-grid3-row-over");
            }
            else {
                // streamable
                this.removeRowClass(iRowIndex, "x-grid3-row-over-nostream");
            }
        }

        Ext.ux.TracksGridViewBase.superclass.onRowOut.call(this, e, t);
    }
});

// configure drag & drop
Ext.ux.TracksGridDragZoneBase = Ext.extend(Ext.ux.ContentPanelGridDragZone, {
});

Ext.ux.TracksDropTargetBase = Ext.extend(Ext.ux.ContentPanelDropTarget, {
});