#ifndef ISMASDATA_H
#define ISMASDATA_H

#include <QJsonObject>
#include <QJsonArray>
#include <QDateTime>

namespace ISMAS {

    static QString computeTimeStamp() {
        QDateTime const local(QDateTime::currentDateTime());

        QDateTime utc(local);
        utc.setTimeSpec(Qt::UTC);

        int const diff = (int)local.secsTo(utc); // diff between UTC and local time

        QTime t(0, 0, 0);
        t = t.addSecs((uint)diff);

        QString const st(QString("%1%2").arg(diff < 0 ? "-" : "+").arg(t.toString("hh:mm")));
        return QString (local.toString(Qt::ISODateWithMs) + st);
    }

    enum RESULT_CODE {
        E_SUCCESS=0,
        // if between 00:00 - 04:00 Wait-button state not WAIT, then we assume
        // that's an automatic nightly (not-necessary) update
        E_NO_UPDATE_NECESSARY=1,
        // if APISM reports the ISMAS is not available (15x, 6s delay each)
        E_ISMAS_NO_CONNECTION_ERROR=2,
        // if not within 00:00-04:00 and WAIT-button was not in state WAIT
        E_ISMAS_TRIGGER_ERROR=3,
        // cloning git repo. not possible
        E_GIT_CLONE_ERROR=4,
        // pulling from remote git server not possible
        E_GIT_PULL_ERROR=5,
        // fetching from remote git server not possible
        E_GIT_FETCH_ERROR=6,
        // merging fetched data not possible
        E_GIT_MERGE_ERROR=7,
        // check sanity of local customer-repository failed
        E_GIT_CHECK_REPOSITORY_ERROR=8,
        // switch/checkout of branch (i.e. zone) on error
        E_GIT_SWITCH_BRANCH_ERROR=9,
        // fetch/pull of new branch failed. the new branch was not available
        // when installing via SD-card followed by intial clone during the
        // update process.
        E_GIT_FETCH_NEW_BRANCH_ERROR=10,
        // error computing git-blob hash-value
        E_GIT_HASH_ERROR=11,
        // update for general json files failed.
        E_JSON_FILES_UPDATE_ERROR=12,
        // error downloading config-json-files to device controller
        E_JSON_FILES_DOWNLOAD_ERROR=13,
        // error downloading device-controller
        E_DC_DOWNLOAD_ERROR=14,
        // error rsyncing json/ini-files to local filesystem
        E_RSYN_ERROR=15,
        // HASH_VALUE_ERROR=14,
        // HW_COMPATIBILITY_ERROR=15,
        E_OPKG_COMMANDS_ERROR=16,
        // CLEANUP_ERROR=18,
        E_UPDATE_IN_ERROR_STATE=99
    };

//
// Note:
//    ! After U0002 immer ein CMD_SENDVERSION
//    ! Only U0002 and U0003 finish the Update process.
//    ! U0001: Update finished but not activated
//    ! U0002: Update finished and activated
//    ! U0003: Update finished but FAILed.
//
// #define _ISMAS_DONE                 "U0001" // 100%, Check: Resultcode: 0
// #define _ISMAS_SET_WAIT_OK          "U0002" // empty WAIT-button (""), ResultCode: 0
// #define _ISMAS_NO_UPDATE_NECESSARY  "M0100" // empty WAIT-button (""), ResultCode: 0
// #define _ISMAS_FAILURE              "U0003" // FAIL
// #define _ISMAS_CONTINUE             "U0010" // %-values: update running, result codes according running step
// #define _ISMAS_RESET_WAIT           "ISMAS" // reset WAIT-button to "WAIT"
// #define _ISMAS_TEST_TRIGGER         "U0099" // check the WAIT-button

    static constexpr const char *DONE               {"U0001"};
    static constexpr const char *SET_WAIT_OK        {"U0002"};
    static constexpr const char *NO_UPDATE_NECESSARY{"M0100"};
    static constexpr const char *FAILURE            {"U0003"};
    static constexpr const char *CONTINUE           {"U0010"};
    static constexpr const char *RESET_WAIT         {"ISMAS"};
    static constexpr const char *TEST_TRIGGER       {"U0099"};

    struct EventData : public QJsonObject {
        struct : public QJsonObject {
            QJsonValue reason;
            QJsonValue timestamp;
            QJsonValue eventID;
            QJsonValue event;
            QJsonValue eventState;
            struct : public QJsonObject {
                QJsonValue percent;
                QJsonValue resultCode;
                QJsonValue step;
                QJsonValue stepResult;
                QJsonValue version;
            } parameter;
        } newsToIsmas;

        explicit EventData(QString const &event,
                           int percent,
                           int resultCode,
                           QString const &step,
                           QString const &stepResult,
                           QString const &version = "",
                           QString const &reason = "SW_UP") {
            newsToIsmas.reason = reason;
            newsToIsmas.timestamp = computeTimeStamp();
            newsToIsmas.eventID = QString{"0"};
            newsToIsmas.event = event;
            newsToIsmas.eventState = 1;
            newsToIsmas.parameter.percent = percent;
            newsToIsmas.parameter.resultCode = resultCode;
            newsToIsmas.parameter.step = step;
            newsToIsmas.parameter.stepResult = stepResult;
            newsToIsmas.parameter.version = version;
        }
    };

    struct StateData : public QJsonObject {
        QJsonValue Timestamp;
        QJsonArray HW_States;
        struct : public QJsonObject {
            QJsonValue name;
            QJsonValue value;
            QJsonValue unit;
        } machineState;                  //
    };


    enum class REQUEST : quint8 {
        NO_REQUEST,
        PING,
        SELF,
        PARAMETER
    };
}



#endif // ISMASDATA_H