#ifndef SENSORLOGSSERVICE_H
#define SENSORLOGSSERVICE_H

#include "httpclient.h"
#include "sensorlogs.h"
#include "utils.h"

#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QMessageBox>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QObject>

class SensorLogService : public QObject
{
    Q_OBJECT

public:
    explicit SensorLogService(HttpClient *client, QObject *parent = nullptr)
        : QObject(parent)
        , httpClient(client)
    {}

    SensorLogs getSensorLog(const QString &targetSensorId)
    {
        QJsonObject responseSettingsJson = httpClient->get(utils::API_URL
                                                           + "/settings/deviceSettings");

        QJsonObject sensors = responseSettingsJson["SENSORS"].toObject();
        for (const QString &groupKey : sensors.keys()) {
            QJsonObject groupData = sensors[groupKey].toObject();
            for (const QString &sensorKey : groupData.keys()) {
                if (sensorKey == targetSensorId) {
                    QJsonObject sensorData = groupData[sensorKey].toObject();
                    SensorLogs sensorLogs;
                    sensorLogs.SensorLogsParse(sensorData);
                    return sensorLogs;
                }
            }
        }

        return {};
    }

private:
    HttpClient *httpClient;
};

#endif // SENSORLOGSSERVICE_H
