#include "controlvolumemanual.h"
#include "ui_controlvolumemanual.h"
#include "LoUIClass/x_uimsgboxok.h"
#include <QJsonValue>
#include <QJsonObject>
#include <QJsonDocument>
#include <globaldefine.h>
ControlVolumeManual::ControlVolumeManual(QWidget *parent,QList<LedCard *> *list) :
    QWidget(parent),
    ui(new Ui::ControlVolumeManual)
{
    ui->setupUi(this);
    refreshLable();
    onSelectedDeviceList(list);
    ui->horizontalSlider->setRange(0,15);
    ui->horizontalSlider->setTickPosition(QSlider::TicksAbove);
    ui->pushButtonReadback->setProperty("ssType", "progManageTool");
    ui->pushButtonReadback->setStyleSheet("QPushButton{background:rgba(28,154,210,1);}");
    connect(ui->pushButtonReadback,SIGNAL(clicked()),this,SLOT(OnReadback()));
    connect(ui->horizontalSlider, SIGNAL(valueChanged(int)), ui->labelCurValue, SLOT(setNum(int)));
    connect(ui->horizontalSlider, SIGNAL(sliderReleased()), this, SLOT(setVolumeValue()));


    MARCO_DEFINE_CONTROL_FUNC2(parent->parent()->parent(),parent->parent())
}

ControlVolumeManual::~ControlVolumeManual()
{
    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 ControlVolumeManual::setVolumeValue()
{
    MACRO_DEFINE_NO_SELECTED_CARD_TIP

    QJsonObject json;
    json.insert("_id", getRandomString(10));
    json.insert("_type", "SetVolume");
    json.insert("volume", ui->labelCurValue->text().toInt());
    MACRO_DEFINE_TIPDLG_FUCN(tr("SetVolume"),tr("Success"),tr("failed"))

}
void ControlVolumeManual::refreshLable()
{
    ui->labelVolume->setText(tr("Volume(min:0-max:15)"));
    ui->pushButtonReadback->setText(tr("Readback"));
}


void ControlVolumeManual::OnReadback()
{
    MACRO_DEFINE_NO_SELECTED_CARD_TIP

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

}
void ControlVolumeManual::onSelectedDeviceList(QList<LedCard*> *list)
{
    m_pLedlist=list;
    MACRO_DEFINE_CONTROL_FUNC3

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

                int itempVolume=jsonObject["volume"].toInt();
                ui->horizontalSlider->setValue(itempVolume);
                ui->labelCurValue->setNum(itempVolume);
            }

            MACRO_ASKTIME_STOP
            MACRO_POSTING_DLG_UNLOCK

        }
    }
}
//获取到httpPost的应答，如果参数变化需要出发修改控件item的内容
void ControlVolumeManual::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 == "GetVolume"){
                int itempVolume=jsonObject["volume"].toInt();
                ui->horizontalSlider->setValue(itempVolume);
                ui->labelCurValue->setNum(itempVolume);

            }

             MACRO_ASKTIME_STOP
             MACRO_POSTING_DLG_UNLOCK

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

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