#pragma once
#include <qhttpmultipart.h>
#include <QNetworkAccessManager>
#include <QObject>
#include <QMap>

class NetworkService final : public QObject {
    Q_OBJECT

public:
    explicit NetworkService(QObject *parent = nullptr);

    ~NetworkService() override;

    void setPayload(const QByteArray &json) { m_json = json; }

    void setDeviceData(const QString &desktopId, const QString &deviceId) {
        m_desktopId = desktopId;
        m_deviceId = deviceId;
    }

    void addExtra(const QString &key, const QVariant &value);

public slots:
    void postAccountsData();

    void getPayments();

    void insertTransactionData();

    void updateTransactionData();

    void start();
    void stop();

signals:
    void finished();

private:
    QByteArray m_json;
    QString m_deviceId;
    QString m_desktopId;
    QMap<QString, QVariant> m_extraData = {};
    bool m_running = true;

    QString m_urlAccountUpdate;
    QString m_urlPaymentsGet;
    QString m_urlTransactionUpdate;
    QString m_urlTransactionInsert;

    QNetworkAccessManager *m_manager;


    QJsonValue post(QHttpMultiPart *multiPart, const QString &path);

    QJsonArray get(const QString &path, const QMap<QString, QString> &params);
};
