#include "HttpClient.h"
#include <QNetworkReply>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <QDebug>

// HttpClient 类的实现

HttpClient::HttpClient(QObject *parent)
    : QObject(parent), m_networkManager(new QNetworkAccessManager(this)) {
    // 构造函数中可以初始化一些变量或设置默认值
}

void HttpClient::sendHttpGetRequest(const QUrl &url) {
    QNetworkRequest request(url);
    m_currenturl = url;
    QNetworkReply *reply = m_networkManager->get(request);
    if (!reply) {
        emit httpErrorOccurred("Failed to create network reply");
    } else {
        // qDebug() << "Network reply created successfully.";
    }

    connect(reply, &QNetworkReply::finished, this, &HttpClient::onGetFinished);
    // connect(reply, &QNetworkReply::errorOccurred, this, &HttpClient::onNetworkError);
    // connect(reply, &QNetworkReply::sslErrors, this, &HttpClient::onSslErrors);
}

void HttpClient::onGetFinished() {
    QNetworkReply *reply = qobject_cast<QNetworkReply *>(sender());
    if (reply) {
        if (reply->error() == QNetworkReply::NoError) {
            QByteArray responseData = reply->readAll();
            QJsonDocument jsonDoc = QJsonDocument::fromJson(responseData);
            if (jsonDoc.isNull()) {
                qDebug() << "Failed to parse JSON!";
                return;
            }
            else {
                emit httpGetFinished(jsonDoc,m_currenturl); // 发出信号通知外部处理
            }

        } else {
            emit httpErrorOccurred(reply->errorString()); // 发出信号通知外部错误
        }
        // 确保释放 reply 对象
        reply->deleteLater();
    }
}

void HttpClient::sendHttpGetAddress(const QUrl &url) {
    QNetworkRequest request(url);
    m_currenturl2 = url;
    QNetworkReply *reply = m_networkManager->get(request);
    if (!reply) {
        emit httpErrorOccurred("Failed to create network reply");
    } else {
        // qDebug() << "Network reply created successfully.";
    }

    connect(reply, &QNetworkReply::finished, this, &HttpClient::onGetAddress);

    // connect(reply, &QNetworkReply::errorOccurred, this, &HttpClient::onNetworkError);
    // connect(reply, &QNetworkReply::sslErrors, this, &HttpClient::onSslErrors);
}

void HttpClient::onGetAddress() {
    QNetworkReply *reply = qobject_cast<QNetworkReply *>(sender());
    if (reply) {
        if (reply->error() == QNetworkReply::NoError) {

            QByteArray responseData = reply->readAll();
            QJsonDocument jsonDoc = QJsonDocument::fromJson(responseData);
            if (jsonDoc.isNull()) {
                return;
            }
            else {
                // 可以进一步处理响应数据，比如解析 JSON 或 XML
                emit httpGetFinished(jsonDoc,m_currenturl2); // 发出信号通知外部处理
            }

        } else {
            // 处理请求错误
            // qDebug() << "GET Error:" << reply->errorString();
            emit httpErrorOccurred(reply->errorString()); // 发出信号通知外部错误
        }

        reply->deleteLater();
    }
}

void HttpClient::onNetworkError(QNetworkReply::NetworkError error) {
    QNetworkReply *reply = qobject_cast<QNetworkReply *>(sender());
    if (reply) {
        emit httpErrorOccurred(reply->errorString());
    }
}

void HttpClient::onSslErrors(const QList<QSslError> &errors) {
    QNetworkReply *reply = qobject_cast<QNetworkReply *>(sender());
    if (reply) {
        QString errorString;
        for (const QSslError &error : errors) {
            if (!errorString.isEmpty()) {
                errorString += ", ";
            }
            errorString += error.errorString();
        }
        emit httpErrorOccurred(errorString);

        // reply->ignoreSslErrors();
    }
}
