#include "devicectrlpanel.h"
#include "devicepanel.h"
#include <QEvent>
#include <QGridLayout>
#include <QButtonGroup>
#include <QToolButton>

#include <wDevicesManager/ctrlbrightpanel.h>
#include <wDevicesManager/controlpowerwidget.h>
#include <wDevicesManager/controlnetconfigwidget.h>
#include <wDevicesManager/ctrlverifyclockpanel.h>
#include <wDevicesManager/controlhdmiwidget.h>
#include <wDevicesManager/controlvolumewidget.h>
#include <wDevicesManager/controlencryptwidget.h>
#include <wDevicesManager/ctrladvancedpanel.h>
#include <wDevicesManager/controltestwidget.h>

QTextEdit *gFdResInfo;

DeviceCtrlPanel::DeviceCtrlPanel(DevicePanel *parent) : QWidget(parent), mDevWgt(parent) {
    auto vBox = new QVBoxLayout(this);
    vBox->setContentsMargins(0,0,0,0);
    vBox->setSpacing(0);

    auto hBox = new QHBoxLayout;
    hBox->setSpacing(2);

    mBtnGrp = new QButtonGroup(this);
    for(int i=0; i<Setting_End; i++) {
        auto btn = new QToolButton();
        auto policy = btn->sizePolicy();
        policy.setHorizontalPolicy(QSizePolicy::Preferred);
        btn->setSizePolicy(policy);
        btn->setIconSize(QSize(48, 48));
        btn->setCheckable(true);
        btn->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
        btn->setProperty("ss", "CtrlTab");
        hBox->addWidget(btn);
        mBtnGrp->addButton(btn, i);
    }
    mBtnGrp->button(Setting_Bright)->setIcon(QIcon(":/res/bnBrightnessAdjustMent_s.png"));
    mBtnGrp->button(Setting_PowerControl)->setIcon(QIcon(":/res/bnPowerControl_s.png"));
    mBtnGrp->button(Setting_NetCfg)->setIcon(QIcon(":/res/bnNetConfig_s.png"));
    mBtnGrp->button(Setting_VerifyClock)->setIcon(QIcon(":/res/bnVerifyClock_s.png"));
    mBtnGrp->button(Setting_Encrypt)->setIcon(QIcon(":/res/encrypt.png"));
    mBtnGrp->button(Setting_HDMI)->setIcon(QIcon(":/res/Hdmi.png"));
    mBtnGrp->button(Setting_Volume)->setIcon(QIcon(":/res/volume.png"));
    mBtnGrp->button(Setting_Advanced)->setIcon(QIcon(":/res/AdvParam.png"));
    mBtnGrp->button(Setting_Test)->setIcon(QIcon(":/res/test.png"));

    connect(mBtnGrp, &QButtonGroup::idToggled, this, [this](int index, bool checked) {
        if(!checked || index==curIndex) return;
        curIndex = index;
        if(wgts[index]==0) {
            if(index==Setting_Bright) wgts[index] = new CtrlBrightPanel(this);
            else if(index==Setting_PowerControl) wgts[index] = new ControlPowerWidget(this,m_pLedlist);
            else if(index==Setting_NetCfg) wgts[index] = new ControlNetConfigWidget(this,m_pLedlist);
            else if(index==Setting_VerifyClock) wgts[index] = new CtrlVerifyClockPanel(this);
            else if(index==Setting_Encrypt) wgts[index] = new ControlEncryptWidget(this,m_pLedlist);
            else if(index==Setting_HDMI) wgts[index] = new ControlHdmiWidget(this,m_pLedlist);
            else if(index==Setting_Volume) wgts[index] = new ControlVolumeWidget(this,m_pLedlist);
            else if(index==Setting_Advanced) wgts[index] = new CtrlAdvancedPanel(this,m_pLedlist);
            else if(index==Setting_Test) wgts[index] = new ControlTestWidget(this,m_pLedlist);
        }
        scrollArea->takeWidget();
        scrollArea->setWidget(wgts[index]);
        emit sigSwitchIndexChanged(curIndex);
    });
    vBox->addLayout(hBox);

    auto line = new QFrame;
    line->setFrameShape(QFrame::HLine);
    line->setFrameShadow(QFrame::Sunken);
    vBox->addWidget(line);

    hBox = new QHBoxLayout();

    scrollArea = new QScrollArea();
    scrollArea->setWidgetResizable(true);
    hBox->addWidget(scrollArea);

    auto vBox2 = new QVBoxLayout();

    fdInfo = new QTextEdit();
    gFdResInfo = fdInfo;
    fdInfo->setReadOnly(true);
    fdInfo->setMaximumWidth(360);
    vBox2->addWidget(fdInfo);

    btnClear = new QPushButton(tr("Clear"));
    btnClear->setMinimumHeight(30);
    btnClear->setProperty("ssType", "progManageTool");
    connect(btnClear, &QPushButton::clicked, fdInfo, &QTextEdit::clear);
    vBox2->addWidget(btnClear);

    hBox->addLayout(vBox2);

    vBox->addLayout(hBox);

    connect(mDevWgt, &DevicePanel::sigSelectedDeviceList, this, [this](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);
                mDevWgt->fdCardNumInfo->setText(tr("single screen operation")+"->"+tr("Current Screen")+":"+m_pLedCard->m_strCardId);
            } else mDevWgt->fdCardNumInfo->setText(tr("Current Screen")+": "+tr("none"));
            fdInfo->hide();
            btnClear->hide();
        } else {
            QString strSelectNum = QString::number(m_pLedlist->count());
            mDevWgt->fdCardNumInfo->setText(tr("Multi screen operation")+"->"+tr("selected num")+":"+strSelectNum);
            fdInfo->clear();
            fdInfo->show();
            btnClear->show();
        }
    });

    transUi();

    mBtnGrp->button(0)->setChecked(true);
}

void DeviceCtrlPanel::changeEvent(QEvent *event) {
    QWidget::changeEvent(event);
    if(event->type() == QEvent::LanguageChange) transUi();
}
void DeviceCtrlPanel::transUi() {
    mBtnGrp->button(Setting_Bright)->setText(tr("Brightness Adjustment"));
    mBtnGrp->button(Setting_PowerControl)->setText(tr("Power Control"));
    mBtnGrp->button(Setting_NetCfg)->setText(tr("Net Config"));
    mBtnGrp->button(Setting_VerifyClock)->setText(tr("Time Synchronization"));
    mBtnGrp->button(Setting_HDMI)->setText(tr("Video source"));
    mBtnGrp->button(Setting_Encrypt)->setText(tr("Encrypt"));
    mBtnGrp->button(Setting_Advanced)->setText(tr("Advanced parameters"));
    mBtnGrp->button(Setting_Volume)->setText(tr("Volume Adjustment"));
    mBtnGrp->button(Setting_Test)->setText(tr("Test"));

    if(m_pLedCard!=nullptr) mDevWgt->fdCardNumInfo->setText(tr("single screen operation")+" -> "+tr("Current Screen:")+m_pLedCard->m_strCardId);
    else mDevWgt->fdCardNumInfo->setText(tr("Current Screen")+": "+tr("none"));
    btnClear->setText(tr("Clear"));
}

void DeviceCtrlPanel::OnOutputInfo(QString strInfo) {
    if(isVisible() && fdInfo->isVisible()) fdInfo->append(strInfo);
}
