#include "controlhdmimanual.h"
#include "ui_controlhdmimanual.h"
#include "LoUIClass/x_uimsgboxok.h"
#include <QJsonValue>
#include <QJsonObject>
#include <QJsonDocument>
#include <globaldefine.h>
ControlHdmiManual::ControlHdmiManual(QWidget *parent,QList<LedCard *> *list) :
    QWidget(parent),
    ui(new Ui::ControlHdmiManual)
{
    ui->setupUi(this);
    refreshLable();
    onSelectedDeviceList(list);
    ui->pushButtonReadback->setProperty("ssType", "progManageTool");
    ui->pushButtonReadback->setStyleSheet("QPushButton{background:rgba(28,154,210,1);}");
    ui->widget->setSliderColor(QColor(0, 160, 230),QColor(0, 160, 230));
    ui->widget->setBgColor(QColor(200,200,200),QColor(200,200,200));
    connect(ui->widget, SIGNAL(checkedChanged(bool)), this, SLOT(onVideoSourceChecked(bool)));
    connect(ui->pushButtonReadback,SIGNAL(clicked()),this,SLOT(OnReadback()));
    MARCO_DEFINE_CONTROL_FUNC2(parent->parent()->parent(),parent->parent())
}

ControlHdmiManual::~ControlHdmiManual()
{
    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 ControlHdmiManual::refreshLable()
{
    ui->labelHdmiIn->setText(tr("HDMI IN"));
    ui->labelAsync->setText(tr("Async"));
    ui->pushButtonReadback->setText(tr("Readback"));

}

void ControlHdmiManual::onVideoSourceChecked(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", "SyncSwitch");
    json.insert("switchOn", 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("SyncSwitch"),tr("Success"),tr("SyncSwitch")+tr("failed"));
            else
                m_PostingDlg->lock(tr("AnSyncSwitch"),tr("Success"),tr("AnSyncSwitch")+tr("failed"));
             m_pGetAskTimer->start(3000);
            m_PostingDlg->exec();

        }
    }
    else {
        if(b)
            emit sigSend(json,tr("SyncSwitch"));
        else
            emit sigSend(json,tr("AnSyncSwitch"));
    }
}
void ControlHdmiManual::OnReadback()
{
    MACRO_DEFINE_NO_SELECTED_CARD_TIP

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

}
void ControlHdmiManual::onSelectedDeviceList(QList<LedCard*> *list)
{
    m_pLedlist=list;
    if(m_pLedlist==nullptr)
        return;
    MACRO_DEFINE_CONTROL_FUNC3

}
//获取到httpPost的应答，如果参数变化需要出发修改控件item的内容
void ControlHdmiManual::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 == "SyncSwitch")
            {
                //emit sigOutputInfo(m_pLedCard->m_strCardId+":"+tr("receive")+"<-"+tr("SetBrightness")+":"+tr("success"));
            }
            else if(strType == "IsSync"){

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

            }
            MACRO_ASKTIME_STOP
            MACRO_POSTING_DLG_UNLOCK
        }
    }
}
//获取到httpPost的应答，如果参数变化需要出发修改控件item的内容
void ControlHdmiManual::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 == "IsSync"){

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

            }

             MACRO_ASKTIME_STOP
             MACRO_POSTING_DLG_UNLOCK

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

}
void ControlHdmiManual::DeletePostingDlg()
{
    if(m_PostingDlg!=nullptr)
    {
        delete m_PostingDlg;
        m_PostingDlg=nullptr;
    }
}
