#include "HttpGetUserInfoById.h"

#include <QJsonObject>

#include "../usr/config.h"
#include "../interactive/InteractiveData.h"

HttpGetUserInfoById::HttpGetUserInfoById(QObject *parent)
    : QThread(parent)
{

}

HttpGetUserInfoById::~HttpGetUserInfoById()
{

}

void HttpGetUserInfoById::run()
{
    httpRequestGetUserInfoById();
}

void HttpGetUserInfoById::cleanUserInfoModel()
{

}

void HttpGetUserInfoById::httpRequestGetUserInfoById()
{
    QJsonObject jsonRoot;
    QDateTime currentDateTime = QDateTime::currentDateTime();
    qint64 timestampSeconds = currentDateTime.toMSecsSinceEpoch();

    QString url = Config()->userInfoByIdUrl;
    jsonRoot.insert("id", this->m_userId);

    QJsonDocument jsonDoc(jsonRoot);
    QByteArray jsonData = jsonDoc.toJson(QJsonDocument::Compact);

    emit signalGetRequestData(timestampSeconds, url, jsonData, GetInteractiveData()->m_token);
}

void HttpGetUserInfoById::slotHttpResponseGetUserInfoById(QByteArray data)
{
    QJsonParseError error;
    QJsonDocument jsonDoc = QJsonDocument::fromJson(data, &error);
    if (error.error != QJsonParseError::NoError) {
        emit signalUserInfoByIdReturnStat(-3, QVariantMap());
        return;
    }
    if (jsonDoc.isNull() || jsonDoc.isEmpty()) {
        emit signalUserInfoByIdReturnStat(-3, QVariantMap());
        return;
    }

    QJsonObject rootObj = jsonDoc.object();
    if(rootObj.contains("code"))
    {
        int codeValue = rootObj.value("code").toInt();
        if(codeValue == 200 || codeValue == 0) {
            if(rootObj.contains("data")) {
                QJsonObject vDataObj = rootObj.value("data").toObject();
                if (vDataObj.empty()) {
                    return;
                }
                QString avator;
                QString cardNfc;
//                if (vDataObj.contains("avatar")) {
//                    UserInfoModel::instance()->setAvatar(vDataObj.value("avatar").toString());
//                }
//                if (vDataObj.contains("cardNfc")) {
//                    UserInfoModel::instance()->setAvatar(vDataObj.value("avatar").toString());
//                }
//                emit signalUserInfoReturnStat(0, "已查询到用户信息");
                return;
            }
        }
    }
    emit signalUserInfoByIdReturnStat(-3, QVariantMap());
    return;
}
