#include "controlbrightnessmanualwidget.h"
#include "ui_controlbrightnessmanualwidget.h"
#include "QIntValidator"
#include "LoUIClass/x_uimsgboxok.h"
#include <QJsonValue>
#include <QJsonObject>
#include <QJsonDocument>
#include <globaldefine.h>
ControlBrightnessManualWidget::ControlBrightnessManualWidget(QWidget *parent,QList<LedCard *> *list) :
    QWidget(parent),
    ui(new Ui::ControlBrightnessManualWidget)
{
     ui->setupUi(this);
     refreshLable();
    ui->pushButtonSet->setProperty("ssType", "progManageTool");
    ui->pushButtonSet->setStyleSheet("QPushButton{background:rgba(28,154,210,1);}");
    ui->pushButtonReadback->setProperty("ssType", "progManageTool");
    ui->pushButtonReadback->setStyleSheet("QPushButton{background:rgba(28,154,210,1);}");
    ui->lineEditBrightness->setValidator(new QIntValidator(1,255,this));//设置最大值和最小值
    ui->lineEditBrightness->setMaxLength(3);//设置最大输入个数
    //按钮事件绑定
    connect(ui->pushButtonSet,SIGNAL(clicked()),this,SLOT(OnClickSet()));
    connect(ui->pushButtonReadback,SIGNAL(clicked()),this,SLOT(OnManualReadbackBrightness()));
    MARCO_DEFINE_CONTROL_FUNC2(parent->parent()->parent(),parent->parent())

}

ControlBrightnessManualWidget::~ControlBrightnessManualWidget()
{
    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 ControlBrightnessManualWidget::refreshLable()
{
    ui->labelBrightnessValue->setText(tr("Brightness value"));
    ui->label_cardTypeTip->setText(tr("Y10/E10/E06：1(min)~64(max); M90/Y30/E60/Y60：1(min)~256(max)"));
    ui->pushButtonSet->setText(tr("Set"));
    ui->pushButtonReadback->setText(tr("Readback"));
}

void ControlBrightnessManualWidget::OnClickSet()
{
//    QString *p = NULL;
//    p->clear();
    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;
        }
    }

    if(ui->lineEditBrightness->text().isEmpty()||ui->lineEditBrightness->text().toInt()>255)
    {
        X_UIMsgBoxOk *pDlg=new X_UIMsgBoxOk(tr("Tip"),tr("ManualBrightnessInputTip"),this,1);
        pDlg->exec();
        ui->lineEditBrightness->setFocus();
        return;
    }
    QJsonObject json;
    json.insert("_id", getRandomString(10));
    json.insert("_type", "SetBrightness");
    json.insert("brightness", ui->lineEditBrightness->text().toInt());
    MACRO_DEFINE_TIPDLG_FUCN(tr("SetBrightness"),tr("Success"),tr("failed"))

}

void ControlBrightnessManualWidget::OnManualReadbackBrightness()
{
    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", "GetBrightness");
    MACRO_DEFINE_TIPDLG_FUCN(tr("GetBrightness"),tr("Success"),tr("failed"))
}
void ControlBrightnessManualWidget::DeletePostingDlg()
{
    if(m_PostingDlg!=nullptr)
    {
        delete m_PostingDlg;
        m_PostingDlg=nullptr;
    }
}
void ControlBrightnessManualWidget::onSelectedDeviceList(QList<LedCard*> *list)
{
    m_pLedlist=list;
    if(m_pLedlist==nullptr)
        return;
    MACRO_DEFINE_CONTROL_FUNC3
}

//获取到httpPost的应答，如果参数变化需要出发修改控件item的内容
void ControlBrightnessManualWidget::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 == "GetBrightness")
            {
                //if(m_pLedCard->brightness!=jsonObject["brightness"].toInt())
                {
                    m_pLedCard->brightness=jsonObject["brightness"].toInt();

                    QString strBrightness=QString::number(m_pLedCard->brightness);
                    ui->lineEditBrightness->setText(strBrightness);
                   // emit sigOutputInfo(m_pLedCard->m_strCardId+":"+tr("receive")+"<-"+tr("GetBrightness")+tr("success")+","+tr("brightness")+"="+strBrightness);

                }
            }
            else if(strType == "SetBrightness")
            {
                //emit sigOutputInfo(m_pLedCard->m_strCardId+":"+tr("receive")+"<-"+tr("SetBrightness")+":"+tr("success"));
            }

            MACRO_ASKTIME_STOP
            MACRO_POSTING_DLG_UNLOCK
        }
        else{
            if(strType == "SetBrightness")
            {
                MACRO_ASKTIME_STOP
                MACRO_POSTING_DLG_UNLOCK
            }

        }
    }
}
//获取到httpPost的应答，如果参数变化需要出发修改控件item的内容
void ControlBrightnessManualWidget::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 == "GetBrightness")
            {
                //if(m_pLedCard->brightness!=jsonObject["brightness"].toInt())
                {
                    m_pLedCard->brightness=jsonObject["brightness"].toInt();

                    QString strBrightness=QString::number(m_pLedCard->brightness);
                    ui->lineEditBrightness->setText(strBrightness);
                   // emit sigOutputInfo(m_pLedCard->m_strCardId+":"+tr("receive")+"<-"+tr("GetBrightness")+tr("success")+","+tr("brightness")+"="+strBrightness);

                }
            }
        }
    }
}

void ControlBrightnessManualWidget::OnControlTypeSwitchIndexChanged(int index)
{
    if(index==ENUM_CONTRL_WIDGET_BRIGHTNESS)
    {
        m_bSelected=true;
        if(m_pLedCard!=nullptr)
            onReadbackAllThisPage();
    }
    else {
        m_bSelected=false;
    }
}
void  ControlBrightnessManualWidget::onReadbackAllThisPage()
{
    if(m_pLedlist==nullptr)
        return;
    if(m_pLedlist->count()!=1)
        return;
    QJsonObject json;
    json.insert("_id", getRandomString(10));
    json.insert("_type", "GetBrightness");
    HttpPostByTypeJsonObject(pHpptClientAll,m_strUrl,json);
    ui->lineEditBrightness->clear();

}
