#ifndef HTTPCLIENT_H
#define HTTPCLIENT_H
#include <QObject>
#include <QThread>
#include <QDebug>
#include <QString>
#include <QMutex>

#define HTTP_REQUEST_TIMEOUT 1000 * 20

enum class httpWorkStat {
    httpWorkSleep,
    httpWorkJson,
    httpWorkFormdata,
    httpWorkGet
};

class httpClient : public QThread
{
    Q_OBJECT
public:
    explicit httpClient(QObject *parent = nullptr);

public slots:
    void slot_post_request_data(quint64 id, QString postUrl, QByteArray data, QByteArray file, QString token);
    void slot_get_request_data(quint64 id, QString postUrl, QByteArray data, QString token);
    void slot_set_thread_stop(void);

    // 全局函数
    static bool getRequest(const QString &url, const QString &token, const QByteArray &inData, QByteArray &outData);
    static bool postRequest(const QString &url, const QString &token, const QByteArray &inData, QByteArray &outData);

private:
    QString post_json_request(void);
    QString post_formdata_request(void);
    QString get_request(void);


    bool threadstatus = true;
    QMutex mutex;
    quint64 id;
    QByteArray httpData;
    QByteArray httpFile;
    QString postUrl = "";
    QString token = "";
    httpWorkStat workStat = httpWorkStat::httpWorkSleep;

public:
    static QString sToken;
signals:
    void signal_response_data(quint64 id, QString url, QByteArray res);

protected:
    void run() override;
};

#endif // HTTPCLIENT_H
