#include "controlpowermanual.h"
#include "ui_controlpowermanual.h"
#include "LoUIClass/x_uimsgboxok.h"
#include <QJsonValue>
#include <QJsonObject>
#include <QJsonDocument>
#include <globaldefine.h>
ControlPowerManual::ControlPowerManual(QWidget *parent,QList<LedCard *> *list) :
    QWidget(parent),
    ui(new Ui::ControlPowerManual)
{
    ui->setupUi(this);
    refreshLable();
    ui->pushButtonReadback->setProperty("ssType", "progManageTool");
    ui->pushButtonReadback->setStyleSheet("QPushButton{background:rgba(28,154,210,1);}");
   ui->widget->setSliderColor(QColor(0, 0, 0),QColor(0, 160, 230));
    ui->widget->setBgColor(QColor(200,200,200),QColor(0,200,0));
    ui->widget->setTextColor(QColor(100,100,100),QColor(0, 160, 230));
    ui->widget->setText(tr("OFF"),tr("ON"));
    //按钮槽函数绑定
    connect(ui->widget, SIGNAL(checkedChanged(bool)), this, SLOT(onPowerChecked(bool)));
    connect(ui->pushButtonReadback,SIGNAL(clicked()),this,SLOT(OnReadback()));
    MARCO_DEFINE_CONTROL_FUNC2(parent->parent()->parent(),parent->parent())

}

ControlPowerManual::~ControlPowerManual()
{
    delete ui;
    if(pHpptClient != nullptr)
    {
        qDebug() <<"delete pHpptClient in ~NetComm";
        delete pHpptClient;
        pHpptClient=nullptr;
    }
    if(pHpptClientAll != nullptr)
    {
        qDebug() <<"delete pHpptClientAll in ~NetComm";
        delete pHpptClientAll;
        pHpptClientAll=nullptr;
    }
}
void ControlPowerManual::refreshLable()
{
    ui->labelPower->setText(tr("POWER"));
    ui->pushButtonReadback->setText(tr("Readback"));
}

void ControlPowerManual::OnReadback()
{
    if(m_pLedlist==nullptr)
        return;
    if(m_pLedlist->count()<=1)
    {
        if(m_pLedCard==nullptr||m_pLedlist->count()==0)
        {
            X_UIMsgBoxOk *pDlg=new X_UIMsgBoxOk(tr("Tip"),tr("NoSelectedController"),this,1);
            pDlg->exec();
            return;
        }
    }

    QJsonObject json;
    json.insert("_id", getRandomString(10));
    json.insert("_type", "IsScreenOn");
    MACRO_DEFINE_TIPDLG_FUCN(tr("IsScreenOn"),tr("Success"),tr("failed"))

}
void ControlPowerManual::onPowerChecked(bool b)
{
    if(m_pLedlist==nullptr)
        return;
    if(m_pLedlist->count()<=1)
    {
        if(m_pLedCard==nullptr||m_pLedlist->count()==0)
        {
            X_UIMsgBoxOk *pDlg=new X_UIMsgBoxOk(tr("Tip"),tr("NoSelectedController"),this,1);
            pDlg->exec();
            return;
        }
    }

    QJsonObject json;
    json.insert("_id", getRandomString(10));
    json.insert("_type", "SetScreenOn");
    json.insert("on", b);
    if(m_pLedlist->count()==1)
    {
        HttpPostByTypeJsonObject(pHpptClient,m_strUrl,json);
        if(m_PostingDlg==nullptr)
        {
            m_PostingDlg = new LoEmptyDialog(this);
            connect(m_PostingDlg,SIGNAL(sigClose()),this,SLOT(DeletePostingDlg()));
            connect(m_pGetAskTimer,SIGNAL(timeout()),m_PostingDlg,SLOT(TimerOutUnlock()));
            if(b)
                m_PostingDlg->lock(tr("SetScreenOn"),tr("Success"),tr("SetScreenOn")+tr("on")+tr("failed"));
            else
                m_PostingDlg->lock(tr("SetScreenOn"),tr("Success"),tr("SetScreenOff")+tr("off")+tr("failed"));
             m_pGetAskTimer->start(3000);
            m_PostingDlg->exec();

        }
    }
    else {
        if(b)
            emit sigSend(json,tr("SetScreenOn")+":"+tr("on"));
        else
            emit sigSend(json,tr("SetScreenOff")+":"+tr("off"));
    }
}
void ControlPowerManual::onSelectedDeviceList(QList<LedCard*> *list)
{
    m_pLedlist=list;
    if(m_pLedlist==nullptr)
        return;
    MACRO_DEFINE_CONTROL_FUNC3
}
//获取到httpPost的应答，如果参数变化需要出发修改控件item的内容
void ControlPowerManual::OnProHttpResponse(QString url, QString postMD5, QByteArray data)
{
    Q_UNUSED(postMD5)
    //qDebug()<< url + "\r\n"+m_strUrl;
    if(url == m_strUrl)
    {
        QJsonParseError parseJsonErr;
        QJsonDocument document = QJsonDocument::fromJson(data,&parseJsonErr);
        if(!(parseJsonErr.error == QJsonParseError::NoError))
        {
            if(data.size()==0)
                qDebug()<<"json is empty";
            else
                qDebug()<<tr("wDevicesItem OnProHttpResponse 解析json文件错误！");
            return;
        }
        QJsonObject jsonObject = document.object();
        QString strType=jsonObject["_type"].toString();
        if(jsonObject["success"].toBool()==true)
        {
            qDebug()<<m_pLedCard->m_strCardId +"---------"+ strType;
            if(strType == "SetScreenOn")
            {
                //emit sigOutputInfo(m_pLedCard->m_strCardId+":"+tr("receive")+"<-"+tr("SetBrightness")+":"+tr("success"));
            }
            else if(strType == "IsScreenOn"){

                ui->widget->setCheckedStatus(jsonObject["on"].toBool());

            }

            MACRO_ASKTIME_STOP
            MACRO_POSTING_DLG_UNLOCK

        }
    }
}
//获取到httpPost的应答，如果参数变化需要出发修改控件item的内容
void ControlPowerManual::OnProHttpResponseAll(QString url, QString postMD5, QByteArray data)
{
    Q_UNUSED(postMD5)
    //qDebug()<< url + "\r\n"+m_strUrl;
    if(url == m_strUrl)
    {
        QJsonParseError parseJsonErr;
        QJsonDocument document = QJsonDocument::fromJson(data,&parseJsonErr);
        if(!(parseJsonErr.error == QJsonParseError::NoError))
        {
            if(data.size()==0)
                qDebug()<<"json is empty";
            else
                qDebug()<<tr("wDevicesItem OnProHttpResponse 解析json文件错误！");
            return;
        }
        QJsonObject jsonObject = document.object();
        QString strType=jsonObject["_type"].toString();
        if(jsonObject["success"].toBool()==true)
        {
            qDebug()<<m_pLedCard->m_strCardId +"---------"+ strType;
             if(strType == "IsScreenOn"){

                ui->widget->setCheckedStatus(jsonObject["on"].toBool());

            }

             MACRO_ASKTIME_STOP
             MACRO_POSTING_DLG_UNLOCK

        }
    }
}
void ControlPowerManual::OnControlTypeSwitchIndexChanged(int index)
{
    if(index==ENUM_CONTRL_WIDGET_POWERCONTROL)
    {
        m_bSelected=true;
        if(m_pLedCard!=nullptr)
            onReadbackAllThisPage();
    }
    else {
        m_bSelected=false;
    }
}
void  ControlPowerManual::onReadbackAllThisPage()
{
    if(m_pLedlist==nullptr)
        return;
    if(m_pLedlist->count()!=1)
        return;
    QJsonObject json;
    json.insert("_id", getRandomString(10));
    json.insert("_type", "IsScreenOn");
    HttpPostByTypeJsonObject(pHpptClientAll,m_strUrl,json);
    MACRO_DEFINE_TIPDLG_ALL_FUCN(tr("Readback"),tr("Success"),tr("failed"))
}
void ControlPowerManual::DeletePostingDlg()
{
    if(m_PostingDlg!=nullptr)
    {
        delete m_PostingDlg;
        m_PostingDlg=nullptr;
    }
}
