#include "CArun.h"
#include "datei.h"

#include "DigitalOutputAbstraction.h"


CArun::CArun(QObject *parent)
    : QObject(parent)
    , setupStep(SETUP_STEP::INIT)
{
    this->timerChainCtrl = new QTimer(this);
    this->connect(timerChainCtrl, SIGNAL(timeout()), this, SLOT(chainControl()));
    this->timerChainCtrl->setSingleShot(true);
    this->timerChainCtrl->setInterval(100);

    loadPlugIn(1);

    // Start setup-steps:
    qCritical() << "CArun: start setup...";

    this->timerChainCtrl->start();


    this->digitalOutputAbstraction = new DigitalOutputAbstraction(this->HWaccess, this);
    this->digitalOutputAbstraction->addCCWake("/sys/class/leds/wakeupctrl_cc/brightness");
    this->digitalOutputAbstraction->addCCPower("/run/powerctrl_cc");
    this->digitalOutputAbstraction->addCCModem("/run/powerctrl_modem");
}


char CArun::loadPlugIn(char lade1_entlade2)
{
    plugInDir.cd("plugins");
    QPluginLoader *pluginLoader = new QPluginLoader();

    pluginLoader->setFileName("/usr/lib/libCAmaster.so"); // for ptu5

    if (lade1_entlade2==2)
    {
        pluginLoader->unload();
        return 0;
    }

    if (!pluginLoader->load())
    {
        qCritical() << "CArun: cannot load plugin";
    } else
        qCritical() << "CArun: plugin loaded: " << pluginLoader->fileName();

    if (!pluginLoader->isLoaded())
    {
        qCritical() << "CArun: ERROR loading plugin: " << pluginLoader->errorString();;
        return 0;
    }

    QObject *plugin = pluginLoader->instance();
    if ( plugin == nullptr)
    {
        // make instance of the root component (which can hold more then one clases)
        // also loads the lib if not yet done
        qCritical() << "CArun: ERROR cannot start instance";
        return 0;
    }

    this->HWaccess = qobject_cast<hwinf *>(plugin);
    // make instance to class "hwinf" in dll_HWapi.h over "interfaces.h"

    return 0;
}

void CArun::openSerialPort()
{
    #define FILENAME_COMPORT            "../comport.csv"  // TODO: use absolute path
                                                          // use settings (ini-file)
            // comport.csv wird mit installiert in: /opt/app/
            // k.A., ob "../comport.csv" gefunden wird.

    qCritical() << "CArun: open serial port...";

    QString  bs, cn;
    int br, ci;

    // load and use last settings:  --------------------
    QByteArray myBA;
    myBA=datei_readFromFile(FILENAME_COMPORT);
    if (myBA.length()>0)
    {
        bs=csv_getEntryAsString(myBA,0);      // read the 's'     war 2!??
        br=csv_getEntryAsInt(myBA,1);               // z.B. 5 (5.Eintrag in der Baud-Liste)
        bs=csv_getEntryAsString(myBA,2);                // z.B 115200
        cn=csv_getEntryAsString(myBA,3);       // z.B. COM9
        ci=csv_getEntryAsInt(myBA,4);               // Eintragsnummer in COM-Fenster
        this->HWaccess->dc_openSerial(br,bs,cn,1);
    } else
    {
        // vermutlich wird dies hier ausgeführt?

        // open with default settings
        qCritical() << "CArun: open serial with default values";

        bs="115200";
        br=5;
        //cn="COM14";     // Windows
        cn="ttymxc2";   // PTU5
        ci=2;
        HWaccess->dc_openSerial(br,bs,cn,1);
    }
}

void CArun::chainControl(void)
{
    switch (this->setupStep) {
    case SETUP_STEP::INIT:
        qCritical() << "CArun: SETUP_STEP::INIT";
        this->setupStep = SETUP_STEP::OPEN_SERIAL_PORT;
        this->timerChainCtrl->start();
        break;
    case SETUP_STEP::OPEN_SERIAL_PORT:
        qCritical() << "CArun: SETUP_STEP::OPEN_SERIAL_PORT";
        this->openSerialPort();
        this->setupStep = SETUP_STEP::TEST_OPEN_PORT;
        this->timerChainCtrl->start();
        break;
    case SETUP_STEP::TEST_OPEN_PORT:
        qCritical() << "CArun: SETUP_STEP::TEST_OPEN_PORT";
        if (this->HWaccess->dc_isPortOpen()) {
            this->setupStep = SETUP_STEP::TEST_RESPONSE_REQUEST;
        }
        else {
            qCritical() << "CArun: port is still closed, restarting..";
            this->setupStep = SETUP_STEP::INIT;
        }
        this->timerChainCtrl->start();
        break;
    case SETUP_STEP::TEST_RESPONSE_REQUEST:
        qCritical() << "CArun: SETUP_STEP::TEST_RESPONSE_REQUEST";
        this->HWaccess->dc_requTestResponse();
        this->setupStep = SETUP_STEP::TEST_RESPONSE_CHECK;
        this->timerChainCtrl->start();
        break;
    case SETUP_STEP::TEST_RESPONSE_CHECK:
        qCritical() << "CArun: SETUP_STEP::TEST_RESPONSE_CHECK";
        if (this->HWaccess->dc_readAnswTestResponse()) {
            // response was correct
            this->setupStep = SETUP_STEP::SETUP_AUTOREQEUST;
        }
        else {
            qCritical() << "CArun: got no answer from DC, retry..";
            this->setupStep = SETUP_STEP::INIT;
        }
        this->timerChainCtrl->start();
        break;
    case SETUP_STEP::SETUP_AUTOREQEUST:
        qCritical() << "CArun: SETUP_STEP::SETUP_AUTOREQEUST";

        this->HWaccess->dc_autoRequest(1);
        this->setupStep = SETUP_STEP::CHECK_VALID_DATA;
        this->timerChainCtrl->start(2000);
        break;
    case SETUP_STEP::CHECK_VALID_DATA:
        qCritical() << "CArun: SETUP_STEP::CHECK_VALID_DATA";
        if (this->HWaccess->sys_areDCdataValid()) {
            qCritical() << "CArun: DC is connected";
            // do not start timer again
        }
        else {
            qCritical() << "CArun: auto request is not running, retry...";
            this->setupStep = SETUP_STEP::INIT;
            this->timerChainCtrl->start();
        }
        break;
    default:
        qCritical() << "CArun: invalid setup step";
        break;
    }
}