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

#ifdef __linux__
#include <stdlib.h> // system()
#endif

#include "message_handler.h"
#include <DeviceController/interfaces.h>
#include "commandline_parser.h"

#include <unistd.h>
#include <memory>
#include <QSharedMemory>
#include <QRunnable>
#include <QThreadPool>
#include <QDir>
#include <QProcess>
#include <QCommandLineParser>
#include <QStandardPaths>
#include <QMainWindow>
#include <QSettings>

#include "update.h"
#include "git/git_client.h"
#include "ismas/ismas_client.h"
#include "worker_thread.h"
#include "worker.h"
#include "mainwindow.h"
#include "utils.h"
// #include "process/command.h"

#include <QThread>
#include <QtWidgets>
#include <QScopedPointer>
#include <QScreen>

#if defined (Q_OS_UNIX) || defined (Q_OS_LINUX)
#include <unistd.h>
#include <errno.h>
#endif

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

// argv[1]: file to send to dc
int main(int argc, char *argv[]) {
    QByteArray const value = qgetenv("LC_ALL");
    if (value.isEmpty() || value != "C") {
        qputenv("LC_ALL", "C");
    }


    // qputenv("XDG_RUNTIME_DIR", "/var/run/user/0");

    openlog("ATB-UPDATE", LOG_PERROR | LOG_PID | LOG_CONS, LOG_USER);

    QApplication a(argc, argv);
    QApplication::setApplicationName("ATBUpdateTool");
    QApplication::setApplicationVersion(APP_VERSION);

    if (!messageHandlerInstalled()) { // change internal qt-QDebug-handling
        atbInstallMessageHandler(nullptr);
        //atbInstallMessageHandler(atbDebugOutput);
        setDebugLevel(LOG_NOTICE);
    }

    CommandLineParser parser;
    parser.process(a);
    parser.readSettings();

    QString repositoryUrl = parser.repositoryUrl();

    if (repositoryUrl.endsWith('/')) {
        repositoryUrl.chop(1);
    }

    QString gitSSHCommand("");

    if (repositoryUrl.contains("ptu-config.atb-comm.de")) {
        QByteArray const v = qgetenv("GIT_SSH_COMMAND");
        if (v.isEmpty()) {
            QString sshKeyFile("/opt/app/tools/atbupdate/.keys/id_ed25519_ptuConfig");
            if (QFileInfo(sshKeyFile).exists()) {
                if (qgetenv("GIT_SSH_COMMAND").isNull()) {
                    gitSSHCommand = "ssh -i /opt/app/tools/atbupdate/.keys/id_ed25519_ptuConfig";
                    if (!qputenv("GIT_SSH_COMMAND", QByteArray(gitSSHCommand.toStdString().c_str()))) {
                        qCritical() << "ERROR: GIT_SSH_COMMAND not put into env. Exiting...";
                        return -1;
                    }
                }
            } else {
                qCritical() << "ERROR ssh-key-file" << sshKeyFile << "does not exists. Exiting...";
                return -1;
            }
        } else {
            gitSSHCommand = QString(v.toStdString().c_str());
            qCritical() << "WARNING GIT_SSH_COMMAND already set in enviroment:"
                        << gitSSHCommand;
            if (gitSSHCommand != "ssh -i /opt/app/tools/atbupdate/.keys/id_ed25519_ptuConfig") {
                qCritical() << "ERROR" << gitSSHCommand << "wrong. Exiting...";
                return -1;
            }
        }
    }

    QString plugInDir = parser.plugInDir();
    QString plugInName = parser.plugInName();
    QString workingDir = parser.workingDir();
    QString iniFileName = parser.iniFileName();
    bool const dryRun = parser.dryRun();
    bool const noUpdatePsaHardware = parser.noUpdatePsaHardware();
    bool const showYoctoVersion = parser.yoctoVersion();
    bool const showYoctoInstallStatus = parser.yoctoInstallStatus();
    bool const showExtendedVersion = parser.extendedVersion();
    bool const alwaysDownloadConfig = parser.alwaysDownloadConfig();
    bool const alwaysDownloadDC = parser.alwaysDownloadDC();

    QString const rtPath = QCoreApplication::applicationDirPath();

    int const machineNr = Utils::read1stLineOfFile("/mnt/system_data/machine_nr");
    int const customerNr = Utils::read1stLineOfFile("/mnt/system_data/cust_nr");
    int const zoneNr = Utils::read1stLineOfFile("/mnt/system_data/zone_nr");
    QString const branchName = (zoneNr != 0)
            ? QString("zg1/zone%1").arg(zoneNr) : "master";

    qInfo() << "pwd ......................" << rtPath;
    qInfo() << "repositoryUrl ............" << repositoryUrl;
    if (!gitSSHCommand.isEmpty()) {
        qInfo() << "GIT_SSH_COMMAND .........." << gitSSHCommand;
    }
    qInfo() << "plugInDir ................" << plugInDir;
    qInfo() << "plugInName ..............." << plugInName;
    qInfo() << "workingDir ..............." << workingDir;
    qInfo() << "dryRun ..................." << dryRun;
    qInfo() << "noUpdatePsaHardware ......" << noUpdatePsaHardware;
    qInfo() << "alwaysDownloadConfig ....." << alwaysDownloadConfig;
    qInfo() << "alwaysDownloadDC ........." << alwaysDownloadDC;
    qInfo() << "showYoctoVersion ........." << showYoctoVersion;
    qInfo() << "showYoctoInstallStatus ..." << showYoctoInstallStatus;
    qInfo() << "showExtendedVersion ......" << showExtendedVersion;
    qInfo() << "iniFileName .............." << iniFileName;
    qInfo() << "extended-version ........." << APP_EXTENDED_VERSION;
    qInfo() << "machineNr ................" << machineNr;
    qInfo() << "customerNr ..............." << customerNr;
    qInfo() << "zoneNr ..................." << zoneNr;

    if (showExtendedVersion) {
        printf(APP_EXTENDED_VERSION"\n");
        return 0;
    }
    if (showYoctoVersion) {
        printf("%s\n", Worker::getATBUpdateToolYoctoVersion().toStdString().c_str());
        return 0;
    }
    if (showYoctoInstallStatus) {
        printf("%s\n", Worker::getATBUpdateToolYoctoInstallationStatus().toStdString().c_str());
        return 0;
    }


    QThread::currentThread()->setObjectName("main thread");
    qInfo() << "Main thread" << QThread::currentThreadId();


    if (!QDir(plugInDir).exists()) {
        qCritical() << plugInDir
                    << "does not exists, but has to contain dc-library";
        if (noUpdatePsaHardware == false) {
            exit(-1);
        }
    }

    // before loading the library, delete all possible shared memory segments
#if defined Q_OS_LINUX || defined Q_OS_UNIX
    // system("rm -rf /tmp/qipc*");
#else
#error "Only tested under UNIX/LINUX"
#endif

    Worker worker(customerNr,
                  machineNr,
                  zoneNr,
                  repositoryUrl,
                  branchName,
                  plugInDir,
                  plugInName,
                  workingDir,
                  noUpdatePsaHardware,
                  alwaysDownloadConfig,
                  alwaysDownloadDC,
                  dryRun);

    MainWindow mw(&worker);
    worker.setMainWindow(&mw);

    mw.setWindowFlags(Qt::Window | Qt::FramelessWindowHint);
    mw.showFullScreen();

    // test
    //worker.jsUpdate();
    worker.workList().exec();
    // worker.summary();

    return a.exec();
}