#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";

    qDebug() << url.toString();

    // 请求对象
    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)
        {
            QString token = resultObj.find("data")->toString();
            this->token = token;
        }
    } else
    {
        resultObj.insert("code", -1);
    }

    resultObj.insert("code", 200); //

    qDebug() << resultObj;

    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();

        if (resultObj.find("code")->toInt() == 200)
        {
            ConstCache::getInstance().deviceTypes.clear();

            QJsonArray data = resultObj.find("data")->toArray();
            for (int i = 0; i < data.size(); i++)
            {
                QJsonObject item = data.at(i).toObject();

                ConstCache::getInstance().deviceTypes.insert(item.find("value")->toString(), item.find("name")->toString());
            }
        }
    } 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);
    }

//    qDebug() << resultObj;

    return resultObj;
}

QJsonObject HttpRequestController::getChannelList(QString deviceId)
{
    QJsonObject resultObj;

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

    QNetworkRequest request;
    request.setUrl(url);
    request.setRawHeader("Content-type", "application/json");
    request.setRawHeader("token", token.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);
    }

    qDebug() << resultObj;

    return resultObj;
}

QJsonObject HttpRequestController::updateChannelInfo(QJsonArray channelInfos)
{
    QJsonObject resultObj;

    // 获取设备列表的接口地址
    QUrl url = baseUrl + "/channel/qtBatchSave";

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

    QJsonDocument jsonDoc = QJsonDocument(channelInfos);
    QByteArray postData = jsonDoc.toJson(QJsonDocument::Compact);

    qDebug() << url;
    qDebug() << channelInfos;

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

    qDebug() << resultObj;

    return resultObj;
}

int HttpRequestController::getSysClockAlarmCount()
{
    QJsonObject resultObj;

    // 获取设备列表的接口地址
    QUrl url = baseUrl + "/alarm/alarmCount";

    QNetworkRequest request;
    request.setUrl(url);
    request.setRawHeader("Content-type", "application/json");
    request.setRawHeader("token", token.toLocal8Bit());
    request.setRawHeader("system", SettingConfig::getInstance().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();
        if (resultObj.value("code").toInt() != 200) {
            return 0;
        }
    } else
    {
        return 0;
    }

    qDebug() << resultObj;

    return resultObj.value("data").toInt();
}

QJsonObject HttpRequestController::getSysClockAlarmList(int limit)
{
    QJsonObject resultObj;

    // 获取设备列表的接口地址
    QUrl url = baseUrl + "/alarm/recordsPage";

    QNetworkRequest request;
    request.setRawHeader("Content-type", "application/json");
    request.setRawHeader("token", token.toLocal8Bit());
    request.setRawHeader("system", SettingConfig::getInstance().SYSTEM.toLocal8Bit());

    QUrlQuery query;
    query.addQueryItem("offset", "1");
    query.addQueryItem("limit", QString::number(limit));
    url.setQuery(query);

    request.setUrl(url);

    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);
    }

    qDebug() << resultObj;

    return resultObj;
}
