#include "update.h"
#include "process/command.h"

#include <QCoreApplication>
#include <QFile>
#include <QTemporaryFile>
#include <QDebug>
#include <QTextStream>
#include <QRegularExpression>
#include <QRegExp>

#if defined (Q_OS_UNIX) || defined (Q_OS_LINUX)
#include "unistd.h"
#endif

#include <DeviceController/interfaces.h>

#include <QSharedMemory>
#include <QScopedPointer>
#include <QDir>
#include <QThread>
#include <QDateTime>
#include <QPluginLoader>
#include <QMap>
#include <QStringList>
#include <QString>

#define UPDATE_OPKG                 (1)
#define UPDATE_DC                   (0)

static const QMap<QString, int> baudrateMap = {
  {"1200"   ,   0}, {"9600"   ,   1}, {"19200"  ,   2}, {"38400"  ,   3},
  {"57600"  ,   4}, {"115200" ,   5}
};

QPluginLoader Update::pluginLoader;

hwinf *Update::loadDCPlugin(QDir const &plugInDir, QString const &fname) {
    hwinf *hw = nullptr;
    if (plugInDir.exists()) {
        QString pluginLibName(fname);
        pluginLibName = plugInDir.absoluteFilePath(pluginLibName);
        QFileInfo info(pluginLibName);
        if (info.exists()) {
            pluginLibName = plugInDir.absoluteFilePath(pluginLibName);
            pluginLoader.setFileName(pluginLibName);
            // static QPluginLoader pluginLoader(pluginLibName);
            if (!pluginLoader.load()) {
                qCritical() << "in directory" << plugInDir.absolutePath();
                qCritical() << "cannot load plugin" << pluginLoader.fileName();
                qCritical() << pluginLoader.errorString();
                return nullptr;
            }

            qCritical() << "loadDCPlugin() plugin directory:" << plugInDir.absolutePath();
            qCritical() << "loadDCPlugin() plugin file name:" << pluginLoader.fileName();

            if (!pluginLoader.isLoaded()) {
                qCritical() << pluginLoader.errorString();
                return nullptr;
            }
            QObject *plugin = pluginLoader.instance();
            if (!plugin) {
                qCritical() << "cannot start instance";
                return nullptr;
            }
            if (! (hw = qobject_cast<hwinf *>(plugin))) {
                qCritical() << "cannot cast plugin" << plugin << "to hwinf";
                return nullptr;
            }
        } else {
            qCritical() << pluginLibName << "does not exist";
            return nullptr;
        }
    } else {
        qCritical() << "plugins directory" << plugInDir.absolutePath()
                    << "does not exist";
        return nullptr;
    }
    return hw;
}

bool Update::unloadDCPlugin() {
    if (pluginLoader.unload()) {
        qCritical() << "unloaded plugin" << pluginLoader.fileName();
        // Note: will re-instantiate the library !
        // QObject *rootObject = pluginLoader.instance();
        // if (rootObject) {
        //    qCritical() << "reloaded plugin: root object again available";
        //    return false;
        // }
        // qCritical()unloaded plugin: root object gone";
        return true;
    }
    return false;
}

QString Update::dcVersion(QString const &dcBinFile) {
    QProcess p;
    QStringList params;

    params << "-c" << QString(R"(strings %1 | grep DC2c.\[0-9\] | uniq)").arg(dcBinFile);

    p.start("bash", params);
    p.waitForFinished();
    return QString(p.readAllStandardOutput()).trimmed().split(QRegularExpression("\\s")).first();
}

class hwapi;
Update::Update(QString customerRepository,
               QString customerNrStr,
               QString branchName,
               QString plugInDir,
               QString pluginName,
               QString workingDir,
               QString psaDcDir,
               bool dryRun,
               QObject *parent,
               char const *serialInterface,
               char const *baudrate)
    : QObject(parent) {
#if 0

    , m_hw(loadDCPlugin(QDir(plugInDir), pluginName))
    , m_serialInterface(serialInterface)
    , m_baudrate(baudrate)
    , m_customerRepository(customerRepository)
    , m_customerNrStr(customerNrStr)
    , m_branchName(branchName)
    , m_pluginName(pluginName)
    , m_workingDir(workingDir)
    , m_psaDcDir(psaDcDir)
    , m_dryRun(dryRun)
    , m_sys_areDCdataValid(false) {

    if (!m_hw) {
        qCritical() << "(" << __func__ << ":" << __LINE__ << ") m_hw == nullptr -> ca-slave plugin loaded ???";
    } else {

        // carun stoppen
    }
#endif
    m_start = QDateTime::currentDateTime();
}

Update::~Update() {
    unloadDCPlugin();
}

Update::DownloadResult Update::sendStatus(int ret) const {
    switch (ret) {                  // return values of dc are:
    case 0:                         // 0: no answer by now
        return DownloadResult::NOP; // 1: error
    case 10:                        // 10: success
        return DownloadResult::OK;
    default:;
    }
    return DownloadResult::ERROR;
}

Update::DownloadResult
Update::sendNextAddress(int bNum) const {
    // sends address only if blockNumber is one of 0, 1024, 2048, 3072, 4096
    int noAnswerCount = 0;
    int errorCount = 0;
    if ( bNum==0 || bNum==1024 || bNum==2048 || bNum==3072 || bNum==4096 ) {
        // qDebug() << "addr-block" << bNum << "...";
        while (noAnswerCount <= 250) {
            // TODO
            // m_hw->bl_sendAddress(bNum);

            QThread::msleep(100);

            // TODO
            // DownloadResult const res = sendStatus(m_hw->bl_wasSendingAddOK());

            DownloadResult const res = DownloadResult::OK;

            if (res != DownloadResult::NOP) {
                if (res == DownloadResult::ERROR) {
                    if (++errorCount >= 10) {
                        qCritical() << "addr-block" << bNum << "...FAILED";
                        return res;
                    }
                } else { // res == DownloadResult::OK
                    qInfo() << nextTimePoint().toUtf8().constData() << "addr-block" << bNum << "...done";
                    return res;
                }
            } else {
                noAnswerCount += 1; // no answer by now
            }
        }
        // wait max. about 3 seconds
        return DownloadResult::TIMEOUT;
    }
    // blockNumber is not one of 0, 1024, 2048, 3072, 4096 -> do nothing
    return DownloadResult::NOP;
}

Update::DownloadResult
Update::sendNextDataBlock(QByteArray const &binary, int bNum) const {
    uint8_t local[66];
    int const bAddr = bNum * 64;
    int noAnswerCount = 0;
    int errorCount = 0;

    memcpy(local, binary.constData() + bAddr, 64);
    local[64] = local[65] = 0x00;

    QString s = nextTimePoint();
    s += " sending block ";
    s += QString("%1/%2 ...done <DC-PROGRESS>").arg(bNum).arg(m_totalBlocks);
    s += QString::number(ceil(((bNum * 100.0) / (double)m_totalBlocks)));

    qInfo() << s.toUtf8().constData();

    QThread::msleep(200);
    return DownloadResult::OK;

    // QByteArray b((const char *)(&local[0]), 64);
    // qCritical() << "SNDB" << bNum << b.size() << b.toHex();

    while (noAnswerCount <= 250) {
        // TODO
        // m_hw->bl_sendDataBlock(64, local);

        // TODO
        // DownloadResult const res = sendStatus(m_hw->bl_wasSendingDataOK());

        DownloadResult const res = DownloadResult::OK;

        if (res != DownloadResult::NOP) {
            if (res == DownloadResult::ERROR) {
                if (++errorCount >= 10) {
                    qCritical() << "data for block" << bNum << "...FAILED";
                    return res;
                }
            } else {
                qInfo() << nextTimePoint().toUtf8().constData() << "data for block"
                        << QString("%1/%2").arg(bNum).arg(m_totalBlocks) << "done";
                return res;
            }
        } else {
            noAnswerCount += 1; // no answer by now
        }
    }
    // wait max. about 3 seconds
    return DownloadResult::TIMEOUT;
}

bool Update::startBootloader() const {
    QThread::msleep(1000);
    qInfo() << nextTimePoint().toUtf8().constData() << "starting bootloader ...done";
    return true;

#if 0
    int nTry = 5;
    while (--nTry >= 0) {
        m_hw->bl_startBL();
        QThread::msleep(5000);
        m_hw->bl_checkBL();
        if (m_hw->bl_isUp()) {
            qInfo() << "starting bootloader...OK";
            QThread::msleep(5000);
            return true;
        } else {
            qCritical() << "bootloader not up (" << nTry << ")";
        }
    }
    qCritical() << "starting bootloader...FAILED";
    return false;
#endif
}

bool Update::stopBootloader() const {
    QThread::msleep(1000);
    qInfo() << nextTimePoint().toUtf8().constData() << "stopping bootloader ...done";
    return true;

#if 0
    qDebug() << "stopping bootloader...";
    int nTry = 5;
    while (--nTry >= 0) {
        m_hw->bl_stopBL();
        QThread::msleep(500);
        if (!m_hw->bl_isUp()) {
            qInfo() << "stopping bootloader...OK";
            return true;
        }
    }
    qCritical() << "stopping bootloader...FAILED";
    return false;
#endif
}

bool Update::resetDeviceController() const {
    // TODO
    // m_hw->bl_rebootDC();

    // wait maximally 3 seconds, before starting bootloader
    QThread::sleep(1);

    qInfo() << nextTimePoint().toUtf8().constData()
            << "resetting device controller ...done";

    return true;
}


QByteArray Update::loadBinaryDCFile(QString const &filename) const {

    QFile file(filename); // closed in destructor call
    if (!file.exists()) {
        qCritical() << "(" << __func__ << ":" << __LINE__ << ")"
                    << file.fileName() << "does not exist";
        return QByteArray();
    }
    if (!file.open(QIODevice::ReadOnly)) {
        qCritical() << "(" << __func__ << ":" << __LINE__ << ")"
                    << "cannot open file" << file.fileName();
        return QByteArray();
    }

    qInfo() << nextTimePoint().toUtf8().constData()
            << "loading dc binary to memory" << Update::dcVersion(filename) << "...done";

    return file.readAll();
}
/*
 ///////////////////////////////////////////////////////////////////////////////
 //
 //                        USING THE DC BOOTLOADER
 //
 ///////////////////////////////////////////////////////////////////////////////

 1  :   bl_reboot()     // send to application, want DC2 to reset (in order to
                        // start the bootloader)
                        //
                        // NOTE: this function is NOT reliable !!! Sometimes it
                        // simply does not work, in which case bl_startBL,
                        // bl_checkBL and bl_isUp do not work as well.
                        // Alas, there is no feedback if bl_reboot worked!
                        //
                        // NOTE: this function can be called only once per
                        // minute, because once called again, the controller
                        // performs some self-checks consuming some time.
                        //
                        // NOTE: after a successful bl_reboot(), the device is
                        // waiting about 4 seconds in the bootloader. To stay in
                        // the bootloader, we have to send the command
                        // bl_startBL(), which is kind of a misnomer, as it
                        // should be bl_doNotLeaveBL().
                        //
 2  :   bl_startBL():   // send within 4s after DC power-on, otherwise
                        // bootloader is left.
                        //
                        // NOTE: a running bootloader is a MUST for the download
                        // process of a device controller firmware as it does
                        // the actual writing of the memory (the bl_reboot()
                        // from above erases the available memory).
                        //
 3  :   bl_check():     // send command to verify if bl is up
                        //
                        // NOTE: this command is kind of a request that we want
                        // to check if the bootloader is up. The device
                        // (actually the bootloader) responds with its version.
                        //
 4  :   bl_isUp():      // returns true if bl is up and running
                        //
                        // NOTE: we know what the bootloader version actually is
                        // as the bootloader does not change. By comparing the
                        // string received in the previous step with this known
                        // version string we know if the bootloader is up.
                        //
                        // NOTE FOR ALL PREVIOUS STEPS: execute them in their
                        // own slots each to be sure to receive any possible
                        // responds from the device.
                        //
 5  :   bl_sendAddress(blockNumber)
                        // send start address, nr of 64-byte block, start with 0
                        // will be sent only for following block-numbers:
                        // 0, 1024, 2048, 3072 and 4096, so basically every
                        // 64kByte.
                        // for other addresses nothing happens

 6  :   bl_wasSendingAddOK()
                        // return val:  0: no response by now
                        //              1: error
                        //             10: OK

 7  :   bl_sendDataBlock()
                        // send 64 byte from bin file

 8  :   bl_sendLastBlock()
                        // send this command after all data are transferred

 9  :   bl_wasSendingDataOK()
                        // return val:  0: no response by now
                        //              1: error
                        //             10: OK

 10 :   bl_stopBL()     // leave bl and start (the new) application
                        //
                        // NOTE: this function MUST work under all conditions.
                        // Alas, there is no direct result for this command, so
                        // the only way of knowing it was successful is to ask
                        // the device if the bootloader is still running.
                        // There is no problem to repeat this command until the
                        // bootloader is really not running anymore.
 */
bool Update::doUpdate(QString const &dcFileName) {
    qInfo() << "<DC-VERSION>" << Update::dcVersion(dcFileName);

    m_dcFileName = dcFileName;

    //QString const &fToWorkOn = usbStickDetected ? QDir::cleanPath(it->trimmed())
    //: QDir::cleanPath(m_customerRepository + QDir::separator() + it->trimmed());

    //if (!m_hw) {
    //    qCritical() << "(" << __func__ << ":" << __LINE__ << "):"
    //                << "ERROR!!! m_hw == nullptr";
    //    return false;
    //}

    QByteArray ba = loadBinaryDCFile(m_dcFileName);
    if (ba.size() > 0) {
        m_totalBlocks = (((ba.size())%64)==0) ? (ba.size()/64) : (ba.size()/64)+1;

        qInfo() << nextTimePoint().toUtf8().constData() << "blocks to send" << m_totalBlocks;

        // fill last block of data to be sent with 0xFF
        ba = ba.leftJustified(m_totalBlocks*64, (char)(0xFF));

        resetDeviceController();
        if (startBootloader()) {
            int currentBlock = 0;
            DownloadResult res = DownloadResult::OK;
            qInfo() << nextTimePoint().toUtf8().constData() << "64-byte block" << currentBlock;

            while (res != DownloadResult::ERROR && currentBlock < m_totalBlocks) {
                if ((res = sendNextAddress(currentBlock)) != DownloadResult::ERROR) {
                    if ((res = sendNextDataBlock(ba, currentBlock)) != DownloadResult::ERROR) {
                        // TODO
                        // m_hw->dcDownloadSetCurrentBlockNumber(currentBlock);
                        currentBlock += 1;
                    } else break;
                }
            }

#if 0
            qCritical() << "DownloadThread::run(): last 64-byte block %04d" << currentBlock;

            int const rest = ba.size() % 64;
            int const offset = ba.size() - rest;
            char const *startAddress = ba.constData() + offset;

            if (rest > 0) {
                // SHOULD NEVER HAPPEN !!!
                uint8_t local[66];
                memset(local, 0xFF, sizeof(local));
                memcpy(local, startAddress, rest);
                qCritical() << "DownloadThread::run(): ERROR SEND REMAINING" << rest << "BYTES";
                m_hw->bl_sendDataBlock(64, local);
            } else {
                m_hw->bl_sendLastBlock();
                m_hw->dcDownloadSetCurrentBlockNumber(currentBlock);
            }
            qCritical() << "DownloadThread::run(): last result" << (int)sendStatus(m_hw->bl_wasSendingDataOK());
#endif
        }
        stopBootloader();   // there is no harm in stopping the bootloader even
                            // if starting the bootloader failed
        qInfo() << nextTimePoint().toUtf8().constData() << "<DC-UPDATE-SUCCESS>";
        return true;
    }

    qInfo() << nextTimePoint().toUtf8().constData() << "<DC-UPDATE-FAILURE>";
    return false;
}

#if 0
bool Update::checkJsonVersions(QStringList const& jsonFileNames) {
    if (!m_hw) {
        qCritical() << "(" << __func__ << ":" << __LINE__ << "):"
                    << "ERROR!!! m_hw == nullptr";
        return false;
    }

    int tries = 20;
    while ((m_sys_areDCdataValid = m_hw->sys_areDCdataValid()) == false) {
        // must deliver 'true', only then are all data from hwapi valid
        if (--tries < 0) {
            qCritical() << "(" << __func__ << ":" << __LINE__ << "):"
                        << "ERROR!!! DC DATA NOT VALID -> CA-SLAVE-PLUGIN NOT CONNECTED";
            return false;
        }
        qCritical() << "(" << __func__ << ":" << __LINE__ << "):"
                    << "ERROR!!! DC DATA NOT VALID -> CA-SLAVE-PLUGIN NOT CONNECTED (" << tries << ")";
        m_hw->dc_autoRequest(true);
        QThread::msleep(500);
    }

    for (QStringList::size_type i=0; i < jsonFileNames.size(); ++i) {

        uint8_t jsonNr = 0;

        QString const &fName = jsonFileNames[i];

        // send one request for every single version
        // jsonNr=1...36, 1=config file (cust.Nr) 2=devices  3=cash  4=res.
        //      6=printer template 1 ..... 36= template 32

        if (fName.endsWith("conf.json")) {
            jsonNr = 1;
        } else
        if (fName.endsWith("device.json")) {
            jsonNr = 2;
        } else
        if (fName.endsWith("cash.json")) {
            jsonNr = 3;
        } else {
            QRegularExpressionMatch match;
            static const QRegularExpression re("^(.*print)([0-3][0-9])\\.json\\s*$");
            int idx = fName.indexOf(re, 0, &match);
            if (idx != -1) {
                QString captured = match.captured(match.lastCapturedIndex());
                bool ok = false;
                int n = captured.toInt(&ok);
                if (ok) {
                    // note: use 5 (instead of 4 -> index has been shifted)
                    jsonNr = n + 5;
                }
            }
        }

        if (jsonNr != 0) {
            // send one request for every single version
            // jsonNr=1...36, 1=config file (cust.Nr) 2=devices  3=cash  4=res.
            //      5=printer template 1 ..... 36= template 32

            m_hw->sys_requestJsonVersions(jsonNr);
            QThread::msleep(500);

            char buf[64];
            memset(buf, 0x00, sizeof(buf));
            m_hw->sys_getJsonVersions(jsonNr, buf);
            buf[16] = '\0'; // the DC only handles 16 bytes

            static const QByteArray cb(16, (char)0xff);

            QString const installedVersion(QString::fromStdString(buf));
            QString const fileVersion = getFileVersion(jsonFileNames[i]);

            QFileInfo fi(jsonFileNames[i]);

            qCritical() << endl;
            qCritical() << "                json request nr:" << jsonNr;

            if (installedVersion == fileVersion) {
                qCritical() << "                      json file:" << fi.fileName();
                qCritical() << "        installed version in DC:" << installedVersion;
            } else
            if (cb == QByteArray(buf) && fileVersion == "") {
                qCritical() << "unknown json file (repo and DC):" << fi.fileName();
            } else {
                qCritical() << "                      json file:" << fi.fileName();
                qCritical() << "        installed version in DC:" << installedVersion;
                qCritical() << "     file version in repository:" << fileVersion;
            }

        } else {
            qCritical() << "CANNOT FIND JSON-NR FOR" << fName;
        }
    }

    return false;
}


QString Update::getFileVersion(QString const& jsonFileName) {
    // "version":"15.10.2023 14:55 02.00.06",
    static const QRegularExpression re("^.*(\\\"[Vv]ersion\\\":)([\\s\\\"]{0,})([^,\\\"]{0,}).*$");

    QString fileVersion("");
    QFile inputFile(QDir::cleanPath(m_customerRepository + QDir::separator() + jsonFileName));

    if (inputFile.exists()) {
        if (inputFile.open(QIODevice::ReadOnly)) {
           QTextStream in(&inputFile);
           while (!in.atEnd()) {
              QString line = in.readLine();

              QRegularExpressionMatch match;
              int idx = line.indexOf(re, 0, &match);
              if (idx != -1) {
                  int const lastCaptured = match.lastCapturedIndex();
                  // the dc only sends 16 Byte
                  fileVersion = match.captured(lastCaptured);
                  fileVersion.truncate(16);
                  break;
              }
           }
           inputFile.close();
        }
    } else {
        // qCritical() << "ERROR" << inputFile.fileName() << "does not exist";
    }

    return fileVersion;
}

bool Update::downloadJson(enum FileTypeJson type,
                          int templateIdx,
                          QString jsFileToSendToDC) const {

    m_hw->dc_autoRequest(true); // downloading Json needs the AutoEmission flag
    qDebug() << "SET AUTO-REQUEST=TRUE";
    QThread::sleep(1);  // make sure the auto-request flag is acknowledged

    QStringList lst;
    bool ready = false;
    int nTry = 25;
    while ((ready = m_hw->sys_ready4sending()) == false) {
        QThread::msleep(200);
        if (--nTry <= 0) {
            qCritical() << "SYS NOT READY FOR SENDING AFTER 5 SECONDS";
            break;
        }
    }

    bool ret = false;
    QString msg;
    lst.clear();
    if (ready) {
        QFile file(jsFileToSendToDC);
        QFileInfo fi(jsFileToSendToDC); // max. size of template file is 800 bytes
        if (file.exists()) {
            if (file.open(QIODevice::ReadOnly)) {
                if (fi.size() > 0 && fi.size() <= 800) {
                    QByteArray ba = file.readAll();
                    // kindOfFile: 1=config, 2=device, 3=cash, 4=serial, 5=time, 6=printer
                    //      nrOfTemplate=1...32 if kindOfFile==6
                    //      content = content of the Json file, max 800byte ascii signs
                    if (m_hw->sys_sendJsonFileToDc((uint8_t)(type),
                                                   templateIdx,
                                                   (uint8_t *)ba.data())) {

                        /*
                         *  Note: the machine id is contained in DC2C_conf.json.
                         *  The idea was to use this to check if the download of
                         *  the json-file was correct. It did not work, as the
                         *  update of the PSA (to reflect a change in the
                         *  machine id) did not happen immediately.
                         *
                            m_hw->dc_autoRequest(true);
                            QThread::msleep(500);

                            // testing
                            m_hw->request_ReadbackMachineID();
                            QThread::msleep(500);

                            uint8_t data[64];
                            memset(data, 0x00, sizeof(data));
                            uint8_t length = 0;

                            m_hw->readback_machineIDdata(&length, data);

                            QThread::msleep(500);

                            QByteArray ba((const char*)data, length);

                            qCritical() << length << "MACHINE ID =" << ba.toHex(':');
                        */

                        ret = true;
                    } else {
                        qCritical() << QString("ERROR SEND JSON-FILE %1 TO DC").arg(file.fileName());
                    }
                } else {
                    qCritical() << QString("SIZE OF %1 TOO BIG (%2 BYTES)").arg(jsFileToSendToDC).arg(fi.size());
                }
            } else {
                qCritical() << QString("CAN NOT OPEN ") + jsFileToSendToDC + " FOR READING";
            }
        } else {
            qCritical() << (QString(jsFileToSendToDC) + " DOES NOT EXIST");
        }
    }

    m_hw->dc_autoRequest(false);
    qDebug() << "SET AUTO-REQUEST=FALSE";
    QThread::sleep(1);  // make sure the auto-request flag is acknowledged

    return ret;
}

bool Update::updatePrinterTemplate(int templateIdx, QString jsFile) const {
    return downloadJson(FileTypeJson::PRINTER, templateIdx, jsFile);
}

bool Update::updateConfig(QString jsFile) {
    return downloadJson(FileTypeJson::CONFIG, 0, jsFile);
}

bool Update::updateCashConf(QString jsFile) {
    return downloadJson(FileTypeJson::CASH, 0, jsFile);
}

bool Update::updateDeviceConf(QString jsFile) {
    return downloadJson(FileTypeJson::DEVICE, 0, jsFile);
}
#endif