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

Alarmlight::Alarmlight()
{
    m_mpDeviceLastCommand.clear();
    m_mpRegulation.clear();
    m_pDataConsumer = nullptr;
}

bool Alarmlight::isInheritedFrom(std::string tp)
{
    bool bEqual = (strcasecmp(tp.c_str(), "BaseModule") == 0);
    return bEqual;
}

void Alarmlight::regConsumer(DataConsumer* pDC)
{
    m_pDataConsumer = pDC;
}

void Alarmlight::Setup(ModuleInfo moduleInfo)
{
    // 把模块里的关键信息读出来,例如怎么干活，什么条件让哪个设备干嘛？
    std::map<QString,QVariant> vSettings;
    for(std::vector<Setting>::iterator it = moduleInfo.vSettings.begin();it != moduleInfo.vSettings.end();it++)
    {
        vSettings[(*it).Name.c_str()] = (*it).qValue;
    }
    if(vSettings.find("num") != vSettings.end())
    {
        int number = vSettings["num"].toInt();
        for(int i = 1;i <= number;i++)
        {
            oneregulation one;
            if(vSettings.find(QString("object_%1").arg(i)) == vSettings.end())
                continue;
            one.sourceobject = vSettings[QString("object_%1").arg(i)].toString();

            if(vSettings.find(QString("operator_%1").arg(i)) == vSettings.end())
                continue;
            one.useoperator = vSettings[QString("operator_%1").arg(i)].toString();

            if(vSettings.find(QString("operator1_%1").arg(i)) == vSettings.end())
                continue;
            one.operator1 = vSettings[QString("operator1_%1").arg(i)].toDouble();

            if(vSettings.find(QString("operator2_%1").arg(i)) == vSettings.end())
                continue;
            one.operator2 = vSettings[QString("operator2_%1").arg(i)].toDouble();

            if(vSettings.find(QString("device_%1").arg(i)) == vSettings.end())
                continue;
            one.targetdevice = vSettings[QString("device_%1").arg(i)].toString();

            if(vSettings.find(QString("devicecommand_%1").arg(i)) == vSettings.end())
                continue;
            one.targetdevicecommand = vSettings[QString("devicecommand_%1").arg(i)].toString();

            this->m_mpRegulation.insert(std::pair<std::string,oneregulation>(one.sourceobject.toStdString(),one));
        }
    }
}

void Alarmlight::OnSubData(std::string name,std::string val)
{
    QByteArray byteArray(val.c_str(), val.length());
    QJsonParseError err;
    QJsonDocument jsonDoc(QJsonDocument::fromJson(byteArray,&err));
    QJsonObject jsonObject = jsonDoc.object();
    QStringList keys = jsonObject.keys();

    for(int i = 0;i < keys.size();i++)
    {
        QString realkey = QString("%1.%2").arg(name.c_str()).arg(keys.at(i));
        double realvalue = jsonObject[keys.at(i)].toDouble();

        // 传入数据,判断数据,用m_pDataConsumer的ondata输出设
        std::multimap<std::string,oneregulation>::iterator it = this->m_mpRegulation.find(realkey.toStdString());
        if(it != this->m_mpRegulation.end())
        {
            for(; it != this->m_mpRegulation.end(); it++)
            {
                if((*it).first == realkey.toStdString())
                {
                    oneregulation one = (*it).second;
                    //double value = std::stod(val);
                    bool send = false;
                    if(one.useoperator == "beyond")
                    {
                        if(realvalue > one.operator1)
                        {
                            send = true;
                        }
                    }
                    else if(one.useoperator == "below")
                    {
                        if(realvalue < one.operator1)
                        {
                            send = true;
                        }
                    }
                    else if(one.useoperator == "range")
                    {
                        if(realvalue >= one.operator1 && realvalue <= one.operator2)
                        {
                            send = true;
                        }
                    }
                    if(send == true)
                    {
                        if(this->m_mpDeviceLastCommand.find(one.targetdevice) != this->m_mpDeviceLastCommand.end())
                        {
                            if(this->m_mpDeviceLastCommand[one.targetdevice] == one.targetdevicecommand)
                                send = false;
                        }
                        if(send == true)
                        {
                            m_pDataConsumer->OnData(one.targetdevice.toStdString(),QVariant(one.targetdevicecommand));
                            this->m_mpDeviceLastCommand[one.targetdevice] = one.targetdevicecommand;
                        }
                    }
                }
                else
                {
                    break;
                }
            }
        }
    }
}

BaseModule* instance()
{
    return new Alarmlight();
}

void destroy(BaseModule* pInstance)
{
    if( pInstance )
    {
        delete pInstance;
    }
}
