/*
Copyright (c) 2010 by Drake Justice <djustice.kde@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.

*/

#include "authorthread.h"

#include <QFileInfo>

#include <settings.h>
#include <QDir>

AuthorThread::AuthorThread(const QStringList& fileList, const QStringList& chapterStrings, const QList<bool>& pauseList, const QString& tmpDir)
{
    m_fileList = fileList;
    m_tmpDir = tmpDir;
    m_chapterStrings = chapterStrings;
    m_pauseList = pauseList;
    m_process = new QProcess;
    m_timer = new QTimer;
    m_currentFile = 0;
}

void AuthorThread::run()
{
    QStringList chpS;

    foreach (QString s, m_chapterStrings) {
      qDebug() << "s: " << s;
        QString t = "0";
        int i = 1;
        while (i < 100) {
            if ((s.toInt() * i) >= 60) {
                int h = (s.toInt() * i) / 60;
                int m = (s.toInt() * i) - (h * 60);
                t.append("," + QString::number(h) + ":" + QString::number(m) + ":00");
            } else {
                t.append("," + QString::number(s.toInt() * i) + ":00");
            }
            i++;
        }
        chpS += t;
    }

    int i = 0;
    bool c = false;
    while (i < m_pauseList.count()) {
        if (m_pauseList.at(i)) {
            c = true;
        }
        i++;
    }

    QString filexml = "<dvdauthor><vmgm /><titleset><titles>";

    if (!c) { filexml += "<pgc>"; }

    i = 0;
    foreach (QString str, m_fileList) {
        if (m_pauseList.at(i)) { filexml += "<pgc>"; }

        filexml += "<vob file=\"" + m_tmpDir + "/" + QFileInfo(str).baseName() + ".mpg\" chapters=\"" + chpS.at(i) + "\"/>";

        if (m_pauseList.at(i)) { filexml += "</pgc>"; }

        i++;
    }

    if (!c) { filexml += "</pgc>"; }

    filexml += "</titles></titleset></dvdauthor>";

    QFile f(m_tmpDir + "/dvd.xml");
    if (f.open(QFile::WriteOnly)) {
        QTextStream s(&f);
        s << filexml;
        s.flush();
    }

    f.close();

    m_process->setProcessChannelMode(QProcess::MergedChannels);

    if (QDir(m_tmpDir + "/dvd").exists()) {
        m_process->start("rm -r \"" + m_tmpDir + "/dvd\"");
        m_process->waitForFinished();
        QDir().mkdir(m_tmpDir + "/dvd");
    }

    m_process->start("dvdauthor -o \"" + m_tmpDir + "/dvd\" -x \"" + m_tmpDir + "/dvd.xml\"");

    qDebug() << "titles: " << filexml;

    connect(m_process, SIGNAL(finished(int)), this, SLOT(dvdFinished()));
    connect(m_timer, SIGNAL(timeout()), this, SLOT(updateProgress()));
    m_timer->start(800);
}

void AuthorThread::updateProgress()
{
    if (m_currentFile >= m_fileList.count()) {
        return;
    }

    QString out = m_process->readAll();
    qDebug() << out;
    foreach (QString str, out.split("\n")) {
        if (str.contains("AT: VOBU")) {
            emit progressMax(QFile(m_tmpDir + "/" + m_fileList.at(m_currentFile - 1)).size() / 1024 / 1024);
            QString mb = str.split(" ", QString::SkipEmptyParts).at(4);
            emit progress(mb.left(mb.length() - 2).toInt());
        } else if (str.contains("Processing")) {
            m_currentFile++;
            emit progressFile(m_currentFile);
        }
    }
}

void AuthorThread::dvdFinished(/* int */) // catch this error_code int
{
    m_timer->stop();
    emit finished();
}
