#include "randomplayer.h"
#include <QDirIterator>

void RandomPlayer::start()
{
    this->setMedia(QUrl::fromLocalFile(fileList.takeFirst()));
    this->play();
    this->_readyToPlay = true;
}

void RandomPlayer::quitPlayMode()
{
    this->_readyToPlay = false;
    this->stop();
}

bool RandomPlayer::isPlayMode(){
    return this->_readyToPlay;
}

void RandomPlayer::initList(bool includePiano, bool includeChants, bool includeMelodies)
{
    QString basedir = iPlayer::getMusicRoot();

    QStringList listFilter;
    listFilter << "*.mp3";

    if(!includePiano && !includeChants && !includeMelodies) {
        includePiano = true;
    }

    if (includePiano) {
        QDirIterator dirIterator(basedir+"/cantiques/", listFilter ,QDir::Files | QDir::NoSymLinks, QDirIterator::Subdirectories);

        while(dirIterator.hasNext())
        {
            fileList << dirIterator.next();
        }
    }

    if (includeChants) {
        QDirIterator dirIterator(basedir+"/chants/", listFilter ,QDir::Files | QDir::NoSymLinks, QDirIterator::Subdirectories);

        while(dirIterator.hasNext())
        {
            fileList << dirIterator.next();
        }
    }

    if (includeMelodies) {
        QDirIterator dirIterator(basedir+"/melodies/", listFilter ,QDir::Files | QDir::NoSymLinks, QDirIterator::Subdirectories);

        while(dirIterator.hasNext())
        {
            fileList << dirIterator.next();
        }
    }

    std::random_shuffle(fileList.begin(), fileList.end());

}

