#include "readmodulefromredis.h"
#include <QJsonArray>
#include <QJsonObject>
#include <QJsonDocument>
#include <QtCore/QTextCodec>
#include <fstream>

readmodulefromredis::readmodulefromredis(RedisAgent* redis)
{
    m_pRedis = redis;
}

bool readmodulefromredis::parse(QString cfg,ModuleInfo& moduleInfo)
{
    QTextCodec *utf8 = QTextCodec::codecForName("UTF-8");
    QTextCodec::setCodecForLocale(utf8);

    QJsonParseError err;
    QJsonDocument jsonDoc(QJsonDocument::fromJson(cfg.toLocal8Bit(),&err));
    QJsonObject jsonObject = jsonDoc.object();
    QStringList keys = jsonObject.keys();

    foreach (QString key, keys)
    {
        if( key.compare("name",Qt::CaseInsensitive) == 0)
        {
            moduleInfo.Name = jsonObject["name"].toString().toLocal8Bit().toStdString();
        }
        else if( key.compare("code",Qt::CaseInsensitive) == 0)
        {
            moduleInfo.Code = jsonObject["code"].toString().toLocal8Bit().toStdString();
        }
        else if( key.compare("type",Qt::CaseInsensitive) == 0)
        {
            QString type = jsonObject["type"].toString();
            moduleInfo.Type = control;
        }
        else if( key.compare("model",Qt::CaseInsensitive) == 0)
        {
            moduleInfo.AssemblyName = jsonObject["model"].toString().toLocal8Bit().toStdString();
        }
        else if( key.compare("serverId",Qt::CaseInsensitive) == 0)
        {
            moduleInfo.serverId = jsonObject["serverId"].toInt();
        }
        else if( key.compare("settings",Qt::CaseInsensitive) == 0)
        {
            QJsonObject jsonSettings = jsonObject["settings"].toObject();
            QStringList lst = jsonSettings.keys();
            foreach (QString item, lst)
            {
                Setting oSetting;
                oSetting.Name = item.toLocal8Bit().toStdString();
                QJsonValue::Type t = jsonSettings[item].type();
                switch( t )
                {
                case QJsonValue::Bool:
                    oSetting.qValue = jsonSettings[item].toBool();
                    break;
                case QJsonValue::Double:
                    oSetting.qValue = jsonSettings[item].toDouble();
                    break;
                case QJsonValue::String:
                    oSetting.qValue = jsonSettings[item].toString();
                    break;
                }
                moduleInfo.vSettings.push_back(oSetting);
            }
        }
        else if( key.compare("inputs",Qt::CaseInsensitive) == 0)
        {
            if( jsonObject["inputs"].isArray() )
            {
                QJsonArray arr = jsonObject["inputs"].toArray();
                for(auto i : arr)
                {
                    if( i.type() == QJsonValue::String )
                    {
                        moduleInfo.lstInputs.push_back(i.toString().toLocal8Bit().toStdString());
                    }
                }
            }
        }
    }
    return true;
}

std::list<ModuleInfo> readmodulefromredis::loadModules(QString id)
{
    std::list<ModuleInfo> lstModules;

    if( m_pRedis != nullptr)
    {
        QStringList lst = m_pRedis->hvals("intelligentcontrol");
        foreach (QString str, lst) // 我就不取hkey了,那个就是用编号区分一下
        {
            if( str.isEmpty() )
                continue;

            ModuleInfo moduleInfo;
            if( parse(str,moduleInfo) )
            {
                // 判断一下是否这个服务器管理的实例,如果是的话加入
                if(moduleInfo.serverId == id.toInt())
                    lstModules.push_back(moduleInfo);
            }
        }
    }
    return lstModules;
}
