/*
 *    Copyright 2010-2011, Leo Franchi <lfranchi@kde.org>
 *    Copyright 2010-2012, Jeff Mitchell <jeff@tomahawk-player.org>
 *
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 2 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License along
 *    with this program; if not, write to the Free Software Foundation, Inc.,
 *    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include "SourceItem.h"

#include "CategoryItems.h"
#include "PlaylistItems.h"
#include "ViewManager.h"
#include "Playlist.h"
#include "GenericPageItems.h"
#include "utils/TomahawkUtilsGui.h"
#include "utils/Logger.h"
#include "widgets/SocialPlaylistWidget.h"
#include "playlist/CustomPlaylistView.h"
#include "playlist/PlaylistView.h"
#include "playlist/RecentlyAddedModel.h"
#include "playlist/RecentlyPlayedModel.h"
#include "playlist/PlaylistLargeItemDelegate.h"
#include "Source.h"
#include "SourceList.h"

/// SourceItem

using namespace Tomahawk;

SourceItem::SourceItem( SourcesModel* mdl, SourceTreeItem* parent, const Tomahawk::source_ptr& source )
    : SourceTreeItem( mdl, parent, SourcesModel::Collection )
    , m_source( source )
    , m_playlists( 0 )
    , m_stations( 0 )
    , m_latchedOn( false )
    , m_sourceInfoItem( 0 )
    , m_coolPlaylistsItem( 0 )
    , m_collectionPage( 0 )
    , m_sourceInfoPage( 0 )
    , m_coolPlaylistsPage( 0 )
    , m_lovedTracksPage( 0 )
    , m_latestAdditionsPage( 0 )
    , m_recentPlaysPage( 0 )
    , m_whatsHotPage( 0 )
{
    if ( m_source.isNull() )
    {
        m_superCol = TomahawkUtils::createAvatarFrame( QPixmap( RESPATH "images/supercollection.png" ) );
        return;
    }

    m_collectionItem = new GenericPageItem( model(), this, tr( "Collection" ), QIcon( RESPATH "images/collection.png" ), //FIXME different icon
                                            boost::bind( &SourceItem::collectionClicked, this ),
                                            boost::bind( &SourceItem::getCollectionPage, this ) );

/*    m_sourceInfoItem = new GenericPageItem( model(), this, tr( "New Additions" ), QIcon( RESPATH "images/new-additions.png" ),
                                            boost::bind( &SourceItem::sourceInfoClicked, this ),
                                            boost::bind( &SourceItem::getSourceInfoPage, this ) );*/

    m_latestAdditionsItem = new GenericPageItem( model(), this, tr( "Latest Additions" ), QIcon( RESPATH "images/new-additions.png" ),
                                                 boost::bind( &SourceItem::latestAdditionsClicked, this ),
                                                 boost::bind( &SourceItem::getLatestAdditionsPage, this ) );

    m_recentPlaysItem = new GenericPageItem( model(), this, tr( "Recently Played" ), QIcon( RESPATH "images/recently-played.png" ),
                                             boost::bind( &SourceItem::recentPlaysClicked, this ),
                                             boost::bind( &SourceItem::getRecentPlaysPage, this ) );

    m_lovedTracksItem = new GenericPageItem( model(), this, tr( "Loved Tracks" ), QIcon( RESPATH "images/loved_playlist.png" ),
                                             boost::bind( &SourceItem::lovedTracksClicked, this ),
                                             boost::bind( &SourceItem::getLovedTracksPage, this ) );

    m_collectionItem->setSortValue( -350 );
//    m_sourceInfoItem->setSortValue( -300 );
    m_latestAdditionsItem->setSortValue( -250 );
    m_recentPlaysItem->setSortValue( -200 );
    m_lovedTracksItem->setSortValue( -150 );

    // create category items if there are playlists to show, or stations to show
    QList< playlist_ptr > playlists = source->collection()->playlists();
    QList< dynplaylist_ptr > autoplaylists = source->collection()->autoPlaylists();
    QList< dynplaylist_ptr > stations = source->collection()->stations();

    if ( !playlists.isEmpty() || !autoplaylists.isEmpty() || source->isLocal() )
    {
        m_playlists = new CategoryItem( model(), this, SourcesModel::PlaylistsCategory, source->isLocal() );
        onPlaylistsAdded( playlists );
        onAutoPlaylistsAdded( autoplaylists );
    }
    if ( !stations.isEmpty() || source->isLocal() )
    {
        m_stations = new CategoryItem( model(), this, SourcesModel::StationsCategory, source->isLocal() );
        onStationsAdded( stations );
    }

/*    if ( ViewManager::instance()->pageForCollection( source->collection() ) )
        model()->linkSourceItemToPage( this, ViewManager::instance()->pageForCollection( source->collection() ) );*/

    m_defaultAvatar = TomahawkUtils::createAvatarFrame( QPixmap( RESPATH "images/user-avatar.png" ) );

    // load auto playlists and stations!

    connect( source.data(), SIGNAL( stats( QVariantMap ) ), SIGNAL( updated() ) );
    connect( source.data(), SIGNAL( syncedWithDatabase() ), SIGNAL( updated() ) );
    connect( source.data(), SIGNAL( stateChanged() ), SIGNAL( updated() ) );
    connect( source.data(), SIGNAL( offline() ), SIGNAL( updated() ) );
    connect( source.data(), SIGNAL( online() ), SIGNAL( updated() ) );
    connect( SourceList::instance(), SIGNAL( sourceLatchedOn( Tomahawk::source_ptr, Tomahawk::source_ptr ) ), SLOT( latchedOn( Tomahawk::source_ptr, Tomahawk::source_ptr ) ) );
    connect( SourceList::instance(), SIGNAL( sourceLatchedOff( Tomahawk::source_ptr, Tomahawk::source_ptr ) ), SLOT( latchedOff( Tomahawk::source_ptr, Tomahawk::source_ptr ) ) );

    connect( source->collection().data(), SIGNAL( playlistsAdded( QList<Tomahawk::playlist_ptr> ) ),
             SLOT( onPlaylistsAdded( QList<Tomahawk::playlist_ptr> ) ), Qt::QueuedConnection );
    connect( source->collection().data(), SIGNAL( autoPlaylistsAdded( QList< Tomahawk::dynplaylist_ptr > ) ),
             SLOT( onAutoPlaylistsAdded( QList<Tomahawk::dynplaylist_ptr> ) ), Qt::QueuedConnection );
    connect( source->collection().data(), SIGNAL( stationsAdded( QList<Tomahawk::dynplaylist_ptr> ) ),
             SLOT( onStationsAdded( QList<Tomahawk::dynplaylist_ptr> ) ), Qt::QueuedConnection );

    if ( m_source->isLocal() )
        QTimer::singleShot( 0, this, SLOT( requestExpanding() ) );
}


Tomahawk::source_ptr
SourceItem::source() const
{
    return m_source;
}


QString
SourceItem::text() const
{
    return m_source.isNull() ? tr( "SuperCollection" ) : m_source->friendlyName();
}


QString
SourceItem::tooltip() const
{
    if ( !m_source.isNull() && !m_source->currentTrack().isNull() )
        return m_source->textStatus();

    return QString();
}


int
SourceItem::IDValue() const
{
    if ( m_source.isNull() )
        return -1;
    if ( m_source->isLocal() )
        return 0;

    return m_source->id();
}


int
SourceItem::peerSortValue() const
{
    if ( m_source.isNull() || m_source->isLocal() )
        return -1;

    return 1;
}


void
SourceItem::activate()
{
    ViewPage* p = 0;
    if ( source().isNull() )
        p = ViewManager::instance()->showSuperCollection();
    else
        emit toggleExpandRequest( this );

    model()->linkSourceItemToPage( this, p );
}


QIcon
SourceItem::icon() const
{
    if ( m_source.isNull() )
       return m_superCol;
    else
    {
        if ( m_source->avatar().isNull() )
            return m_defaultAvatar;
        else
            return m_source->avatar( Source::FancyStyle );
    }
}


bool
SourceItem::localLatchedOn() const
{
    // Don't show a listen icon if this is the local collection and we are latched on to someone who went offline
    // we are technically still latched on (if they come back online we'll be still listening along) but it's not visible
    // in the UI and confusing to the user why the red headphones are still there

    if ( !m_source.isNull() && m_source->isLocal() &&
         !m_latchedOnTo.isNull() && !m_latchedOnTo->isOnline() )
        return false;

    return m_latchedOn;
}


Tomahawk::PlaylistModes::LatchMode
SourceItem::localLatchMode() const
{
    if ( !m_source.isNull() && !m_source->isLocal() )
        return m_source->playlistInterface()->latchMode();

    return Tomahawk::PlaylistModes::StayOnSong;
}


void
SourceItem::latchedOff( const source_ptr& from, const source_ptr& to )
{
    if ( from->isLocal() && ( m_source == to || m_source == from ) )
    {
        m_latchedOn = false;
        disconnect( m_latchedOnTo->playlistInterface().data(), SIGNAL( latchModeChanged( Tomahawk::PlaylistModes::LatchMode ) ) );
        m_latchedOnTo.clear();
        emit updated();
    }
}


void
SourceItem::latchedOn( const source_ptr& from, const source_ptr& to )
{
    if ( from->isLocal() && ( m_source == to || m_source == from ) )
    {
        m_latchedOn = true;
        m_latchedOnTo = to;
        connect( m_latchedOnTo->playlistInterface().data(), SIGNAL( latchModeChanged( Tomahawk::PlaylistModes::LatchMode ) ), SLOT( latchModeChanged( Tomahawk::PlaylistModes::LatchMode ) ) );
        emit updated();
    }
}


void
SourceItem::latchModeChanged( Tomahawk::PlaylistModes::LatchMode mode )
{
    Q_UNUSED( mode );
    emit updated();
}


void
SourceItem::playlistsAddedInternal( SourceTreeItem* parent, const QList< dynplaylist_ptr >& playlists )
{
    QList< SourceTreeItem* > items;
    int addOffset = playlists.first()->author()->isLocal() ? 1 : 0;

    int from = parent->children().count() - addOffset;
    parent->beginRowsAdded( from, from + playlists.count() - 1 );
    foreach ( const dynplaylist_ptr& p, playlists )
    {
        DynamicPlaylistItem* plItem = new DynamicPlaylistItem( model(), parent, p, parent->children().count() - addOffset );
//        qDebug() << "Dynamic Playlist added:" << p->title() << p->creator() << p->info();
        p->loadRevision();
        items << plItem;

        if ( p->mode() == Static )
        {
            if ( m_source->isLocal() )
                connect( p.data(), SIGNAL( aboutToBeDeleted( Tomahawk::dynplaylist_ptr ) ),
                        SLOT( onAutoPlaylistDeleted( Tomahawk::dynplaylist_ptr ) ), Qt::QueuedConnection );
            else
                connect( p.data(), SIGNAL( deleted( Tomahawk::dynplaylist_ptr ) ),
                        SLOT( onAutoPlaylistDeleted( Tomahawk::dynplaylist_ptr ) ), Qt::QueuedConnection );
        }
        else
        {
            if ( m_source->isLocal() )
                connect( p.data(), SIGNAL( aboutToBeDeleted( Tomahawk::dynplaylist_ptr ) ),
                        SLOT( onStationDeleted( Tomahawk::dynplaylist_ptr ) ), Qt::QueuedConnection );
            else
                connect( p.data(), SIGNAL( deleted( Tomahawk::dynplaylist_ptr ) ),
                        SLOT( onStationDeleted( Tomahawk::dynplaylist_ptr ) ), Qt::QueuedConnection );
        }
    }
    parent->endRowsAdded();
}


template< typename T >
void
SourceItem::playlistDeletedInternal( SourceTreeItem* parent, const T& p )
{
    Q_ASSERT( parent ); // How can we delete playlists if we have none?
    int curCount = parent->children().count();
    for( int i = 0; i < curCount; i++ )
    {
        PlaylistItem* pl = qobject_cast< PlaylistItem* >( parent->children().at( i ) );
        if ( pl && pl->playlist() == p )
        {
            parent->beginRowsRemoved( i, i );
            parent->removeChild( pl );
            parent->endRowsRemoved();

            delete pl;
            break;
        }
    }

    if ( ( parent == m_playlists || parent == m_stations ) &&
         parent->children().isEmpty() && parent->parent() ) // Don't leave an empty Playlist or Station category
    {
        int idx = parent->parent()->children().indexOf( parent );
        if ( idx < 0 )
            return;

        parent->parent()->beginRowsRemoved( idx, idx );
        parent->parent()->removeChild( parent );
        parent->parent()->endRowsRemoved();

        if ( parent == m_playlists )
            m_playlists = 0;
        else if ( parent == m_stations )
            m_stations = 0;
        delete parent;
    }
}


void
SourceItem::onPlaylistsAdded( const QList< playlist_ptr >& playlists )
{
    if ( playlists.isEmpty() )
        return;

    if ( !m_playlists )
    {
        // add the category too
        int cur = children().count();
        beginRowsAdded( cur, cur );
        m_playlists = new CategoryItem( model(), this, SourcesModel::PlaylistsCategory, source()->isLocal() );
        endRowsAdded();
    }

    QList< SourceTreeItem* > items;
    int addOffset = playlists.first()->author()->isLocal() ? 1 : 0;

    int from = m_playlists->children().count() - addOffset;
    m_playlists->beginRowsAdded( from, from + playlists.count() - 1 );
    foreach ( const playlist_ptr& p, playlists )
    {
        PlaylistItem* plItem = new PlaylistItem( model(), m_playlists, p, m_playlists->children().count() - addOffset );
        p->loadRevision();
        items << plItem;

        if ( m_source->isLocal() )
            connect( p.data(), SIGNAL( aboutToBeDeleted( Tomahawk::playlist_ptr ) ),
                     SLOT( onPlaylistDeleted( Tomahawk::playlist_ptr ) ), Qt::QueuedConnection );
        else
            connect( p.data(), SIGNAL( deleted( Tomahawk::playlist_ptr ) ),
                     SLOT( onPlaylistDeleted( Tomahawk::playlist_ptr ) ), Qt::QueuedConnection );

    }
    m_playlists->endRowsAdded();
}


void
SourceItem::onPlaylistDeleted( const playlist_ptr& playlist )
{
    playlistDeletedInternal( m_playlists, playlist );
}


void
SourceItem::onAutoPlaylistsAdded( const QList< dynplaylist_ptr >& playlists )
{
    if ( playlists.isEmpty() )
        return;

    if ( !m_playlists )
    {
        // add the category too
        int cur = children().count();
        beginRowsAdded( cur, cur );
        m_playlists = new CategoryItem( model(), this, SourcesModel::PlaylistsCategory, source()->isLocal() );
        endRowsAdded();
    }

    playlistsAddedInternal( m_playlists, playlists );
}


void
SourceItem::onAutoPlaylistDeleted( const dynplaylist_ptr& playlist )
{
    if ( !m_playlists )
        qDebug() << "NO playlist category item for a deleting playlist...";

    playlistDeletedInternal( m_playlists, playlist );
}


void
SourceItem::onStationsAdded( const QList< dynplaylist_ptr >& stations )
{
    if ( stations.isEmpty() )
        return;

    if ( !m_stations )
    {
        // add the category too
        int cur = children().count();
        beginRowsAdded( cur, cur );
        m_stations = new CategoryItem( model(), this, SourcesModel::StationsCategory, source()->isLocal() );
        endRowsAdded();
    }

    playlistsAddedInternal( m_stations, stations );
}


void
SourceItem::onStationDeleted( const dynplaylist_ptr& station )
{
    playlistDeletedInternal( m_stations, station );
}


void
SourceItem::requestExpanding()
{
    emit expandRequest( this );
}


ViewPage*
SourceItem::sourceInfoClicked()
{
    if ( m_source.isNull() )
        return 0;

    m_sourceInfoPage = ViewManager::instance()->show( m_source );
    return m_sourceInfoPage;
}


ViewPage*
SourceItem::getSourceInfoPage() const
{
    return m_sourceInfoPage;
}


ViewPage*
SourceItem::collectionClicked()
{
    if ( m_source.isNull() )
        return 0;

    m_collectionPage = ViewManager::instance()->show( m_source->collection() );
    return m_collectionPage;
}


ViewPage*
SourceItem::getCollectionPage() const
{
    return m_collectionPage;;
}


ViewPage*
SourceItem::coolPlaylistsClicked()
{
    if ( !m_source.isNull() )
        return 0;

    if ( !m_coolPlaylistsPage )
        m_coolPlaylistsPage = new SocialPlaylistWidget( ViewManager::instance()->widget() );

    ViewManager::instance()->show( m_coolPlaylistsPage );
    return m_coolPlaylistsPage;
}


ViewPage*
SourceItem::getCoolPlaylistsPage() const
{
    return m_coolPlaylistsPage;
}


ViewPage*
SourceItem::lovedTracksClicked()
{
    if ( !m_lovedTracksPage )
    {
        CustomPlaylistView* view = new CustomPlaylistView( m_source.isNull() ? CustomPlaylistView::TopLovedTracks : CustomPlaylistView::SourceLovedTracks, m_source, ViewManager::instance()->widget() );
        PlaylistLargeItemDelegate* del = new PlaylistLargeItemDelegate( PlaylistLargeItemDelegate::LovedTracks, view, view->proxyModel() );
        connect( del, SIGNAL( updateIndex( QModelIndex ) ), view, SLOT( update( QModelIndex ) ) );
        view->setItemDelegate( del );
        view->setEmptyTip( tr( "Sorry, we could not find any loved tracks!" ) );

        m_lovedTracksPage = view;
    }

    ViewManager::instance()->show( m_lovedTracksPage );
    return m_lovedTracksPage;
}


ViewPage*
SourceItem::getLovedTracksPage() const
{
    return m_lovedTracksPage;
}


ViewPage*
SourceItem::latestAdditionsClicked()
{
    if ( !m_latestAdditionsPage )
    {
        TrackView* cv = new TrackView( ViewManager::instance()->widget() );
        cv->setFrameShape( QFrame::NoFrame );
        cv->setAttribute( Qt::WA_MacShowFocusRect, 0 );

        RecentlyAddedModel* raModel = new RecentlyAddedModel( cv );
        raModel->setStyle( PlayableModel::Large );
        raModel->setTitle( tr( "Latest Additions" ) );

        if ( m_source->isLocal() )
            raModel->setDescription( tr( "Latest additions to your collection" ) );
        else
            raModel->setDescription( tr( "Latest additions to %1's collection" ).arg( m_source->friendlyName() ) );

        PlaylistLargeItemDelegate* del = new PlaylistLargeItemDelegate( PlaylistLargeItemDelegate::LatestAdditions, cv, cv->proxyModel() );
        connect( del, SIGNAL( updateIndex( QModelIndex ) ), cv, SLOT( update( QModelIndex ) ) );
        cv->setItemDelegate( del );

        cv->setPlayableModel( raModel );
        cv->sortByColumn( PlayableModel::Age, Qt::DescendingOrder );
        cv->setEmptyTip( tr( "Sorry, we could not find any recent additions!" ) );

        raModel->setSource( m_source );

        m_latestAdditionsPage = cv;
    }

    ViewManager::instance()->show( m_latestAdditionsPage );
    return m_latestAdditionsPage;
}


ViewPage*
SourceItem::getLatestAdditionsPage() const
{
    return m_latestAdditionsPage;
}


ViewPage*
SourceItem::recentPlaysClicked()
{
    if ( !m_recentPlaysPage )
    {
        PlaylistView* pv = new PlaylistView( ViewManager::instance()->widget() );
        pv->setFrameShape( QFrame::NoFrame );
        pv->setAttribute( Qt::WA_MacShowFocusRect, 0 );

        RecentlyPlayedModel* raModel = new RecentlyPlayedModel( pv );
        raModel->setStyle( PlayableModel::Large );
        raModel->setTitle( tr( "Recently Played Tracks" ) );

        if ( m_source->isLocal() )
            raModel->setDescription( tr( "Your recently played tracks" ) );
        else
            raModel->setDescription( tr( "%1's recently played tracks" ).arg( m_source->friendlyName() ) );

        PlaylistLargeItemDelegate* del = new PlaylistLargeItemDelegate( PlaylistLargeItemDelegate::RecentlyPlayed, pv, pv->proxyModel() );
        connect( del, SIGNAL( updateIndex( QModelIndex ) ), pv, SLOT( update( QModelIndex ) ) );
        pv->setItemDelegate( del );

        pv->setPlaylistModel( raModel );
        pv->setEmptyTip( tr( "Sorry, we could not find any recent plays!" ) );
        raModel->setSource( m_source );

        m_recentPlaysPage = pv;
    }

    ViewManager::instance()->show( m_recentPlaysPage );
    return m_recentPlaysPage;
}


ViewPage*
SourceItem::getRecentPlaysPage() const
{
    return m_recentPlaysPage;
}
