#include "QHandbrake.h"
#include <QFile>
#include <QProcess>
#include <QDebug>
#include <QThread>
#include <QFileInfo>

QHandbrake::QHandbrake(QObject *parent)
    : QObject{parent} {
}

uint8_t QHandbrake::setPath(QString pathIn) {
    if (QFile::exists(pathIn)) {
        exePath = pathIn;
        return 1;
    }
    return -1;
}

uint8_t QHandbrake::startEncode(const QString path,const QString title) {
    QFileInfo inFile = QFileInfo(path);
    qInfo() << inFile.absolutePath();
    qInfo() << inFile.baseName();

    //HandBrakeCLI -i "../%TITLE%/%TITLE% - 4k.mkv" -o "../%TITLE%/temp/handbrake.mkv" -f av_mkv -m -e x265_10bit --encoder-preset slower -q 20 --encoder-profile auto --all-audio -E copy --audio-copy-mask aac,eac3,dts,ac3,truehd,dtshd,mp3 --crop-mode auto --auto-anamorphic --all-subtitles > "../%TITLE%/temp/log.txt"
    const QStringList args4kHD = {"-i", path, "-o", "test.mkv", "-f", "av_mkv", "-m", "-e", "x265_10bit", "--encoder-preset", "slower", "-q", "20", "--encoder-profile", "auto", "--all-audio", "-E", "copy", "--audio-copy-mask", "aac,eac3,dts,ac3,truehd,dtshd,mp3", "--crop-mode", "auto", "--auto-anamorphic", "--all-subtitles"} ;

    QStringList arguments;
    arguments << "-i" <<  "Cosmos.mkv" << "-o" << "test.mkv" << "-f" << "av_mkv" << "-m" << "-e" << "x265_10bit" << "--encoder-preset" << "slower" << "-q" << "20" << "--encoder-profile" << "auto" << "--all-audio" << "-E" << "copy" << "--audio-copy-mask" << "aac,eac3,dts,ac3,truehd,dtshd,mp3" << "--crop-mode" << "auto" << "--auto-anamorphic" << "--all-subtitles";

    HBProcess = new QProcess(parent());
    HBProcess->start(exePath, arguments);

    emit statusChanged("Encode started.");

    QObject::connect(HBProcess, &QProcess::readyReadStandardOutput, this, &QHandbrake::printStandardStatus);
    QObject::connect(HBProcess, &QProcess::finished, HBProcess, &QHandbrake::deleteLater);

    return true;
}

uint8_t QHandbrake::stopEncode() {
    QObject::disconnect(HBProcess, &QProcess::readyReadStandardOutput, this, &QHandbrake::printStandardStatus);
    HBProcess->close();
    emit statusChanged("Encode stopped.");
    return true;
}

uint8_t QHandbrake::setCoreUsage(uint8_t cores) {
    tasksetProcess = new QProcess(parent());
    QString argCores = QString::number(QThread::idealThreadCount() - cores) + "-" +  QString::number(QThread::idealThreadCount() - 1); //Set affinity to lowest numbered cores.
    QStringList arguments = {"-a", "-pc", argCores, QString::number(HBProcess->processId())};

    qInfo() << arguments;
    tasksetProcess->start("/usr/bin/taskset", arguments);
    QObject::connect(tasksetProcess, &QProcess::finished, tasksetProcess, &QHandbrake::deleteLater);
    QObject::connect(tasksetProcess, &QProcess::readyReadStandardOutput, this, &QHandbrake::printOutput);
    return true;
}

void QHandbrake::printStandardStatus() {
    emit statusChanged(HBProcess->readAllStandardOutput().trimmed());
}

void QHandbrake::printOutput() {
    qInfo() << tasksetProcess->readAllStandardOutput().trimmed();
}
