// SPDX-License-Identifier: GPL-3.0-or-later

// This file is auto-generated by scripts/generate_nymea_api.py
// Any changes made here will be lost when the files are regenerated.
// Do not edit manually.

#include "thing.h"

namespace nymea::api {

Thing Thing::fromJson(const QJsonObject &object) {
    Thing value;
    if (object.contains(QStringLiteral("setupComplete"))) {
        value.setupComplete = (object.value(QStringLiteral("setupComplete"))).toBool();
    }
    if (object.contains(QStringLiteral("name"))) {
        value.name = (object.value(QStringLiteral("name"))).toString();
    }
    if (object.contains(QStringLiteral("settings"))) {
        value.settings = ([&]() { QList<Param> list; for (const QJsonValue &item : (object.value(QStringLiteral("settings"))).toArray()) { list.append(Param::fromJson((item).toObject())); } return list; }());
    }
    if (object.contains(QStringLiteral("id"))) {
        value.id = QUuid((object.value(QStringLiteral("id"))).toString());
    }
    if (object.contains(QStringLiteral("loggedActionTypeIds"))) {
        value.loggedActionTypeIds = ([&]() { QList<QUuid> list; for (const QJsonValue &item : (object.value(QStringLiteral("loggedActionTypeIds"))).toArray()) { list.append(QUuid((item).toString())); } return list; }());
    }
    if (object.contains(QStringLiteral("loggedEventTypeIds"))) {
        value.loggedEventTypeIds = ([&]() { QList<QUuid> list; for (const QJsonValue &item : (object.value(QStringLiteral("loggedEventTypeIds"))).toArray()) { list.append(QUuid((item).toString())); } return list; }());
    }
    if (object.contains(QStringLiteral("loggedStateTypeIds"))) {
        value.loggedStateTypeIds = ([&]() { QList<QUuid> list; for (const QJsonValue &item : (object.value(QStringLiteral("loggedStateTypeIds"))).toArray()) { list.append(QUuid((item).toString())); } return list; }());
    }
    if (object.contains(QStringLiteral("parentId"))) {
        value.parentId = QUuid((object.value(QStringLiteral("parentId"))).toString());
    }
    if (object.contains(QStringLiteral("setupDisplayMessage"))) {
        value.setupDisplayMessage = (object.value(QStringLiteral("setupDisplayMessage"))).toString();
    }
    if (object.contains(QStringLiteral("params"))) {
        value.params = ([&]() { QList<Param> list; for (const QJsonValue &item : (object.value(QStringLiteral("params"))).toArray()) { list.append(Param::fromJson((item).toObject())); } return list; }());
    }
    if (object.contains(QStringLiteral("setupError"))) {
        value.setupError = parseThingError(object.value(QStringLiteral("setupError")));
    }
    if (object.contains(QStringLiteral("setupStatus"))) {
        value.setupStatus = parseThingSetupStatus(object.value(QStringLiteral("setupStatus")));
    }
    if (object.contains(QStringLiteral("states"))) {
        value.states = ([&]() { QList<State> list; for (const QJsonValue &item : (object.value(QStringLiteral("states"))).toArray()) { list.append(State::fromJson((item).toObject())); } return list; }());
    }
    if (object.contains(QStringLiteral("thingClassId"))) {
        value.thingClassId = QUuid((object.value(QStringLiteral("thingClassId"))).toString());
    }
    return value;
}

QJsonObject Thing::toJson() const {
    QJsonObject object;
    object.insert(QStringLiteral("setupComplete"), QJsonValue(setupComplete));
    if (name.has_value()) {
        object.insert(QStringLiteral("name"), QJsonValue(*name));
    }
    if (settings.has_value()) {
        object.insert(QStringLiteral("settings"), ([&]() { QJsonArray array; for (const auto &item : *settings) { array.append(QJsonValue((item).toJson())); } return QJsonValue(array); }()));
    }
    object.insert(QStringLiteral("id"), QJsonValue((id).toString()));
    if (loggedActionTypeIds.has_value()) {
        object.insert(QStringLiteral("loggedActionTypeIds"), ([&]() { QJsonArray array; for (const auto &item : *loggedActionTypeIds) { array.append(QJsonValue((item).toString())); } return QJsonValue(array); }()));
    }
    if (loggedEventTypeIds.has_value()) {
        object.insert(QStringLiteral("loggedEventTypeIds"), ([&]() { QJsonArray array; for (const auto &item : *loggedEventTypeIds) { array.append(QJsonValue((item).toString())); } return QJsonValue(array); }()));
    }
    if (loggedStateTypeIds.has_value()) {
        object.insert(QStringLiteral("loggedStateTypeIds"), ([&]() { QJsonArray array; for (const auto &item : *loggedStateTypeIds) { array.append(QJsonValue((item).toString())); } return QJsonValue(array); }()));
    }
    if (parentId.has_value()) {
        object.insert(QStringLiteral("parentId"), QJsonValue((*parentId).toString()));
    }
    if (setupDisplayMessage.has_value()) {
        object.insert(QStringLiteral("setupDisplayMessage"), QJsonValue(*setupDisplayMessage));
    }
    object.insert(QStringLiteral("params"), ([&]() { QJsonArray array; for (const auto &item : params) { array.append(QJsonValue((item).toJson())); } return QJsonValue(array); }()));
    object.insert(QStringLiteral("setupError"), QJsonValue(toString(setupError)));
    object.insert(QStringLiteral("setupStatus"), QJsonValue(toString(setupStatus)));
    object.insert(QStringLiteral("states"), ([&]() { QJsonArray array; for (const auto &item : states) { array.append(QJsonValue((item).toJson())); } return QJsonValue(array); }()));
    object.insert(QStringLiteral("thingClassId"), QJsonValue((thingClassId).toString()));
    return object;
}

} // namespace nymea::api
