#include "devicescontrollwidget.h"
#include "ui_devicescontrollwidget.h"
#include "mdevicesetting.h"
#include <globaldefine.h>

DevicesControllWidget::DevicesControllWidget(QWidget *parent,QList<LedCard *> *list) :
    QWidget(parent),
    ui(new Ui::DevicesControllWidget)
{
    ui->setupUi(this);
    refreshLable();
    onSelectedDeviceList(list);
    m_wnds.clear();
    ui->labelCardId->setStyleSheet("QLabel{color:rgba(0,0,255,1);}");
    for(int i=0;i<=ENUM_CONTRL_WIDGET_END;i++)
        m_wnds.append(ui->label);
    curIndex=ENUM_CONTRL_WIDGET_END;

     SwitchControlWidget(0);
    ui->pushButtonClear->setProperty("ssType", "progManageTool");
    ui->pushButtonClear->setStyleSheet("QPushButton{background:rgba(28,154,210,1);}");
    connect(ui->pushButtonClear,SIGNAL(clicked()),ui->textEdit,SLOT(clear()));
    connect(parent,SIGNAL(sigSelectedDeviceList(QList<LedCard *> *)),this,SLOT(onSelectedDeviceList(QList<LedCard *> *)));
    connect(parent,SIGNAL(sigChangeLanguage()),this,SLOT(refreshLable()));
}

DevicesControllWidget::~DevicesControllWidget()
{
    delete ui;
}
void DevicesControllWidget::refreshLable()
{
    if(m_pLedCard!=nullptr)
        ui->labelCardId->setText(tr("single screen operation")+"->"+tr("Current Screen:")+m_pLedCard->m_strCardId);
    else {
        ui->labelCardId->setText(tr("Current Screen:")+tr("none"));

    }
    ui->pushButtonClear->setText(tr("Clear"));
}


void DevicesControllWidget::SwitchControlWidget(int index)
{
    if(index==curIndex)
        return;
    switch(index)
    {
        case ENUM_CONTRL_WIDGET_BRIGHTNESS:
        {
            if(m_pControlBrightnessWidget==nullptr)
            {
                m_pControlBrightnessWidget=new ControlBrightnessWidget(this,m_pLedlist);
                m_wnds.replace(ENUM_CONTRL_WIDGET_BRIGHTNESS,m_pControlBrightnessWidget);
            }
        }
        break;
        case ENUM_CONTRL_WIDGET_POWERCONTROL:
        {
            if(m_pControlPowerWidget==nullptr)
            {
                m_pControlPowerWidget=new ControlPowerWidget(this,m_pLedlist);
                m_wnds.replace(ENUM_CONTRL_WIDGET_POWERCONTROL,m_pControlPowerWidget);
                //如果只选中一个控制卡则需要提前获取卡的电源状态
            }
        }
        break;
        case ENUM_CONTRL_WIDGET_NETCONFIG:
        {
            if(m_pControlNetConfigWidget==nullptr)
            {
                m_pControlNetConfigWidget=new ControlNetConfigWidget(this,m_pLedlist);
                m_wnds.replace(ENUM_CONTRL_WIDGET_NETCONFIG,m_pControlNetConfigWidget);
            }
        }
        break;
        case ENUM_CONTRL_WIDGET_VERIFYCLOCK:
        {
            if(m_pControlVerifyClockWidget==nullptr)
            {
                m_pControlVerifyClockWidget=new ControlVerifyClockWidget(this,m_pLedlist);
                m_wnds.replace(ENUM_CONTRL_WIDGET_VERIFYCLOCK,m_pControlVerifyClockWidget);
            }
        }
        break;
        case ENUM_CONTRL_WIDGET_HDMIINPUT:
        {
            if(m_pControlHdmiWidgett==nullptr)
            {
                m_pControlHdmiWidgett=new ControlHdmiWidget(this,m_pLedlist);
                m_wnds.replace(ENUM_CONTRL_WIDGET_HDMIINPUT,m_pControlHdmiWidgett);
            }
        }
        break;
        case ENUM_CONTRL_WIDGET_VOLUME:
        {
            if(m_pControlVolumeWidget==nullptr)
            {
                m_pControlVolumeWidget=new ControlVolumeWidget(this,m_pLedlist);
                m_wnds.replace(ENUM_CONTRL_WIDGET_VOLUME,m_pControlVolumeWidget);
            }
        }
        break;
        case ENUM_CONTRL_WIDGET_ENCRYPT:
        {
            if(m_pControlEncryptWidget==nullptr)
            {
                m_pControlEncryptWidget=new ControlEncryptWidget(this,m_pLedlist);
                m_wnds.replace(ENUM_CONTRL_WIDGET_ENCRYPT,m_pControlEncryptWidget);
            }
        }
        break;
        case ENUM_CONTRL_WIDGET_ADVPARAM:
        {
            if(m_pControlAdvParamWidget==nullptr)
            {
                m_pControlAdvParamWidget=new ControlAdvParamWidget(this,m_pLedlist);
                m_wnds.replace(ENUM_CONTRL_WIDGET_ADVPARAM,m_pControlAdvParamWidget);
            }
        }
        break;
        case ENUM_CONTRL_WIDGET_TEST:
        {
            if(m_pControlTestWidget==nullptr)
            {
                m_pControlTestWidget=new ControlTestWidget(this,m_pLedlist);
                m_wnds.replace(ENUM_CONTRL_WIDGET_TEST,m_pControlTestWidget);
            }
        }
        break;
        default:
        {

        }
        break;

    }
    ui->horizontalLayout_3->replaceWidget(m_wnds.at(curIndex), m_wnds.at(index));
    m_wnds.at(index)->show();//显示选中页面
    m_wnds.at(curIndex)->hide();//隐藏上一个页面
    curIndex=index;
    emit sigSwitchIndexChanged(curIndex);

}

void DevicesControllWidget::OnOutputInfo(QString strInfo)
{
    if(this->isVisible()&& ui->textEdit->isVisible())
    {
        ui->textEdit->append(strInfo+"\r\n");
    }
}
void DevicesControllWidget::onSelectedDeviceList(QList<LedCard*> *list)
{
    m_pLedlist=list;
    if(m_pLedlist==nullptr)
        return;
    if(m_pLedlist->count()<=1)
    {
        if(m_pLedlist->count()==1)
        {
            m_pLedCard=list->at(0);
            ui->labelCardId->setText(tr("single screen operation")+"->"+tr("Current Screen")+":"+m_pLedCard->m_strCardId);

        }
        else
        {
            ui->labelCardId->setText(tr("Current Screen")+":"+tr("none"));

        }
        ui->textEdit->hide();
        ui->pushButtonClear->hide();
    }
    else {
        QString strSelectCards="";
//        for(int i=0;i<m_pLedlist->count();i++)
//        {
//            strSelectCards+=m_pLedlist->at(i)->m_strCardId;
//            if(i<m_pLedlist->count()-1)
//                strSelectCards+=",";
//        }
//        ui->labelCardId->setText(tr("Current Screen")+":"+strSelectCards);
        QString strSelectNum=QString::number(m_pLedlist->count());
        //ui->labelCardId->setText(tr("多屏操作->选中屏幕数")+":"+strSelectNum);
        ui->labelCardId->setText(tr("Multi screen operation")+"->"+tr("selected num")+":"+strSelectNum);
        ui->textEdit->show();
        ui->textEdit->clear();
        ui->pushButtonClear->show();
    }
}

