#ifndef HTTPCLIENT_H
#define HTTPCLIENT_H

#include <QThread>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QByteArray>
#include <QString>
#include <QMutex>
#include <QDateTime>
#include <QUrl>
#include <QNetworkRequest>

// HTTP请求工作状态枚举（Qt5.15推荐enum class，支持）
enum class httpWorkStat {
    httpWorkSleep,
    httpWorkJson,
    httpWorkFormdata,
    httpWorkGet
};

class HttpClient : public QThread
{
    Q_OBJECT // Qt5.15必须加，否则信号槽报错
public:
    explicit HttpClient(QObject *parent = nullptr);
    ~HttpClient() override; // Qt5.15 override关键字需显式写

    static QString sToken; // 全局静态Token
    /** 清空静态 sToken（与 InteractiveData::clearToken 配合使用） */
    static void clearToken();

    void slotSetThreadStop();

    // 供 QML 调用的模拟还锁请求（参数为字符串，避免 QByteArray 类型转换）
    Q_INVOKABLE void simulateUpdateBackLock(const QString& postUrl, const QString& jsonBody, const QString& token);

signals:
    // 保留你所有的业务信号，Qt5.15完美支持
    void signalResponsePasswordLoginData(QByteArray);
    void signalResponseCardLoginData(QByteArray);
    void signalResponseUpdateNodeApprovalUrl(QByteArray);
    void signalResponseUploadJobTicket(QByteArray);
    void signalResponseUpdateWorkTicket(QByteArray);
    void signalResponseUploadPositionInfo(QByteArray);
    void signalResponseUpdateUncolock(QByteArray);
    void signalResponseUpdateColock(QByteArray);
    void signalResponseUpdatePointUnlock(QByteArray);
    void signalResponseUpdateBackLock(QByteArray);
    void signalResponseUpdateKeyBack(QByteArray);
    void signalResponseGetJobTickets(QByteArray);
    void signalResponseGetWorkNodeDetail(QByteArray);
    void signalResponseGetFormById(QByteArray);
    void signalResponseGetUserInfoUrl(QByteArray);
    void signalResponseGetKeyMAC(QByteArray);
    void signalResponseGetIsolationPointInfo(QByteArray);
    void signalReponseUpdateUserInfoUrl(QByteArray);
    void signalReponseUpdateUserPasswordUrl(QByteArray);

    void signalResponseGetWorkTicketByNodeId(QByteArray);
public slots:
    void slotPostRequestData(quint64 id, QString postUrl, QByteArray data, QByteArray file, QString token);
    void slotGetRequestData(quint64 id, QString postUrl, QByteArray data, QString token);
    void slotPutRequestData(quint64 id, QString postUrl, QByteArray data, QByteArray file, QString token);

private:
    // 核心请求方法（全部声明，无缺失）
    QString postJsonRequest();
    QString postFormdataRequest();
    QString getRequest();
    QString putJsonRequest();

    // 通用工具方法（Qt5.15实现无压力）
    QString sendRequest(const QNetworkRequest& request, QNetworkAccessManager::Operation op, const QByteArray& data = QByteArray());
    QString formatErrorResponse(int statusCode, const QString& errorInfo);
    QUrl buildNormalizedUrl(const QString& host, const QString& path, const QString& query = "");

    // 线程运行函数
    void run() override;

    // 线程安全变量（Qt5.15 volatile关键字支持）
    volatile bool m_threadstatus = true;
    volatile httpWorkStat m_workStat = httpWorkStat::httpWorkSleep;
    QMutex m_mutex;
    quint64 m_id = 0;
    QString m_postUrl;
    QByteArray m_httpData;
    QByteArray m_httpFile;
    QString m_token;

    // 网络核心对象：主线程用 m_pHttpMgr；run() 工作线程用 m_pHttpMgrWorker，避免跨线程创建子对象
    QNetworkAccessManager* m_pHttpMgr = nullptr;
    QNetworkAccessManager* m_pHttpMgrWorker = nullptr;

    static const int HTTP_REQUEST_TIMEOUT;
};

#endif // HTTPCLIENT_H
