#ifndef JOURNALSERVICE_H
#define JOURNALSERVICE_H

#include "HttpClient.h"
#include "logs_r.h"
#include "utils.h"

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

struct JournalSettings{
    QMap<QString, QList<QString>> groupsWithSensors;
    qint64 start_time;
    qint64 end_time;
    bool cmp;
    QList<int> status;
    JournalSettings(QMap<QString, QList<QString>> a, qint64 b, qint64 c, bool d, QList<int> e) {
        groupsWithSensors = a;
        start_time = b;
        end_time = c;
        cmp = d;
        status = e;
    }
};

class JournalService : public QObject
{
    Q_OBJECT

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

    int getLogsAmount(const QMap<QString, QList<QString>> &groupWithSensors, const QList<int> &threatLevels,
                      qint64 start, qint64 end)
    {
        QJsonObject rootObj;
        rootObj["start_time"] = start;
        rootObj["end_time"] = end;
        QJsonArray threatLevelsArray;
        for (int level : threatLevels) {
            threatLevelsArray.append(level);
        }
        rootObj["threat_levels"] = threatLevelsArray;
        QJsonObject sensorsObj;
        for (auto it = groupWithSensors.constBegin(); it != groupWithSensors.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 + "/logs/getLogsAmount",
                                                    QJsonDocument::fromJson(requestData).object());

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

    QList<LogsR> getLogs(const QMap<QString, QList<QString>> &groupWithSensors,
                        const QList<int> &threatLevels,
                        bool cmp,
                        qint64 start,
                        qint64 end,
                        int pageNum,
                        int pageSize)
    {
        QJsonObject rootObj;
        QJsonArray threatLevelsArray;
        for (int level : threatLevels) {
            threatLevelsArray.append(level);
        }
        rootObj["threat_levels"] = threatLevelsArray;
        rootObj["start_time"] = start;
        rootObj["end_time"] = end;
        rootObj["page_number"] = pageNum;
        rootObj["page_size"] = pageSize;
        QJsonObject sensorsObj;
        for (auto it = groupWithSensors.constBegin(); it != groupWithSensors.constEnd(); ++it) {
            QJsonArray sensorArray;
            for (QString sensorId : it.value()) {
                sensorArray.append(sensorId);
            }
            sensorsObj[it.key()] = sensorArray;
        }
        rootObj["sensors"] = sensorsObj;
        QJsonArray orderArray;
        QJsonArray orderItem;
        cmp ? orderItem.append("threat_level") : orderItem.append("date");
        orderItem.append("-");
        orderArray.append(orderItem);
        rootObj["order"] = orderArray;
        QJsonDocument doc(rootObj);
        QByteArray requestData = doc.toJson();

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

        QList<LogsR> logsList;

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

            for (const QJsonValue &logValue : logsArray) {
                if (logValue.isObject()) {
                    LogsR log;
                    log.parseResponse(logValue.toObject());
                    logsList.append(log);
                }
            }
        }

        return logsList;
    }

private:
    HttpClient *httpClient;
};

#endif // JOURNALSERVICE_H
