#include "networkagent.h"

#include <QDebug>
#include "legalizationagent.h"

class NetworkAgentPrivate
{
public:
    NetworkAgentPrivate(NetworkAgent *parent) : q_ptr(parent) {}

    CommonSystemDetector *detector = nullptr;
    LegalizationAgent *legalization = nullptr;

    NetworkAgent *q_ptr;
    Q_DECLARE_PUBLIC(NetworkAgent)
};

NetworkAgent::NetworkAgent() : dd_ptr(new NetworkAgentPrivate(this))
{

}

NetworkAgent::~NetworkAgent()
{

}

/*
X-QA-MAC-Address: fc:aa:14:3a:d7:a4
X-QA-Name: wyl
X-Client-Version: 1.0.2
X-Client-Platform: windows
*/
QNetworkRequest NetworkAgent::createRequest(const QUrl &url) const
{
    Q_D(const NetworkAgent);
    QNetworkRequest request(url);
    request.setRawHeader("X-QA-MAC-Address", d->detector->macAddress().toLatin1());
    request.setRawHeader("X-QA-Name", qgetenv("DLC_QA_NAME"));
    request.setRawHeader("X-Client-Version", d->detector->version().toLatin1());
    request.setRawHeader("X-Client-Platform", d->detector->platform().toLatin1());
    request.setRawHeader("X-Client-Areacode", d->legalization->QueryAreacode().toLatin1());
    request.setRawHeader("X-Client-Orgcode", d->legalization->QueryOrgcode().toLatin1());
    return request;
}

curl_slist *NetworkAgent::customCurlHeader() const
{
    Q_D(const NetworkAgent);
    struct curl_slist *chunk = nullptr;

    chunk = curl_slist_append(chunk, QString("X-QA-MAC-Address: %1").arg(d->detector->macAddress()).toStdString().c_str());
    chunk = curl_slist_append(chunk, QString("X-QA-Name: %1").arg(QString(qgetenv("DLC_QA_NAME"))).toStdString().c_str());
    chunk = curl_slist_append(chunk, QString("X-Client-Version: %1").arg(d->detector->version()).toStdString().c_str());
    chunk = curl_slist_append(chunk, QString("X-Client-Platform: %1").arg(d->detector->platform()).toStdString().c_str());
    chunk = curl_slist_append(chunk, QString("X-Client-Areacode: %1").arg(d->legalization->QueryAreacode()).toStdString().c_str());
    chunk = curl_slist_append(chunk, QString("X-Client-Orgcode: %1").arg(d->legalization->QueryOrgcode()).toStdString().c_str());

    return chunk;
}

QString NetworkAgent::clientPlatform() const
{
    Q_D(const NetworkAgent);
    return d->detector->platform();
}

void NetworkAgent::setSystemDetector(CommonSystemDetector *detector, LegalizationAgent *agent)
{
    Q_D(NetworkAgent);
    d->detector = detector;
    d->legalization = agent;
}
