#include "DataManagerProxy.h"
#include "RedisClient.h"
#include <QJsonArray>
#include <QJsonObject>
DataManagerProxy::DataManagerProxy() {

}

QList<DataManagerInfo> DataManagerProxy::loadModuleInfos(const Config& config)
{
    RedisClient redis;
    QList<DataManagerInfo > listDataManageInfo;
    QString key = QString("%1:%2").arg(config.AppName, config.AppId);
    DeviceInfo di;

    redis.Setup(config.redisCfg.host, config.redisCfg.port, config.redisCfg.au);
    // QHash<QString, QString> h = redis->hgetall(key);
    // for(auto it = h.begin(); it != h.end(); it++)
    // {
    //     qDebug()<< "key" << it.key();
    //     //qDebug()<< "val" << it.value();
    //     QJsonArray ja = QJsonDocument::fromJson(it.value().toUtf8()).array();
    //     // QJsonObject jo = QJsonDocument::fromJson(it.value().toUtf8()).object();
    // }

    QStringList lst = redis.hvals(key);
    foreach (QString str, lst)
    {
        if( str.isEmpty() )
        {
            continue;
        }
        QJsonParseError jsonParseError;
        QJsonDocument jsonDocument(QJsonDocument::fromJson(str.toUtf8(), &jsonParseError));
        if(QJsonParseError::NoError != jsonParseError.error)
        {
            //LOGERROR("parse json file {} error", fullpath.toStdString().c_str());
            continue;
        }
        if(!jsonDocument.isArray()){
            continue;
        }
        QJsonArray ja = jsonDocument.array();
        foreach (auto var, ja) {
            QJsonObject item = var.toObject();
            QString topic = item["topic"].toString();
            QJsonArray clients = item["Client"].toArray();
            DataManagerInfo dataManageInfo;
            ModuleInfo mi;
            mi.Name = "TDengineSubscriber";
            mi.AssemblyName = "plugins/TDengineSubscriber.dll";
            // mi.ClassName = "TDengineSubscriber";
            mi.Code = topic.toStdString();
            mi.Topic = topic.toStdString();
            foreach (auto client, clients) {
                ConsumerInfo ci;
                ci.Name = client["Name"].toString().toStdString();
                ci.AssemblyName = client["AssemblyName"].toString().toStdString();
                //ci.ClassName = "Alarm";
                ci.SubscribeName = topic.toStdString();
                ci.Settings = client["Settings"].toVariant().toString().toStdString();
                dataManageInfo.consumers.push_back(ci);
            }
            dataManageInfo.modules.push_back(mi);
            listDataManageInfo.push_back(dataManageInfo);
        }
    }

#if 0

            ModuleInfo mi;
            mi.Name = "TDengineSubscriber";
            mi.AssemblyName = "plugins/TDengineSubscriber.dll";
            mi.ClassName = "TDengineSubscriber";
            mi.Code = "topic:xxx";

            ConsumerInfo ci;
            ci.Name = "Alarm";
            ci.AssemblyName = "plugins/Alarm.dll";
            ci.ClassName = "Alarm";
            ci.SubscribeName = "TDengineSubscriber";
            DataManagerInfo dataManageInfo;
            dataManageInfo.consumers.push_back(ci);
            dataManageInfo.modules.push_back(mi);
            listDataManageInfo.push_back(dataManageInfo);
#endif
    //return di;

    return listDataManageInfo;
}
