#include "autotest_cod.h"

const QList<QPair<QString, bool> > &AutoTest_COD::getCollectedTestResults() const {
    return m_subtestResults;
}

void AutoTest_COD::recordResult(const QString &name, bool passed) {
    m_subtestResults.append({ name, passed });
}

void AutoTest_COD::setFastFileDirectory(const QString aFastFileDir) {
    mFastFileDirectory = aFastFileDir;
}

QString AutoTest_COD::getFastFileDirectory() {
    return mFastFileDirectory;
}

void AutoTest_COD::setZoneFileDirectory(const QString aZoneFileDir) {
    mZoneFileDirectory = aZoneFileDir;
}

QString AutoTest_COD::getZoneFileDirectory() {
    return mZoneFileDirectory;
}

void AutoTest_COD::createDirectory(const QString aDir) {
    QDir newDir(".");
    newDir.mkpath(aDir);
}

QStringList AutoTest_COD::findZoneFiles(const QString &aBaseDir, int aMaxIter) {
    QList<QPair<qint64, QString>> fileList;

    QDirIterator it(aBaseDir, QStringList() << "*.zone", QDir::Files, QDirIterator::Subdirectories);
    int i = 0;
    while (it.hasNext() && i < aMaxIter) {
        QString path = it.next();
        QFileInfo fi(path);
        fileList.append(qMakePair(fi.size(), path));
        ++i;
    }

    std::sort(fileList.begin(), fileList.end(),
              [](const QPair<qint64, QString> &a, const QPair<qint64, QString> &b) {
                  return a.first < b.first; // sort by size
              });

    QStringList sorted;
    for (const auto &pair : fileList)
        sorted << pair.second;

    return sorted;
}

QStringList AutoTest_COD::findFastFiles(const QString &aBaseDir, int aMaxIter) {
    QList<QPair<qint64, QString>> fileList;

    QDirIterator it(aBaseDir, QStringList() << "*.ff", QDir::Files, QDirIterator::Subdirectories);
    int i = 0;
    while (it.hasNext() && i < aMaxIter) {
        QString path = it.next();
        QFileInfo fi(path);
        fileList.append(qMakePair(fi.size(), path));
        ++i;
    }

    std::sort(fileList.begin(), fileList.end(),
              [](const QPair<qint64, QString> &a, const QPair<qint64, QString> &b) {
                  return a.first < b.first; // sort by size
              });

    QStringList sorted;
    for (const auto &pair : fileList)
        sorted << pair.second;

    return sorted;
}

void AutoTest_COD::testDecompression_data() {
    QTest::addColumn<QString>("fastFilePath");

    QStringList ffFiles = findFastFiles(getFastFileDirectory());
    for (const QString &filePath : ffFiles) {
        QString fileName = QFileInfo(filePath).fileName();
        QTest::newRow(qPrintable(fileName)) << filePath;
    }
}

void AutoTest_COD::testCompression_data() {
    QTest::addColumn<QString>("zoneFilePath");

    QStringList zoneFiles = findZoneFiles(getZoneFileDirectory());
    for (const QString &filePath : zoneFiles) {
        QString fileName = QFileInfo(filePath).fileName();
        QTest::newRow(qPrintable(fileName)) << filePath;
        break;
    }
}

void AutoTest_COD::testFactory_data() {
    QTest::addColumn<QString>("fastFilePath");

    QStringList ffFiles = findFastFiles(getFastFileDirectory());
    for (const QString &filePath : ffFiles) {
        QString fileName = QFileInfo(filePath).fileName();
        QTest::newRow(qPrintable(fileName)) << filePath;
    }
}
