#include "httprequest.h"
#include <QUrl>
#include <QJsonParseError>
#include <QNetworkReply>
#include <pqQtlib/transport/pqhttpformdata.h>


HttpRequest::HttpRequest(QObject *parent) : QObject(parent)
{
    connect(&_manager, &QNetworkAccessManager::finished, this, &HttpRequest::finishRequest);
}


void HttpRequest::get(const QString & url)
{
    QNetworkRequest req;
    req.setUrl(QUrl(url));
    initRequest(req);
    _manager.get(req);
}

void HttpRequest::post(const QString & url, const QJsonObject & body)
{
     QNetworkRequest req;
     req.setUrl(url);
     initRequest(req, 1);
     req.setHeader(QNetworkRequest::ContentTypeHeader,"application/json");
     _manager.post(req, QJsonDocument(body).toJson());
}

void HttpRequest::post(const QString & url, const QMap<QString, QString> &body)
{
    QNetworkRequest req;
    req.setUrl(QUrl(url));
    initRequest(req, 1);
    PQ::PQHttpFormData * dt = new PQ::PQHttpFormData();
    dt->set(body);
    dt->postThis(&_manager,req);
}

void HttpRequest::finishRequest(QNetworkReply * reply)
{
    if (reply->error()){
        //qDebug()<<"Error!"<<endl;
        emit result(500, QJsonObject());
    }else{
        QByteArray byte_array = reply->readAll();
        QJsonParseError json_error;
        QJsonDocument parse_document = QJsonDocument::fromJson(byte_array, &json_error);
        if(json_error.error == QJsonParseError::NoError)
        {
            if(parse_document.isObject())
            {
                emit result(200, parse_document.object());
                reply->deleteLater();
                return;
            }
        }
        emit result(200, QJsonObject());
    }
    reply->deleteLater();
}

QNetworkRequest HttpRequest::request(const QString & url)
{
    QNetworkRequest req;
    req.setUrl(url);
    initRequest(req, 1);
    return req;
}

void HttpRequest::initRequest(QNetworkRequest & req, bool /*post*/)
{
    if(!_aid.isEmpty()){
        req.setRawHeader("Auid",_aid.toUtf8());
        req.setRawHeader("Access-Token",_token.toUtf8());
    }
//    if(post)
//    {
//        req.setHeader(QNetworkRequest::ContentTypeHeader,"multipart/form-data");
//    }
}

void HttpRequest::initMultiPart(QHttpMultiPart & part,const QMap<QString,QString> body)
{
    for(auto begin = body.begin(); begin != body.end(); ++begin){
        QHttpPart textPart;
        textPart.setHeader(QNetworkRequest::ContentDispositionHeader, QString("form-data; name=\"%1\"").arg(begin.key()));
        textPart.setBody(begin.value().toUtf8());
        part.append(textPart);
    }
}

//void HttpRequest::initMultiPart(QHttpMultiPart & part,const QMap<QString,QVariant> body)
//{
//    for(auto begin = body.begin(); begin != body.end(); ++begin){
//        QHttpPart textPart;
//        textPart.setHeader(QNetworkRequest::ContentDispositionHeader, QString("form-data; name=\"%1\"").arg(begin.key()));
//        if(begin.value().typeName()=="QString")
//        {
//            textPart.setBody(begin.value().toString().toUtf8());
//        }
//        else
//        {
//            textPart.setBody(begin.value().toByteArray());
//        }
//        part.append(textPart);
//    }
//}
