// 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 "ruleseditruleparams.h"

namespace nymea::api {

RulesEditRuleParams RulesEditRuleParams::fromJson(const QJsonObject &object) {
    RulesEditRuleParams value;
    if (object.contains(QStringLiteral("actions"))) {
        value.actions = ([&]() { QList<RuleAction> list; for (const QJsonValue &item : (object.value(QStringLiteral("actions"))).toArray()) { list.append(RuleAction::fromJson((item).toObject())); } return list; }());
    }
    if (object.contains(QStringLiteral("name"))) {
        value.name = (object.value(QStringLiteral("name"))).toString();
    }
    if (object.contains(QStringLiteral("enabled"))) {
        value.enabled = (object.value(QStringLiteral("enabled"))).toBool();
    }
    if (object.contains(QStringLiteral("eventDescriptors"))) {
        value.eventDescriptors = ([&]() { QList<EventDescriptor> list; for (const QJsonValue &item : (object.value(QStringLiteral("eventDescriptors"))).toArray()) { list.append(EventDescriptor::fromJson((item).toObject())); } return list; }());
    }
    if (object.contains(QStringLiteral("executable"))) {
        value.executable = (object.value(QStringLiteral("executable"))).toBool();
    }
    if (object.contains(QStringLiteral("exitActions"))) {
        value.exitActions = ([&]() { QList<RuleAction> list; for (const QJsonValue &item : (object.value(QStringLiteral("exitActions"))).toArray()) { list.append(RuleAction::fromJson((item).toObject())); } return list; }());
    }
    if (object.contains(QStringLiteral("stateEvaluator"))) {
        value.stateEvaluator = StateEvaluator::fromJson((object.value(QStringLiteral("stateEvaluator"))).toObject());
    }
    if (object.contains(QStringLiteral("timeDescriptor"))) {
        value.timeDescriptor = TimeDescriptor::fromJson((object.value(QStringLiteral("timeDescriptor"))).toObject());
    }
    if (object.contains(QStringLiteral("ruleId"))) {
        value.ruleId = QUuid((object.value(QStringLiteral("ruleId"))).toString());
    }
    return value;
}

QJsonObject RulesEditRuleParams::toJson() const {
    QJsonObject object;
    object.insert(QStringLiteral("actions"), ([&]() { QJsonArray array; for (const auto &item : actions) { array.append(QJsonValue((item).toJson())); } return QJsonValue(array); }()));
    object.insert(QStringLiteral("name"), QJsonValue(name));
    if (enabled.has_value()) {
        object.insert(QStringLiteral("enabled"), QJsonValue(*enabled));
    }
    if (eventDescriptors.has_value()) {
        object.insert(QStringLiteral("eventDescriptors"), ([&]() { QJsonArray array; for (const auto &item : *eventDescriptors) { array.append(QJsonValue((item).toJson())); } return QJsonValue(array); }()));
    }
    if (executable.has_value()) {
        object.insert(QStringLiteral("executable"), QJsonValue(*executable));
    }
    if (exitActions.has_value()) {
        object.insert(QStringLiteral("exitActions"), ([&]() { QJsonArray array; for (const auto &item : *exitActions) { array.append(QJsonValue((item).toJson())); } return QJsonValue(array); }()));
    }
    if (stateEvaluator.has_value()) {
        object.insert(QStringLiteral("stateEvaluator"), QJsonValue((*stateEvaluator).toJson()));
    }
    if (timeDescriptor.has_value()) {
        object.insert(QStringLiteral("timeDescriptor"), QJsonValue((*timeDescriptor).toJson()));
    }
    object.insert(QStringLiteral("ruleId"), QJsonValue((ruleId).toString()));
    return object;
}

} // namespace nymea::api
