#include "HttpGetUserInfo.h"

#include <QJsonObject>

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

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

}

HttpGetUserInfo::~HttpGetUserInfo()
{

}

void HttpGetUserInfo::run()
{
    httpRequestGetUserInfo();
}

void HttpGetUserInfo::cleanUserInfoModel()
{

}

void HttpGetUserInfo::httpRequestGetUserInfo()
{
    QJsonObject jsonRoot;
    QDateTime currentDateTime = QDateTime::currentDateTime();
    qint64 timestampSeconds = currentDateTime.toMSecsSinceEpoch();

    QString url = Config()->userInfoUrl;

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

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

void HttpGetUserInfo::slotHttpResponseGetUserInfo(QByteArray data)
{
    QJsonParseError error;
    QJsonDocument jsonDoc = QJsonDocument::fromJson(data, &error);
    if (error.error != QJsonParseError::NoError) {
        emit signalUserInfoReturnStat(-3, "无法查询到用户信息");
        return;
    }
    if (jsonDoc.isNull() || jsonDoc.isEmpty()) {
        emit signalUserInfoReturnStat(-3, "无法查询到用户信息");
        return;
    }

    QJsonObject rootObj = jsonDoc.object();
    if(rootObj.contains("code"))
    {
        int codeValue = rootObj.value("code").toInt();
        if (codeValue == 408) {
            emit signalUserInfoReturnStat(408, "处理超时，请稍后重试。");
            return;
        }
        if(codeValue == 200 || codeValue == 0) {
            if(rootObj.contains("data")) {
                QJsonObject vDataObj = rootObj.value("data").toObject();
                if (vDataObj.empty()) {
                    return;
                }

                if (vDataObj.contains("id")) {
                    UserInfoModel::instance()->setId(vDataObj.value("id").toInt());
                }
                if (vDataObj.contains("username")) {
                    UserInfoModel::instance()->setUsername(vDataObj.value("username").toString());
                }
                if (vDataObj.contains("nickname")) {
                    UserInfoModel::instance()->setNickname(vDataObj.value("nickname").toString());
                }
                if (vDataObj.contains("email")) {
                    UserInfoModel::instance()->setEmail(vDataObj.value("email").toString());
                }
                if (vDataObj.contains("mobile")) {
                    UserInfoModel::instance()->setMobile(vDataObj.value("mobile").toString());
                }
                if (vDataObj.contains("sex")) {
                    UserInfoModel::instance()->setSex(vDataObj.value("sex").toInt());
                }
                if (vDataObj.contains("avatar")) {
                    UserInfoModel::instance()->setAvatar(vDataObj.value("avatar").toString());
                }
                if (vDataObj.contains("loginIp")) {
                    UserInfoModel::instance()->setLoginIp(vDataObj.value("loginIp").toString());
                }
                if (vDataObj.contains("loginDate")) {
                    qint64 loginDate = vDataObj.value("loginDate").toDouble();
                    if (loginDate > 0) {
                        QDateTime dateTime = QDateTime::fromMSecsSinceEpoch(loginDate);
                        UserInfoModel::instance()->setLoginDate(dateTime.toString("yyyy-MM-dd HH:mm:ss"));
                    }
                }
                if (vDataObj.contains("createTime")) {
                    qint64 createTime = vDataObj.value("createTime").toDouble();
                    if (createTime > 0) {
                        QDateTime dateTime = QDateTime::fromMSecsSinceEpoch(createTime);
                        UserInfoModel::instance()->setCreateTime(dateTime.toString("yyyy-MM-dd HH:mm:ss"));
                    }
                }
                if (vDataObj.contains("roles")) {
                    QJsonObject rolesJsonObj;
                    rolesJsonObj["roles"] = vDataObj.value("roles");
                    QJsonDocument rolesDoc(rolesJsonObj);
                    UserInfoModel::instance()->setRoles(rolesDoc.toJson(QJsonDocument::Compact));
                }
                if (vDataObj.contains("dept")) {
                    QJsonObject deptJsonObj;
                    deptJsonObj["dept"] = vDataObj.value("dept");
                    QJsonDocument deptDoc(deptJsonObj);
                    UserInfoModel::instance()->setDept(deptDoc.toJson(QJsonDocument::Compact));
                }
                if (vDataObj.contains("posts")) {
                    QJsonObject postsJsonObj;
                    postsJsonObj["posts"] = vDataObj.value("posts");
                    QJsonDocument postsDoc(postsJsonObj);
                    UserInfoModel::instance()->setPosts(postsDoc.toJson(QJsonDocument::Compact));
                }
                emit signalUserInfoReturnStat(0, "已查询到用户信息");
                return;
            }
        }
    }
    emit signalUserInfoReturnStat(-3, "无法查询到用户信息");
    return;
}
