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

#include <device/ctrlbrightpanel.h>
#include <device/controlpowerwidget.h>
#include <device/ctrlnetworkpanel.h>
#include <device/ctrlverifyclockpanel.h>
#include <device/ctrlhdmipanel.h>
#include <device/controlvolumewidget.h>
#include <device/ctrlpwdpanel.h>
#include <device/ctrladvancedpanel.h>
#include <device/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, gSelCards);
            else if(index==Setting_NetCfg) wgts[index] = new CtrlNetworkPanel(this);
            else if(index==Setting_VerifyClock) wgts[index] = new CtrlVerifyClockPanel(this);
            else if(index==Setting_Encrypt) wgts[index] = new CtrlPwdPanel(this);
            else if(index==Setting_HDMI) wgts[index] = new CtrlHdmiPanel(this);
            else if(index==Setting_Volume) wgts[index] = new ControlVolumeWidget(this, gSelCards);
            else if(index==Setting_Advanced) wgts[index] = new CtrlAdvancedPanel(this);
            else if(index==Setting_Test) wgts[index] = new ControlTestWidget(this, gSelCards);
        }
        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]() {
        if(gSelCards->count() < 2) {
            if(gSelCards->count()==1) mDevWgt->fdCardNumInfo->setText(tr("Current Screen")+": "+gSelCards->at(0)->m_strCardId);
            else mDevWgt->fdCardNumInfo->setText(tr("Current Screen")+": "+tr("none"));
            fdInfo->hide();
            btnClear->hide();
        } else {
            mDevWgt->fdCardNumInfo->setText(tr("Multi screen operation")+". "+tr("selected num")+": "+QString::number(gSelCards->count()));
            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 Sync"));
    mBtnGrp->button(Setting_HDMI)->setText(tr("Video source"));
    mBtnGrp->button(Setting_Encrypt)->setText(tr("Password"));
    mBtnGrp->button(Setting_Advanced)->setText(tr("Advanced"));
    mBtnGrp->button(Setting_Volume)->setText(tr("Volume Adj."));
    mBtnGrp->button(Setting_Test)->setText(tr("Test"));

    if(gSelCards->count() < 1) mDevWgt->fdCardNumInfo->setText(tr("Current Screen")+": "+tr("none"));
    else if(gSelCards->count()==1) mDevWgt->fdCardNumInfo->setText(tr("Current Screen")+": "+gSelCards->at(0)->m_strCardId);
    else mDevWgt->fdCardNumInfo->setText(tr("Multi screen operation")+". "+tr("selected num")+": "+QString::number(gSelCards->count()));
    btnClear->setText(tr("Clear"));
}

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