#include "phononwebplayer.h"
#include <QSlider>
#include <QHBoxLayout>
#include <QTimer>

PhononWebPlayer::PhononWebPlayer(QNetworkReply* reply, QString mode) :
    mediaPlayer(0),
    replyStream(0),
    loaded(false),
    buffering(false),
    mediaBuffer(0)
{
    if(reply == NULL)
        return;

    if(mode.isEmpty())
        mode = "QNetworkReply";

    qDebug() << QString("Mod : %1").arg(mode);

    replyStream = reply;

    mediaPlayer = new Phonon::VideoPlayer(Phonon::VideoCategory);

    mod = mode;

    mediaBuffer = new QBuffer();
    mediaBuffer->open(QBuffer::ReadWrite);

    connect(replyStream,SIGNAL(downloadProgress(qint64,qint64)),SLOT(downloading(qint64,qint64)));
    connect(replyStream,SIGNAL(finished()),SLOT(downloadFinished()));

    //GUI
    QVBoxLayout *vLayout = new QVBoxLayout();
    vLayout->addWidget(mediaPlayer);
    setLayout(vLayout);
}

void PhononWebPlayer::load(){
    qDebug() << "Loading media";

    if(!mod.compare("QBuffer")){
        mediaPlayer->load(Phonon::MediaSource(mediaBuffer));
    }else if(!mod.compare("QNetworkReply")){
        mediaPlayer->load(Phonon::MediaSource(replyStream));
    }else{
        deleteLater();
    }

    connect(mediaPlayer->mediaObject(),SIGNAL(stateChanged(Phonon::State,Phonon::State)),
                                            SLOT(statusState(Phonon::State,Phonon::State)));

    switch(mediaPlayer->mediaObject()->currentSource().type()){
        case Phonon::MediaSource::Invalid:
            qDebug() << "MediaSource::Invalid ";
            break;
        case Phonon::MediaSource::LocalFile:
            qDebug() << "MediaSource::LocalFile ";
            break;
        case Phonon::MediaSource::Url:
            qDebug() << "MediaSource::Url ";
            break;
        case Phonon::MediaSource::Disc:
            qDebug() << "MediaSource::Disc ";
            break;
        case Phonon::MediaSource::Stream:
            qDebug() << "MediaSource::Stream ";
            break;
        case Phonon::MediaSource::Empty:
            qDebug() << "MediaSource::Empty ";
            break;
    }

}

void PhononWebPlayer::play(){
    qDebug() << "Playing media";
    mediaPlayer->play();
}

void PhononWebPlayer::downloadFinished(){
    qDebug() << "DL Finished";

  //THIS WORK BUT NEED TO WAIT DOWNLOAD FINISHED :| not convenient
  //IF load() and play() is called inside downloading SLOT THEN BLACKSCREEN ...
    //load();
    //play();
}

void PhononWebPlayer::downloading(qint64 cur, qint64 tot){
    //qDebug() << QString("Buffer size is %1").arg(mediaBuffer->buffer().size());
    //qDebug() << QString("QBuffer size is %1").arg(mediaBuffer->size());
    //qDebug() << QString("Download size is %1").arg(cur);

    if(!mod.compare("QBuffer")){

        mediaBuffer->write(replyStream->readAll());

        if(!loaded && mediaBuffer->size() > 0){
            loaded = true;
            //if load is called here then, doesnt work
            //if load is called above  (then first comment the load() here)
                    //inside DL-Finished then it works (if media is big it's not convenient )
            load();
        }

    }else if(!mod.compare("QNetworkReply")){

        if(!loaded && replyStream->bytesAvailable() > 0){
            loaded = true;
            //if load is called here then, doesnt work
            //if load is called above (then first comment the load() here)
                    //inside DL-Finished then it works (if media is big it's not convenient )
            load();
        }
    }

}

void PhononWebPlayer::statusState(Phonon::State newSt, Phonon::State oldSt){

    switch(newSt){
        case Phonon::LoadingState:
            //NEVER PASS THERE WHY?
            qDebug() << "LoadingState";
            buffering = true;
            break;
        case Phonon::StoppedState:
            qDebug() << "StoppedState";
            if(buffering){
                qDebug() << "Ready to read";
                //Play is launch once phonon entered LoadingState then Stopped state (means ready to read)
                //as said on qt Phonon doc
                //Otherwise trigger play when downloadFinished is emited play well the media
                //But In this case need to stream as soon as possible (no waiting for 80MB file finished download)
                buffering = false;
                QTimer::singleShot(1000,this,SLOT(play()));
            }
            break;
        case Phonon::PlayingState:
            qDebug() << "PlayingState";
            break;
        case Phonon::BufferingState:
            qDebug() << "BufferingState";
            break;
        case Phonon::PausedState:
            qDebug() << "PausedState";
            break;
        case Phonon::ErrorState:
            qDebug() << "ErrorState";
            qDebug() << mediaPlayer->mediaObject()->errorString();

            switch(mediaPlayer->mediaObject()->errorType()){
                case Phonon::NoError:
                    qDebug() << "NoError";
                    break;
                case Phonon::NormalError:
                    qDebug() << "NormalError";
                    break;
                case Phonon::FatalError:
                    qDebug() << "FatalError";
                    break;
            }

            break;
    }
}
