#include "HttpPasswordLogin.h"

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

#include <QJsonObject>

HttpPasswordLogin::HttpPasswordLogin(QObject *parent)
    : QThread(parent)
{
}

void HttpPasswordLogin::run()
{
    httpRequestUsernameLogin();

    //qDebug() << "HttpPasswordLogin thread exit!";
}

void HttpPasswordLogin::httpRequestUsernameLogin()
{
    QJsonObject jsonRoot;
    QDateTime currentDateTime = QDateTime::currentDateTime();
    qint64 timestampSeconds = currentDateTime.toMSecsSinceEpoch();

    QString url = Config()->usernameLogin_url;
    jsonRoot.insert("username", this->m_username);
    jsonRoot.insert("password", this->m_password);

    QJsonDocument jsonDoc(jsonRoot);
    QByteArray jsonData = jsonDoc.toJson(QJsonDocument::Compact);
    qDebug() << "json=" << QString::fromUtf8(jsonData);

    emit signalPostRequestData(timestampSeconds, url, jsonData, NULL, m_accessToken);
}

void HttpPasswordLogin::slotHttpResponseUsernameLogin(QByteArray data)
{
    QJsonParseError error;
    QJsonDocument jsonDoc = QJsonDocument::fromJson(data, &error);
    if (error.error != QJsonParseError::NoError) {
        GetInteractiveData()->clearToken();
        emit signalLoginReturnStat(-3, "登录失败");
        return;
    }
    if(jsonDoc.isNull() || jsonDoc.isEmpty()) {
        GetInteractiveData()->clearToken();
        emit signalLoginReturnStat(-3, "登录失败");
        return;
    }

    QJsonObject rootObj = jsonDoc.object();
    if(rootObj.contains("code"))
    {
        int codeValue = rootObj.value("code").toInt();
        if (codeValue == 408) {
            emit signalLoginReturnStat(408, "处理超时，请稍后重试。");
            return;
        }
        if(codeValue == 200 || codeValue == 0)
        {
            if(rootObj.contains("data"))
            {
                QJsonObject vDataObj = rootObj.value("data").toObject();
                if (vDataObj.empty()) {
                    GetInteractiveData()->clearToken();
                    emit signalLoginReturnStat(-3, "登录失败");
                    return;
                }
                if (!vDataObj.contains("accessToken")) {
                    GetInteractiveData()->clearToken();
                    emit signalLoginReturnStat(-3, "登录失败");
                    return;
                }
                QJsonValue value = vDataObj.value("accessToken");
                if(value.type() == QJsonValue::String)
                {
                    GetInteractiveData()->m_token = value.toString();
                    InteractiveHttp::strToken = value.toString();
                    m_accessToken = value.toString();
                    if (vDataObj.contains("refreshToken") && vDataObj.value("refreshToken").type() == QJsonValue::String) {
                        m_refreshToken = vDataObj.value("refreshToken").toString();
                    }
                    QString username = "未知用户";
                    if(vDataObj.contains("nickname"))
                    {
                        QJsonValue name = vDataObj.value("nickname");
                        if(name.type() == QJsonValue::String)
                        {
                            username = name.toString();
                        }
                    }
                    Config()->username = username;
                    GetInteractiveData()->m_userName = username;
                    emit signalLoginReturnStat(0, "登录成功");
                    emit signalLoginReturnParam(username, 0);
                }
                else {
                    GetInteractiveData()->clearToken();
                    emit signalLoginReturnStat(-3, "登录失败");
                }
            }
        }
        else{
            GetInteractiveData()->clearToken();
            emit signalLoginReturnStat(-1, "用户名或者密码错误");
        }
    }
}
