Tomahawk::result_ptr
PlayableProxyModelPlaylistInterface::currentItem() const
{
    if ( m_proxyModel.isNull() )
        return Tomahawk::result_ptr();

    PlayableProxyModel* proxyModel = m_proxyModel.data();

    PlayableItem* item = proxyModel->itemFromIndex( proxyModel->mapToSource( proxyModel->currentIndex() ) );
    if ( item && !item->query().isNull() && item->query()->playable() )
        return item->query()->results().at( 0 );
    return Tomahawk::result_ptr();
}
QList< Tomahawk::query_ptr >
PlayableProxyModelPlaylistInterface::tracks()
{
    if ( m_proxyModel.isNull() )
        return QList< Tomahawk::query_ptr >();

    PlayableProxyModel* proxyModel = m_proxyModel.data();
    QList<Tomahawk::query_ptr> queries;

    for ( int i = 0; i < proxyModel->rowCount( QModelIndex() ); i++ )
    {
        PlayableItem* item = proxyModel->itemFromIndex( proxyModel->mapToSource( proxyModel->index( i, 0 ) ) );
        if ( item )
            queries << item->query();
    }

    return queries;
}
Tomahawk::result_ptr
PlayableProxyModelPlaylistInterface::siblingItem( int itemsAway, bool readOnly )
{
    qDebug() << Q_FUNC_INFO;

    if ( m_proxyModel.isNull() )
        return Tomahawk::result_ptr();

    PlayableProxyModel* proxyModel = m_proxyModel.data();

    QModelIndex idx = proxyModel->index( 0, 0 );
    if ( proxyModel->rowCount() )
    {
        if ( m_shuffled )
        {
            // random mode is enabled
            // TODO come up with a clever random logic, that keeps track of previously played items
            idx = proxyModel->index( qrand() % proxyModel->rowCount(), 0 );
        }
        else if ( proxyModel->currentIndex().isValid() )
        {
            idx = proxyModel->currentIndex();

            // random mode is disabled
            if ( m_repeatMode != PlaylistModes::RepeatOne )
            {
                // keep progressing through the playlist normally
                idx = proxyModel->index( idx.row() + itemsAway, 0 );
            }
        }
    }

    if ( !idx.isValid() && m_repeatMode == PlaylistModes::RepeatAll )
    {
        // repeat all tracks
        if ( itemsAway > 0 )
        {
            // reset to first item
            idx = proxyModel->index( 0, 0 );
        }
        else
        {
            // reset to last item
            idx = proxyModel->index( proxyModel->rowCount() - 1, 0 );
        }
    }

    // Try to find the next available PlaylistItem (with results)
    while ( idx.isValid() )
    {
        PlayableItem* item = proxyModel->itemFromIndex( proxyModel->mapToSource( idx ) );
        if ( item && item->query()->playable() )
        {
            qDebug() << "Next PlaylistItem found:" << item->query()->toString() << item->query()->results().at( 0 )->url();
            if ( !readOnly )
                proxyModel->setCurrentIndex( idx );
            return item->query()->results().at( 0 );
        }

        idx = proxyModel->index( idx.row() + ( itemsAway > 0 ? 1 : -1 ), 0 );
    }

    if ( !readOnly )
        proxyModel->setCurrentIndex( QModelIndex() );
    return Tomahawk::result_ptr();
}
qint64
PlayableProxyModelPlaylistInterface::siblingIndex( int itemsAway, qint64 rootIndex ) const
{
    if ( m_proxyModel.isNull() )
        return -1;

    PlayableProxyModel* proxyModel = m_proxyModel.data();

    while ( m_shuffleHistory.count() && m_shuffleHistory.count() >= proxyModel->rowCount() )
    {
        m_shuffleHistory.removeFirst();
    }

    QModelIndex idx = QModelIndex();
    if ( proxyModel->rowCount() )
    {
        if ( m_shuffled )
        {
            if ( itemsAway < 0 )
            {
                if ( m_shuffleHistory.count() > 1 )
                {
                    if ( proxyModel->itemFromQuery( m_shuffleHistory.at( m_shuffleHistory.count() - 2 ) ) )
                        idx = proxyModel->mapFromSource( proxyModel->itemFromQuery( m_shuffleHistory.at( m_shuffleHistory.count() - 2 ) )->index );
                }
                else
                    return -1;
            }
            else
            {
                // random mode is enabled
                if ( m_shuffleCache.isValid() )
                {
                    idx = m_shuffleCache;
                }
                else
                {
                    int safetyCounter = 0;
                    PlayableItem* item = 0;
                    do
                    {
                        safetyCounter++;
                        idx = proxyModel->index( qrand() % proxyModel->rowCount(), 0 );
                        item = proxyModel->itemFromIndex( proxyModel->mapToSource( idx ) );
                    }
                    while ( safetyCounter < proxyModel->rowCount() &&
                          ( !item || !item->query()->playable() || m_shuffleHistory.contains( item->query() ) ) );

                    if ( item && item->query()->playable() )
                    {
                        m_shuffleCache = idx;
                        tDebug( LOGVERBOSE ) << "Next shuffled PlaylistItem cached:" << item->query()->toString() << item->query()->results().at( 0 )->url()
                                             << "- after" << safetyCounter << "tries to find a track";
                    }
                    else
                    {
                        tDebug() << Q_FUNC_INFO << "Error finding next shuffled playable track";
                    }
                }
            }
        }
        else
        {
            if ( m_repeatMode == PlaylistModes::RepeatOne )
            {
                idx = proxyModel->currentIndex();
            }
            else
            {
                // random mode is disabled
                if ( rootIndex == -1 )
                {
                    idx = proxyModel->currentIndex();
                }
                else
                {
                    PlayableItem* pitem = reinterpret_cast<PlayableItem*>( (void*)rootIndex );
                    if ( !pitem || !pitem->index.isValid() )
                        return -1;

                    idx = proxyModel->mapFromSource( pitem->index );
                }

                idx = proxyModel->index( idx.row() + itemsAway, 0 );
            }
        }
    }

    if ( !idx.isValid() && m_repeatMode == PlaylistModes::RepeatAll )
    {
        // repeat all tracks
        if ( itemsAway > 0 )
        {
            // reset to first item
            idx = proxyModel->index( 0, 0 );
        }
        else
        {
            // reset to last item
            idx = proxyModel->index( proxyModel->rowCount() - 1, 0 );
        }
    }

    while ( idx.isValid() )
    {
        PlayableItem* item = proxyModel->itemFromIndex( proxyModel->mapToSource( idx ) );
        if ( item )
        {
            return (qint64)( item->index.internalPointer() );
        }

        idx = proxyModel->index( idx.row() + ( itemsAway > 0 ? 1 : -1 ), 0 );
    }

    return -1;
}