/*主界面终端管理，节目管理，高级管理，终端控制的控件界面*/
#include "mtabwidget.h"
#include "loappconfig.h"
#include <QSettings>
mTabWidget::mTabWidget(QWidget *wDeviceManager, QWidget *wProgramManager,
                       QWidget *wAdvanceManager, QWidget *wDeviceSetting,QWidget *wGuangYinPin,
                       QWidget *parent) :

    QWidget(parent),
    curIndex(0)
{
    setAttribute(Qt::WA_DeleteOnClose);
    m_pDeviceWidget=wDeviceManager;
    //创建主页面4个页面切换按钮-终端管理，节目管理，高级节目管理，终端控制
    bnDeviceManager  = new QPushButton();
    bnProgramManager = new QPushButton();
//    bnAdvanceManager = new QPushButton();
    bnDeviceSetting  = new QPushButton();
    bnGuangYinPin = new QPushButton();

    bnDeviceManager->setObjectName("bnDeviceManager");
    bnProgramManager->setObjectName("bnProgramManager");
//    bnAdvanceManager->setObjectName("bnAdvanceManager");
    bnDeviceSetting->setObjectName("bnDeviceSetting");
    bnGuangYinPin->setObjectName("bnGuangYinPin");

    bnDeviceManager->setText( "\n\n\n" + tr("Device"));
    bnProgramManager->setText("\n\n\n" + tr("Program"));
//    bnAdvanceManager->setText("\n\n\n" + tr("Advance"));
    bnDeviceSetting->setText( "\n\n\n" + tr("Control"));
    bnGuangYinPin->setText( "\n\n\n" + tr("GuangYinPin"));

    wDeviceManager->setObjectName("wDeviceManager");
    wProgramManager->setObjectName("wProgramManager");
//    wAdvanceManager->setObjectName("wAdvanceManager");
    wDeviceSetting->setObjectName("wDeviceSetting");
    wGuangYinPin->setObjectName("wGuangYinPin");

    m_bns = new QButtonGroup();
    m_bns->addButton(bnDeviceManager,  ENUM_MAIN_TOOL_DEVICESMANAGER);
    m_bns->addButton(bnProgramManager, ENUM_MAIN_TOOL_PROGRAM);
    m_bns->addButton(bnDeviceSetting,  ENUM_MAIN_TOOL_DEVICESCONTROL);
//    m_bns->addButton(bnAdvanceManager, ENUM_MAIN_TOOL_PROGRAM_A);
    m_bns->addButton(bnGuangYinPin, ENUM_MAIN_TOOL_GUANGYINPIN);

    for(int i=0; i<ENUM_MAIN_TOOL_END; i++) {
        m_bns->button(i)->setProperty("ssType", "mainSwitcher");
        m_bns->button(i)->setCheckable(true);
        if(i == curIndex) {
            m_bns->button(i)->setChecked(true);//初始按钮终端管理为选中状态
        } else {
            m_bns->button(i)->setChecked(false);
        }
    }
    //4个按钮横向布局
    QHBoxLayout *plBns = new QHBoxLayout();
    //alahover 20200203 -s
    //plBns->addStretch(1);
    plBns->addSpacing(160);
    plBns->addWidget(bnDeviceManager, 0);
    plBns->addSpacing(10);
    plBns->addWidget(bnProgramManager, 0);
    plBns->addSpacing(10);
//    plBns->addWidget(bnAdvanceManager, 0);
//    plBns->addSpacing(10);
    plBns->addWidget(bnDeviceSetting, 0);
    plBns->addSpacing(10);
    plBns->addWidget(bnGuangYinPin, 0);
    plBns->addSpacing(60);

    //plBns->addStretch(1);
    //alahover 20200203 -o
    plBns->setContentsMargins(0, 0, 0, 0);
    //页面数组装入4个切换响应页面
    m_wnds.clear();
    m_wnds.append(wDeviceManager);
    m_wnds.append(wProgramManager);
//    m_wnds.append(wAdvanceManager);
    m_wnds.append(wDeviceSetting);
    m_wnds.append(wGuangYinPin);
    for(int i=0; i<ENUM_MAIN_TOOL_END; i++) {
        m_wnds.at(i)->setProperty("ssBackground", "bright");
        m_wnds.at(i)->setProperty("ssPrettify", "fillet");
    }

    QVBoxLayout *plWnd = new QVBoxLayout();
    plWnd->addLayout(plBns, 0);
    plWnd->addWidget(m_wnds.at(curIndex), 1);//初始化响应页面为终端管理页面
    plWnd->setSpacing(0);
    plWnd->setContentsMargins(0, 0, 0, 0);
    setLayout(plWnd);


    LoAppConfig *cfg = LoAppConfig::getInstance();
    QSettings *settings = new QSettings(cfg->OrganizationName(), cfg->ApplicationName());
    if(settings->value("GuangYingPin").isValid())
    {
        if(settings->value("GuangYingPin").toBool())
        {
            m_globBoolGuangYingPin=true;
        }
        else {
            m_globBoolGuangYingPin=false;

        }
    }
    else {
        m_globBoolGuangYingPin=false;

    }
    bnGuangYinPin->setVisible(m_globBoolGuangYingPin);
    //按钮信号和槽函数绑定
    connect(m_bns, SIGNAL(buttonClicked(int)), this, SLOT(onBnsClicked(int)));
}
void mTabWidget::onGuangYingPinChanged()
{
    bnGuangYinPin->setVisible(m_globBoolGuangYingPin);
}
void mTabWidget::refreshLable()
{
    if(bnDeviceManager!=nullptr)
        bnDeviceManager->setText( "\n\n\n" + tr("Device"));
    if(bnProgramManager!=nullptr)
        bnProgramManager->setText("\n\n\n" + tr("Program"));
    if(bnAdvanceManager!=nullptr)
        bnAdvanceManager->setText("\n\n\n" + tr("Advance"));
    if(bnDeviceSetting!=nullptr)
        bnDeviceSetting->setText( "\n\n\n" + tr("Control"));
    if(bnGuangYinPin!=nullptr)
      bnGuangYinPin->setText( "\n\n\n" + tr("GuangYinPin"));
    bnGuangYinPin->setVisible(m_globBoolGuangYingPin);
}
void mTabWidget::paintEvent(QPaintEvent *)
{
    QStyleOption opt;
    opt.init(this);
    QPainter p(this);
    style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
}

void mTabWidget::onBnsClicked(int id)
{
    if(id != curIndex)
    {
        if((id ==ENUM_MAIN_TOOL_DEVICESMANAGER && curIndex==ENUM_MAIN_TOOL_DEVICESCONTROL)||(id ==ENUM_MAIN_TOOL_DEVICESCONTROL && curIndex==ENUM_MAIN_TOOL_DEVICESMANAGER))
         {
            emit ((mDeviceManager *)m_pDeviceWidget)->sigShowDevicesInfoOrControlWidget(id);
            curIndex = id;
        }
        else {
            layout()->replaceWidget(m_wnds.at(curIndex), m_wnds.at(id));
            m_wnds.at(curIndex)->hide();//隐藏上一个页面
            m_wnds.at(id)->show();//显示选中页面
            curIndex = id;
            if(curIndex == ENUM_MAIN_TOOL_DEVICESMANAGER ||curIndex ==ENUM_MAIN_TOOL_DEVICESCONTROL)
            {
              //开启UDP定时器
                emit sigOpenDeviceUdpTimer(true);
                emit ((mDeviceManager *)m_pDeviceWidget)->sigShowDevicesInfoOrControlWidget(id);

            }
            else {
                //取消定时器
               // emit sigOpenDeviceUdpTimer(false);
            }
        }

    }
    else {

    }
}
