#include "alarmEventConfigDialog.h"
#include "ui_alarmEventConfigDialog.h"

AlarmEventConfigDialog::AlarmEventConfigDialog(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::alarmEventConfigDialog)
    , m_curActiveTab(nullptr)
{
    ui->setupUi(this);
    setWindowFlags(Qt::FramelessWindowHint | Qt::WindowDoesNotAcceptFocus);
    setAttribute(Qt::WA_TranslucentBackground);

    initialize();
}

AlarmEventConfigDialog::~AlarmEventConfigDialog()
{
    delete ui;
}

void AlarmEventConfigDialog::showEvent(QShowEvent* event)
{
    //级别选择
    ui->level_notify->setChecked(g_alarmCfgResults.level_notify);
    ui->level_warning->setChecked(g_alarmCfgResults.level_warining);
    ui->level_abnormal->setChecked(g_alarmCfgResults.level_abnormal);
    ui->level_accident->setChecked(g_alarmCfgResults.level_accident);
    //类型选择
    ui->type_hard->setChecked(g_alarmCfgResults.type_hard);
    ui->type_platformSoft->setChecked(g_alarmCfgResults.type_platformSoft);
    ui->type_appSoft->setChecked(g_alarmCfgResults.type_appSoft);
    //提示行为
    ui->switch_sound->setChecked(g_alarmCfgResults.notify_sound);
    ui->switch_autoPop->setChecked(g_alarmCfgResults.notify_autoPop);
    //需要自动弹窗的事件级别
    ui->pop_notify->setChecked(g_alarmCfgResults.autoPop_nofity);
    ui->pop_warning->setChecked(g_alarmCfgResults.autoPop_warining);
    ui->pop_abnormal->setChecked(g_alarmCfgResults.autoPop_abnormal);
    ui->pop_accident->setChecked(g_alarmCfgResults.autoPop_accident);

    ui->tabDataFilter->click();
}

void AlarmEventConfigDialog::initialize()
{
    ui->stackedWidget->setCurrentIndex(0);

    ui->tabDataFilter->setProperty("index", 0);
    connect(ui->tabDataFilter, &QPushButton::clicked, this, &AlarmEventConfigDialog::onBtnClicked_tabBtn);
    ui->tabDataSource->setProperty("index", 1);
    connect(ui->tabDataSource, &QPushButton::clicked, this, &AlarmEventConfigDialog::onBtnClicked_tabBtn);

    connect(ui->btnConfirm, &QPushButton::clicked, this, &AlarmEventConfigDialog::onBtnClicked_confirm);
    connect(ui->btnCancle, &QPushButton::clicked, this, &AlarmEventConfigDialog::onBtnClicked_cancle);
}

void AlarmEventConfigDialog::onBtnClicked_tabBtn()
{
    QPushButton* pTab = qobject_cast<QPushButton*>(sender());
    if(pTab == m_curActiveTab)
        return;

    if(m_curActiveTab)
    {
        m_curActiveTab->setStyleSheet("QPushButton\n"
                                      "{\n"
                                      "color: rgb(250, 250, 250);\n"
                                      "font: 700 12pt \"黑体\";\n"
                                      "border:0px;\n"
                                      "background-color:transparent;\n"
                                      "}\n"
                                      "QPushButton:hover\n"
                                      "{\n"
                                      "}\n"
                                      "QPushButton:pressed\n"
                                      "{\n"
                                      "}");
    }
    if(pTab)
    {
        pTab->setStyleSheet("QPushButton\n"
                            "{\n"
                            "color: rgb(250, 250, 250);\n"
                            "font: 700 12pt \"黑体\";\n"
                            "border:1px solid rgb(200,200,200);\n"
                            "border-bottom:0px;\n"
                            "background-color:rgba(36,43,50, 250);\n"
                            "}\n"
                            "QPushButton:hover\n"
                            "{\n"
                            "}\n"
                            "QPushButton:pressed\n"
                            "{\n"
                            "}");

        int nIndex = pTab->property("index").toInt();
        ui->stackedWidget->setCurrentIndex(nIndex);
    }
    m_curActiveTab = pTab;
}

void AlarmEventConfigDialog::onBtnClicked_confirm()
{
    //级别选择
    g_alarmCfgResults.level_notify = ui->level_notify->isChecked();
    g_alarmCfgResults.level_warining = ui->level_warning->isChecked();
    g_alarmCfgResults.level_abnormal = ui->level_abnormal->isChecked();
    g_alarmCfgResults.level_accident = ui->level_accident->isChecked();
    //类型选择
    g_alarmCfgResults.type_hard = ui->type_hard->isChecked();
    g_alarmCfgResults.type_platformSoft = ui->type_platformSoft->isChecked();
    g_alarmCfgResults.type_appSoft = ui->type_appSoft->isChecked();
    //提示行为
    g_alarmCfgResults.notify_sound = ui->switch_sound->isChecked();
    g_alarmCfgResults.notify_autoPop = ui->switch_autoPop->isChecked();
    //需要自动弹窗的事件级别
    g_alarmCfgResults.autoPop_nofity = ui->pop_notify->isChecked();
    g_alarmCfgResults.autoPop_warining = ui->pop_warning->isChecked();
    g_alarmCfgResults.autoPop_abnormal = ui->pop_abnormal->isChecked();
    g_alarmCfgResults.autoPop_accident = ui->pop_accident->isChecked();

    //emit sgl_configFinish(g_alarmCfgResults);

    hide();
    emit sgl_hide();
}

void AlarmEventConfigDialog::onBtnClicked_cancle()
{
    hide();
    emit sgl_hide();
}
