#include "Config.h"
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <QFile>


Config::Config(const QString &filename)
{
    // QMap<QString, QString> configMap;

    //QString fullpath = QCoreApplication::applicationDirPath() + "/config.json";
    QFile file(filename);
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
        //LOGERROR("open {} error", filename.toStdString().c_str());
        return;
    }
    QByteArray array = file.readAll();
    file.close();
    QJsonParseError jsonParseError;
    QJsonDocument jsonDocument(QJsonDocument::fromJson(array, &jsonParseError));
    if(QJsonParseError::NoError != jsonParseError.error)
    {
        //LOGERROR("parse json file {} error", fullpath.toStdString().c_str());
        return;
    }
    if(!jsonDocument.isObject()){
        return;
    }
    QJsonObject obj = jsonDocument.object();
    QString appName = obj["AppName"].toString();
    QString appId = obj["AppId"].toString();
    QJsonObject redis = obj["redis"].toObject();
    redisCfg.host = redis["host"].toString();
    redisCfg.port = redis["port"].toInt();
    redisCfg.au = redis["au"].toString();
}
