#ifndef ALARMSSERVICE_H
#define ALARMSSERVICE_H

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

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

struct AlarmSettings{
    QMap<QString, QList<QString>> groupsWithSensors;
    qint64 start_time;
    qint64 end_time;
    AlarmSettings(QMap<QString, QList<QString>> a, qint64 b, qint64 c) {
        groupsWithSensors = a;
        start_time = b;
        end_time = c;
    }
};

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> 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;
        QJsonArray orderArray;
        QJsonArray orderItem;
        orderItem.append("start_time");
        orderItem.append("-");
        orderArray.append(orderItem);
        rootObj["order"] = orderArray;
        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
