/****************************************************************************************
*  YAROCK                                                                               *
*  Copyright (c) 2010-2016 Sebastien amardeilh <sebastien.amardeilh+yarock@gmail.com>   *
*                                                                                       *
*  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, see <http://www.gnu.org/licenses/>.                           *
*****************************************************************************************/

#include "models/stream/service_favorite.h"
#include "core/database/database.h"
#include "threadmanager.h"

#include "utilities.h"
#include "debug.h"
#include "views/item_button.h"

#include <QSqlQuery>
#include <QSqlError>
/*
********************************************************************************
*                                                                              *
*    Class FavoriteStreams                                                     *
*                                                                              *
********************************************************************************
*/
FavoriteStreams::FavoriteStreams() : Service(tr("Favorite streams"), SERVICE::LOCAL)
{
    /* root link */
    m_root_link = MEDIA::LinkPtr(new MEDIA::Link());
    m_root_link->name  = QString(tr("All"));
    m_root_link->state = int (SERVICE::NO_DATA);
   
    m_active_link = m_root_link;
    
    /* restore state  */
    set_state(SERVICE::NO_DATA);
    
    connect(ThreadManager::instance(), SIGNAL(modelPopulationFinished(E_MODEL_TYPE)), this, SLOT(slot_dbBuilder_stateChange()));
}

FavoriteStreams::~FavoriteStreams() 
{
}



void FavoriteStreams::slot_dbBuilder_stateChange()
{
    reload();
}

void FavoriteStreams::updateItem(MEDIA::TrackPtr stream)
{
    Debug::debug() << "    [FavoriteStreams] updateItem";
  
    if(stream->isFavorite)
    {
      /* remove it from favorite */ 
      foreach(MEDIA::TrackPtr _stream, m_streams) {
        if(_stream == stream) {
          m_streams.removeOne(stream);
          stream->isFavorite = false;
          break;
        }
      }
    }
    else
    {
      /* add it to favorite */  
      m_streams << stream;
      stream->isFavorite = true;
    }
}


QList<MEDIA::LinkPtr> FavoriteStreams::links()
{
    Debug::debug() << "    [FavoriteStreams] links";
  
    QList<MEDIA::LinkPtr> links;

    foreach(MEDIA::MediaPtr media, m_active_link->children()) {
        if(media->type() == TYPE_LINK)
          links << MEDIA::LinkPtr::staticCast(media);
    }    
    
    MEDIA::LinkPtr p_link = m_active_link->parent();
    while(p_link)
    {
        int i = 0;

        foreach(MEDIA::MediaPtr media, p_link->children()) {
          if(media->type() == TYPE_LINK)
            links.insert(i++, MEDIA::LinkPtr::staticCast(media));
        }    

        p_link = p_link->parent();
    }
    
    return links;
}


QList<MEDIA::TrackPtr> FavoriteStreams::streams()
{
    QList<MEDIA::TrackPtr> streams;
  
    foreach(MEDIA::MediaPtr media, m_active_link->children())
    {
      if(media->type() == TYPE_STREAM) 
      {
        MEDIA::TrackPtr stream = MEDIA::TrackPtr::staticCast(media);
        streams << stream;
      }
      /* !! add auto recursion to see all favorite stream when opening view */
      else if(media->type() == TYPE_LINK) 
      {
         foreach(MEDIA::MediaPtr child, media->children()) {
            if(child->type() == TYPE_STREAM) {
              MEDIA::TrackPtr stream = MEDIA::TrackPtr::staticCast(child);
              streams << stream;
            }
         }
      }
    }
      
    return streams;
}

void FavoriteStreams::reload() 
{
    m_root_link.reset();
    delete m_root_link.data();
    
    /* root link */
    m_root_link = MEDIA::LinkPtr(new MEDIA::Link());
    m_root_link->name  = QString(tr("All"));
    m_root_link->state = int (SERVICE::NO_DATA);
   
    m_active_link = m_root_link;


    /* register update */    
    m_active_link->state = int(SERVICE::NO_DATA);
    set_state(SERVICE::NO_DATA);    
    
    emit stateChanged();
}
    
void FavoriteStreams::load()
{
    Debug::debug() << "    [FavoriteStreams] load";

    m_streams.clear();


    /*-----------------------------------------------------------*/
    /* Get connection                                            */
    /* ----------------------------------------------------------*/
    if (!Database::instance()->open()) {
        Debug::warning() << "  [FavoriteStreams] db connect failed";
        /* emit change */    
        m_root_link->state = int (SERVICE::ERROR);
        set_state(SERVICE::ERROR);
        emit stateChanged();
        return;
    }


    MEDIA::LinkPtr currentLink = MEDIA::LinkPtr(0);

    QSqlQuery query("SELECT id,url,name,genre,provider FROM favorite_stream ORDER BY genre COLLATE NOCASE ASC",*Database::instance()->db());

    while ( query.next() )
    {
        /* new genre link */
        if( !currentLink || currentLink->name != query.value(3).toString() )
        {
            Debug::debug() << "new Link";
            currentLink         = MEDIA::LinkPtr::staticCast( m_root_link->addChildren(TYPE_LINK) );
            currentLink->setType(TYPE_LINK);
            currentLink->name   = query.value(3).toString();
            currentLink->state  = int(SERVICE::DATA_OK);
            currentLink->setParent(m_root_link);  
        }


        /* new stream */
        MEDIA::TrackPtr stream = MEDIA::TrackPtr(new MEDIA::Track());
        stream->setType(TYPE_STREAM);
        stream->id               = query.value(0).toInt();
        stream->url              = query.value(1).toString();
        stream->extra["station"] = query.value(2).toString();
        stream->genre            = query.value(3).toString();
        stream->isFavorite       = false;
        stream->isPlaying        = false;
        stream->isBroken         = false;
        stream->isPlayed         = false;
        stream->isStopAfter      = false;
        
        currentLink->insertChildren( stream );
        stream->setParent( currentLink );
        
        m_streams << stream;
    }
    
    /* emit change */    
    m_root_link->state = int (SERVICE::DATA_OK);
    set_state(SERVICE::DATA_OK);
    emit stateChanged();
}

bool FavoriteStreams::findStream(MEDIA::TrackPtr stream)
{
    /* loop over streams list */
    foreach(MEDIA::TrackPtr _stream, m_streams) 
    {
        if( _stream->title == stream->title &&
            _stream->url == stream->url ) 
        {
          return true;
          break;
        }
      }
      
    return false;
}


void FavoriteStreams::saveToDatabase()
{
    Debug::debug() << "    [FavoriteStreams] saveToDatabase";
 
    /*-----------------------------------------------------------*/
    /* Get connection                                            */
    /* ----------------------------------------------------------*/
    if (!Database::instance()->open()) {
        Debug::warning() << "  [FavoriteStreams] db connect failed";
        return;
    }
    
    QSqlQuery query("DELETE FROM `favorite_stream`;", *Database::instance()->db() );
    //query.exec();

    foreach(MEDIA::TrackPtr stream, m_streams) 
    {
      query.prepare("INSERT INTO `favorite_stream`(`url`,`name`,`genre`,`website`,`provider`) VALUES(?,?,?,?,?);");

      query.addBindValue( stream->url );
      query.addBindValue( stream->extra["station"].toString() );
      query.addBindValue( stream->genre );
      query.addBindValue( stream->extra["website"].toString() );
      query.addBindValue( stream->extra["provider"].toString() );
      
      Debug::debug() << "exec " << query.exec();
    }
}


void FavoriteStreams::slot_activate_link(MEDIA::LinkPtr link)
{
    Debug::debug() << "    [FavoriteStreams] slot_activate_link";
  
    MEDIA::LinkPtr activated_link;
    
    activated_link = link;
    
    if(!link) {
      ButtonItem* button = qobject_cast<ButtonItem*>(sender());
    
      if (!button) return;  
  
      activated_link = qvariant_cast<MEDIA::LinkPtr>( button->data() );
    }

    if(activated_link) {
      m_active_link = activated_link;
      set_state(SERVICE::State(m_active_link->state));
      emit stateChanged();
    }
}

