#include <QCoreApplication>
#include <QApplication>
#include <QDebug>
#include <QTimer>
#include <QFileInfo>

#include "message_handler.h"
#include "plugins/interfaces.h"

#include <unistd.h>
#include <thread>
#include <memory>
#include <QSharedMemory>
#include <QRunnable>
#include <QThreadPool>
#include <QDir>

#include "update.h"
#include "worker_thread.h"
#include "worker.h"

#include <thread>

#ifdef PTU5
#define SERIAL_PORT "ttymxc2"
#else
#define SERIAL_PORT "ttyUSB0"
#endif

static void doWork(QString update_ctrl_file, QString workingDir) {
    std::this_thread::sleep_for(std::chrono::milliseconds(2000));
    Update update(update_ctrl_file, workingDir);
    update.doUpdate();
    std::this_thread::sleep_for(std::chrono::milliseconds(2000));
    QCoreApplication::quit();
}

// argv[1]: file to send to dc
int main(int argc, char *argv[]) {
    QApplication a(argc, argv);

    if (!messageHandlerInstalled()) { // change internal qt-QDebug-handling
        atbInstallMessageHandler(atbDebugOutput);
        setDebugLevel(QtMsgType::QtDebugMsg);
        //setDebugLevel(QtMsgType::QtDebugMsg);
    }

    QByteArray const value = qgetenv("XDG_RUNTIME_DIR");
    if (value.size() == 0) {
        qputenv("XDG_RUNTIME_DIR", "/run/user/0");
    }

    // QString const update_ctrl_file = "/opt/app/tools/atbupdate/update_log.csv";
    // QString const workingDir = (argc == 2) ? argv[1] : ".";

    // std::thread t(doWork, update_ctrl_file, workingDir);

    int ret = a.exec();
    // t.join();

    return ret;
}
