#include "HttpRequestController.h"

HttpRequestController::HttpRequestController(QObject *parent) : QObject(parent)
{
    httpUtil = new HttpRequestUtil(this);
    baseUrl = SettingConfig::getInstance().getProperty("http", "baseUrl").toString();
    system = SettingConfig::getInstance().getProperty("client", "clientId").toString();
}

QJsonObject HttpRequestController::getTokenByClientId(QString clientId, QString key)
{
    QJsonObject resultObj;

    // 获取token的url地址
    QUrl url = baseUrl + "/getTokenByClientId";

    // 请求对象
    QNetworkRequest request;
    request.setUrl(url);
    request.setRawHeader("Content-type", "application/json");

    // 生成随机数作为salt
    qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
    float random = (qrand() % 10000) / (float)10000;
    QString salt = QByteUtil::binToHexString(QByteUtil::floatToBytes(random));
    QString clientSecret = clientId + "_" + key + "_" + salt;

    // MD5加密clientSecret
    char secretEn[CHAR_MD5_LEN];
    MD5::MD5Encode(clientSecret.toStdString().data(), clientSecret.length(), secretEn);
    QString secretMD5(secretEn);

    QJsonObject paramObj;
    paramObj.insert("clientId", clientId);
    paramObj.insert("salt", salt);
    paramObj.insert("clientSecret", secretMD5);
    QJsonDocument document;
    document.setObject(paramObj);
    QByteArray content = document.toJson(QJsonDocument::Compact);

    QNetworkReply * reply = httpUtil->sendPostRequest(request, content);

    const QByteArray reply_data = reply->readAll();
    QJsonDocument jsonDocument = QJsonDocument::fromJson(reply_data);
    if(jsonDocument.isNull() == false) {
        resultObj = jsonDocument.object();

        if (resultObj.find("code")->toInt() == 200)
        {
            this->token = resultObj.find("data")->toString();
        }
    } else
    {
        resultObj.insert("code", -1);
    }

    return resultObj;
}

QJsonObject HttpRequestController::initDictDeviceType()
{
    QJsonObject resultObj;

    // 获取字典值的接口地址
    QUrl url = baseUrl + "/sys/dict/code/deviceType";

    //
    QNetworkRequest request;
    request.setUrl(url);

    request.setRawHeader("Content-type", "application/json");
    request.setRawHeader("token", token.toLocal8Bit());

    QNetworkReply * reply = httpUtil->sendGetRequest(request);
    const QByteArray reply_data = reply->readAll();
    QJsonDocument jsonDocument = QJsonDocument::fromJson(reply_data);
    if(jsonDocument.isNull() == false)
    {
        resultObj = jsonDocument.object();
    } else
    {
        resultObj.insert("code", -1);
    }

    return resultObj;
}

QJsonObject HttpRequestController::initDeviceList(QString devType)
{
    QJsonObject resultObj;

    // 获取设备列表的接口地址
    QUrl url = baseUrl + "/device/list";
    QUrlQuery query;
    query.addQueryItem("type", devType);
    url.setQuery(query);

    QNetworkRequest request;
    request.setUrl(url);
    request.setRawHeader("Content-type", "application/json");
    request.setRawHeader("token", token.toLocal8Bit());
    request.setRawHeader("system", "");

    qDebug() << url;

    QNetworkReply * reply = httpUtil->sendGetRequest(request);
    const QByteArray reply_data = reply->readAll();
    QJsonDocument jsonDocument = QJsonDocument::fromJson(reply_data);
    if(jsonDocument.isNull() == false)
    {
        resultObj = jsonDocument.object();
    } else
    {
        resultObj.insert("code", -1);
    }

    return resultObj;
}

QJsonObject HttpRequestController::initDeviceList(QString devType, QString system)
{
    QJsonObject resultObj;

    // 获取设备列表的接口地址
    QUrl url = baseUrl + "/device/list";
    QUrlQuery query;
    query.addQueryItem("type", devType);
    url.setQuery(query);

    QNetworkRequest request;
    request.setUrl(url);
    request.setRawHeader("Content-type", "application/json");
    request.setRawHeader("token", token.toLocal8Bit());
    request.setRawHeader("system", system.toLocal8Bit());

    qDebug() << url;

    QNetworkReply * reply = httpUtil->sendGetRequest(request);
    const QByteArray reply_data = reply->readAll();
    QJsonDocument jsonDocument = QJsonDocument::fromJson(reply_data);
    if(jsonDocument.isNull() == false)
    {
        resultObj = jsonDocument.object();
    } else
    {
        resultObj.insert("code", -1);
    }

    return resultObj;
}
