#include "ismas/ismas_client.h"

#include <cstring>
#include <cstdio>

#if 0
# $1: EVENT: U0001 update finished: 100%
#            U0002 reset TRG
#            U0003 error
#            U0010 for update process
# $2: PERCENT : "only for ISMAS: 0-100%",
# $3: RESULTCODE : "only for ISMAS",
#   0: Success
#   1: no Update nessesary
#   2: Backup failed
#   3: Package error/ Wrong package
#   4: Install Error
# $4: STEP : "running step (only for us): update_psa...",
# $5: STEP_RESULT : "error and result text",
# $6: VERSION : "opkg and conf info; what will be updated"
#
#endif

#include <QDateTime>
#include <QDebug>

QString IsmasClient::updateNewsToIsmas(char const *event,
                                       int percent,
                                       int resultCode,
                                       char const *step,
                                       char const *step_result,
                                       char const *version) {
    char buf[1024];
    memset(buf, 0, sizeof(buf));

    QString const ts = QDateTime::currentDateTime().toString(Qt::ISODateWithMs);
    snprintf(buf, sizeof(buf)-1,
        "{"
            "\"REASON\":\"SW_UP\","
            "\"TIMESTAMP\":\"%s\","
            "\"EVENT_ID\":\"0\","
            "\"EVENT\":\"%s\","
            "\"EVENTSTATE\":1,"
            "\"PARAMETER\": {"
                "\"PERCENT\" : %d,"
                "\"RESULTCODE\" : %d,"
                "\"STEP\" : \"%s\","
                "\"STEP_RESULT\" : \"%s\","
                "\"VERSION\" : \"%s\""
            "}"
        "}", ts.toStdString().c_str(), event, percent, resultCode,
             step, step_result, version);
    return buf;
}

QString IsmasClient::updateOfPSASendVersion(QString const &tariffVersion,
                                            QString const &tariffProject,
                                            int tariffZone,
                                            QString const &tariffInfo,
                                            QString const &tariffLoadTime,
                                            QString const &linuxVersion,
                                            QString const &cpuSerial,
                                            QString const &deviceControllerVersion,
                                            QString const &deviceControllerGitBlob,
                                            QString const &deviceControllerGitLastCommit,
                                            QString const &raucVersion,
                                            QString const &opkgVersion,
                                            QString const &atbQTVersion,
                                            QString const &atbQTGitDescribe,
                                            QString const &deviceControllerPluginVersion,
                                            QString const &ingenicoISelfCCPluginVersion,
                                            QString const &mobilisisCalculatePricePluginVersion,
                                            QString const &mobilisisCalculatePriceConfigUiVersion,
                                            QString const &prmCalculatePricePluginVersion,
                                            QString const &prmCalculatePriceConfigUiPluginVersion,
                                            QString const &tcpZVTPluginVersion) {
    char buf[4096];
    memset(buf, 0, sizeof(buf));

    QString const ts = QDateTime::currentDateTime().toString(Qt::ISODateWithMs);
    QString sendVersionHash;

    // local data="#M=APISM#C=CMD_SENDVERSION#J=
    snprintf(buf, sizeof(buf)-1,
        "{"
            "\"VERSION_INFO\" : {"
                "\"CREATED\":\"%s\","
                "\"HASH\":\"%s\""
            "},"
            "\"TARIFF\" : {"
                "\"VERSION\" : \"%s\","
                "\"PROJECT\" : \"%s\","
                "\"ZONE\" : %d,"
                "\"INFO\" : \"%s\","
                "\"LOADED\" : \"%s\""
            "},"
            "\"HARDWARE\" : {"
                "\"DEVICES\" : [\"PTU5\", \"DC\", \"PRINTER\", \"BNA\"]"
            "},"
            "\"OS\" : {"
                "\"Linux\": \"%s\""
            "},"
            "\"CONFIG\" : {"
                "\"PTU5\" : {"
                    "\"CPU_SERIAL\" : \"%s\""
                "},"
                "\"DC\" : {"
                    "\"VERSION\" : \"%s\","
                    "\"GITBLOB\" : \"%s\","
                    "\"GITLASTCOMMIT\" : \"%s\""
                "},"
                "\"PRINTER\" : {"
                "},"
                "\"BNA\" : {"
                "}"
            "},"
            "\"SOFTWARE\": {"
                "\"RAUC\" : \"%s\","
                "\"OPKG\" : \"%s\","
                "\"ATBQT\" : {"
                    "\"VERSION\" : \"%s\","
                    "\"GIT_DESCRIBE\" : \"%s\""
                "}"
            "},"
            "\"PLUGINS\" : {"
                "\"libATBDeviceControllerPlugin.so\" : {"
                    "\"VERSION\" : \"%s\""
                "},"
                "\"libIngenicoISelf_CCPlugin.so\" : {"
                    "\"VERSION\" : \"%s\""
                "},"
                "\"libMOBILISIS_CalculatePricePlugin.so\" : {"
                    "\"VERSION\" : \"%s\""
                "},"
                "\"libMOBILISIS_CalculatePricePlugin_ConfigUi.so\" : {"
                    "\"VERSION\" : \"%s\""
                "},"
                "\"libPRM_CalculatePricePlugin.so\" : {"
                    "\"VERSION\" : \"%s\""
                "},"
                "\"libPRM_CalculatePricePlugin_ConfigUi.so\" : {"
                    "\"VERSION\" : \"%s\""
                "},"
                "\"libTCP_ZVT_CCPlugin.so\" : {"
                    "\"VERSION\" : \"%s\""
                "}"
            "}"
        "}",
        ts.toStdString().c_str(),
        sendVersionHash.toStdString().c_str(),

        tariffVersion.toStdString().c_str(),
        tariffProject.toStdString().c_str(),
        tariffZone,
        tariffInfo.toStdString().c_str(),
        tariffLoadTime.toStdString().c_str(),

        linuxVersion.toStdString().c_str(),

        cpuSerial.toStdString().c_str(),

        deviceControllerVersion.toStdString().c_str(),
        deviceControllerGitBlob.toStdString().c_str(),
        deviceControllerGitLastCommit.toStdString().c_str(),

        raucVersion.toStdString().c_str(),
        opkgVersion.toStdString().c_str(),
        atbQTVersion.toStdString().c_str(),
        atbQTGitDescribe.toStdString().c_str(),

        deviceControllerPluginVersion.toStdString().c_str(),
        ingenicoISelfCCPluginVersion.toStdString().c_str(),
        mobilisisCalculatePricePluginVersion.toStdString().c_str(),
        mobilisisCalculatePriceConfigUiVersion.toStdString().c_str(),
        prmCalculatePricePluginVersion.toStdString().c_str(),
        prmCalculatePriceConfigUiPluginVersion.toStdString().c_str(),
        tcpZVTPluginVersion.toStdString().c_str());

    return buf;
}


QString IsmasClient::updateOfPSAActivated() {
    return updateNewsToIsmas("U0010",
                             1,
                             0,
                             "activated",
                             "detected WAIT state",
                             "1.0.0");
}

QString IsmasClient::updateOfPSASucceeded() {
    return updateNewsToIsmas("U0001",
                             100,
                             0,
                             "update_succeeded",
                             "",
                             "1.0.0");
}

QString IsmasClient::setUpdatesAvailable() {
    return updateNewsToIsmas("U0099",
                             10,
                             0,
                             "set_updates_available",
                             "",
                             "");
}



bool checkForAvailableUpdates();
