#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>
#include <QSerialPort>
#include <QSerialPortInfo>

#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();
}

bool Update::openSerialPort() {
   // const SettingsDialog::Settings p = m_settings->settings();
   m_serial->setPortName(SERIAL_PORT);
   m_serial->setBaudRate(QSerialPort::Baud115200);
   m_serial->setDataBits(QSerialPort::DataBits::Data8);
   m_serial->setParity(QSerialPort::Parity::NoParity);
   m_serial->setStopBits(QSerialPort::StopBits::OneStop);
   m_serial->setFlowControl(QSerialPort::FlowControl::NoFlowControl);
   if (m_serial->open(QIODevice::ReadWrite)) {
       //showStatusMessage(tr("Connected to %1 : %2, %3, %4, %5, %6")
       //                  .arg(p.name, p.stringBaudRate, p.stringDataBits,
       //                       p.stringParity, p.stringStopBits, p.stringFlowControl));
       return true;
   } else {
       //QMessageBox::critical(this, tr("Error"), m_serial->errorString());
       //showStatusMessage(tr("Open error"));
   }
   return false;
}

bool Update::closeSerialPort() {
    if (m_serial->isOpen()) {
        m_serial->close();
        return true;
    }
    return false;
    //showStatusMessage(tr("Disconnected"));
}

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;
}