/*  KStars UI tests
    Copyright (C) 2020
    Eric Dejouhanet <eric.dejouhanet@gmail.com>

    SPDX-License-Identifier: GPL-2.0-or-later
 */

#include "test_ekos_filterwheel.h"

#if defined(HAVE_INDI)

#include "kstars_ui_tests.h"
#include "test_ekos.h"
#include "test_ekos_helper.h"
#include "fitsviewer/fitsdata.h"
#include "ekos/capture/capture.h"

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

void TestEkosFilterWheel::initTestCase()
{
    // use the helper to start the profile
    TestEkosHelper *helper = new TestEkosHelper();
    helper->startEkosProfile();
    // Create a test profile
    helper->init();
    // prepare the optical trains
    helper->prepareOpticalTrains();


    // HACK: Reset clock to initial conditions
    //KHACK_RESET_EKOS_TIME();
}

void TestEkosFilterWheel::cleanupTestCase()
{
    KTRY_EKOS_STOP_SIMULATORS();
    KTRY_CLOSE_EKOS();
    KVERIFY_EKOS_IS_HIDDEN();
}

void TestEkosFilterWheel::init()
{
    // Get manager instance
    Ekos::Manager * const ekos = Ekos::Manager::Instance();

    // Wait for Capture to come up, switch to capture tab
    QTRY_VERIFY_WITH_TIMEOUT(ekos->captureModule() != nullptr, 5000);
    KTRY_EKOS_GADGET(QTabWidget, toolsWidget);
    toolsWidget->setCurrentWidget(ekos->captureModule());
    QTRY_COMPARE_WITH_TIMEOUT(toolsWidget->currentWidget(), ekos->captureModule(), 1000);
}

void TestEkosFilterWheel::cleanup()
{
    Ekos::Manager::Instance()->captureModule()->clearSequenceQueue();
    KTRY_CAPTURE_GADGET(QTableWidget, queueTable);
    QTRY_VERIFY_WITH_TIMEOUT(queueTable->rowCount() == 0, 2000);
}

QFileInfoList TestEkosFilterWheel::searchFITS(QDir const &dir) const
{
    QFileInfoList list = dir.entryInfoList(QDir::Files);
    for (const auto &d : dir.entryList(QDir::NoDotAndDotDot | QDir::Dirs))
        list.append(searchFITS(QDir(dir.path() + QDir::separator() + d)));
    return list;
}

void TestEkosFilterWheel::testFilterWheelSync()
{
    QTemporaryDir destination(KTest::tempDirPattern(QStringLiteral("filterwheel")));
    QVERIFY(destination.isValid());
    QVERIFY(destination.autoRemove());
    KTRY_CAPTURE_ADD_LIGHT(0.5, 1, 0, "Green", destination.path());

    // Start capturing and wait for procedure to end (visual icon changing)
    KTRY_CAPTURE_GADGET(QPushButton, startB);
    QCOMPARE(startB->icon().name(), QString("media-playback-start"));
    KTRY_CAPTURE_CLICK(startB);
    QTRY_COMPARE_WITH_TIMEOUT(startB->icon().name(), QString("media-playback-start"), 30000);

    QFileInfoList list = searchFITS(QDir(destination.path()));
    QVERIFY(!list.isEmpty());

    QString filepath = list.first().absoluteFilePath();
    QScopedPointer<FITSData> data(new FITSData());
    data->loadFromFile(filepath).waitForFinished();

    QVariant filter;
    if (data->getRecordValue("FILTER", filter))
    {
        QVERIFY(filter == "Green");
    }
    else
    {
        const QString fullPath = QFileInfo(filepath).absoluteFilePath();
        QVERIFY2(fullPath.contains("Green"), "FILTER header missing and filename does not include filter name");
    }
}

QTEST_KSTARS_MAIN(TestEkosFilterWheel)

#endif // HAVE_INDI
