#ifndef GETSENSORS_R_H
#define GETSENSORS_R_H

#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QList>
#include <QString>

class Sensor
{
public:
    int alarmCooldown;
    double alarmLowerBound;
    double alarmUpperBound;
    bool isBooled; // Поле из EXTRA
    bool isHide;   // Поле из EXTRA
    QString group;
    QString id;
    bool isAlarmEnabled;
    bool isEnabled;
    double lastValue;
    QString name;
    QString unitName;

    void parseSensor(const QJsonObject &sensorObj)
    {
        alarmCooldown = sensorObj["ALARM_COOLDOWN"].toInt();
        alarmLowerBound = sensorObj["ALARM_LOWERBOUND"].toDouble();
        alarmUpperBound = sensorObj["ALARM_UPPERBOUND"].toDouble();

        // Обработка полей из EXTRA
        QString extraString = sensorObj["EXTRA"].toString();
        if (!extraString.isEmpty()) {
            QJsonParseError parseError;
            QJsonDocument extraDoc = QJsonDocument::fromJson(extraString.toUtf8(), &parseError);
            if (parseError.error == QJsonParseError::NoError && extraDoc.isObject()) {
                QJsonObject extraObj = extraDoc.object();
                isBooled = extraObj.value("isBool").toBool(false);
                isHide = extraObj.value("isHide").toBool(true); // Обратное значение "isHide"
            } else {
                isBooled = false;
                isHide = false;
            }
        } else {
            isBooled = false;
            isHide = false;
        }

        group = sensorObj["GROUP"].toString();
        id = sensorObj["ID"].toString();
        isAlarmEnabled = sensorObj["IS_ALARM_ENABLED"].toBool();
        isEnabled = sensorObj["IS_ENABLED"].toBool();
        lastValue = sensorObj["LAST_VALUE"].toDouble();
        name = sensorObj["NAME"].toString();
        unitName = sensorObj["UNIT_NAME"].toString();
    }
};

class GetSensorsR
{
public:
    GetSensorsR() = default;

    void parseResponse(const QJsonObject &responseObj)
    {
        if (responseObj.contains("sensors") && responseObj["sensors"].isArray()) {
            QJsonArray sensorsArray = responseObj["sensors"].toArray();
            for (const QJsonValue &value : sensorsArray) {
                if (value.isObject()) {
                    Sensor sensor;
                    sensor.parseSensor(value.toObject());
                    sensors.append(sensor);
                }
            }
        }
    }

    const QList<Sensor> &getSensors() const { return sensors; }

private:
    QList<Sensor> sensors;
};

#endif // GETSENSORS_R_H
