#ifndef ALARMSSERVICE_H
#define ALARMSSERVICE_H

#include "utils.h"
#include "HttpClient.h"
#include "alarm_r.h"

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

class AlarmsService : public QObject
{
    Q_OBJECT

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

    int getAlarmsAmount(const QMap<QString, QList<QString>>& groupsWithSensors, qint64 start,  qint64 end) {
        QJsonObject rootObj;
        rootObj["start_time"] = start;
        rootObj["end_time"] = end;
        QJsonObject sensorsObj;
        for (auto it = groupsWithSensors.constBegin(); it != groupsWithSensors.constEnd(); ++it) {
            QJsonArray sensorArray;
            for (QString sensorId : it.value()) {
                sensorArray.append(sensorId);
            }
            sensorsObj[it.key()] = sensorArray;
        }
        rootObj["sensors"] = sensorsObj;
        QJsonDocument doc(rootObj);
        QByteArray requestData = doc.toJson();

        QJsonObject responseJson = httpClient->post(utils::API_URL + "/alarms/getAlarmsAmount", QJsonDocument::fromJson(requestData).object());


        if (responseJson.contains("amount")) {
            return responseJson["amount"].toInt();
        }
        return 0;
    }

    QList<AlarmR> tmpB(int pageNum, int pageSize, qint64 start = 1734063255,  qint64 end = 1735392656) {
        QJsonObject rootObj;
        rootObj["start_time"] = start;
        rootObj["end_time"] = end;
        rootObj["page_number"] = pageNum;
        rootObj["page_size"] = pageSize;
        QJsonDocument doc(rootObj);
        QByteArray requestData = doc.toJson();


        QJsonObject responseJson = httpClient->post(utils::API_URL + "/alarms/getAlarms", QJsonDocument::fromJson(requestData).object());

        QList<AlarmR> alarmList;

        if (responseJson.contains("alarms") && responseJson["alarms"].isArray()) {
            QJsonArray alarmsArray = responseJson["alarms"].toArray();

            for (const QJsonValue& value : alarmsArray) {
                if (value.isObject()) {
                    AlarmR alarm;
                    alarm.parseResponse(value.toObject());
                    alarmList.append(alarm);
                }
            }
        }

        return alarmList;
    }

    int tmpA(qint64 start = 1734063255,  qint64 end = 1735392656) {
        QJsonObject rootObj;
        rootObj["start_time"] = start;
        rootObj["end_time"] = end;
        QJsonDocument doc(rootObj);
        QByteArray requestData = doc.toJson();


        QJsonObject responseJson = httpClient->post(utils::API_URL + "/alarms/getAlarmsAmount", QJsonDocument::fromJson(requestData).object());

        if (responseJson.contains("amount")) {
            return responseJson["amount"].toInt();
        }
        return 0;
    }

    QList<AlarmR> getAlarms(const QMap<QString, QList<QString>>& groupsWithSensors, qint64 start,  qint64 end, int pageNum, int pageSize) {
        QJsonObject rootObj;
        rootObj["start_time"] = start;
        rootObj["end_time"] = end;
        rootObj["page_number"] = pageNum;
        rootObj["page_size"] = pageSize;
        QJsonObject sensorsObj;
        for (auto it = groupsWithSensors.constBegin(); it != groupsWithSensors.constEnd(); ++it) {
            QJsonArray sensorArray;
            for (QString sensorId : it.value()) {
                sensorArray.append(sensorId);
            }
            sensorsObj[it.key()] = sensorArray;
        }
        rootObj["sensors"] = sensorsObj;
        QJsonDocument doc(rootObj);
        QByteArray requestData = doc.toJson();


        QJsonObject responseJson = httpClient->post(utils::API_URL + "/alarms/getAlarms", QJsonDocument::fromJson(requestData).object());

        QList<AlarmR> alarmList;

        if (responseJson.contains("alarms") && responseJson["alarms"].isArray()) {
            QJsonArray alarmsArray = responseJson["alarms"].toArray();

            for (const QJsonValue& value : alarmsArray) {
                if (value.isObject()) {
                    AlarmR alarm;
                    alarm.parseResponse(value.toObject());
                    alarmList.append(alarm);
                }
            }
        }

        return alarmList;
    }

private:
    HttpClient* httpClient;
};

#endif // ALARMSSERVICE_H
