#ifndef SENSORLOGSSERVICE_H
#define SENSORLOGSSERVICE_H

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

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

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
