#include "alarmEventRealTimeDock.h"
#include "./ui_alarmEventRealTimeDock.h"
#include "alarmEventDataView.h"

#include <QPropertyAnimation>
#include <QTimer>

#define bottomMargin 0
AlarmEventRealTimeDock::AlarmEventRealTimeDock(QWidget* parent)
    : QWidget(parent)
    , ui(new Ui::alarmEventRealTimeDock)
{
    ui->setupUi(this);
    setAttribute(Qt::WA_TranslucentBackground);

    m_isInAnimationExpand = false;
    m_isInAnimationAlert = false;
    m_curState = "collapse";

    m_tableView = new AlarmEventDataView(RealTime, this);
    ui->tableLayout->addWidget(m_tableView);
    connect(m_tableView->model(), &AlarmEventDataModel::receivedNewAlarm, this, &AlarmEventRealTimeDock::onSIG_receivedNewAlarm);

    m_animationExpand = new QPropertyAnimation(this, "geometry", this);
    m_animationExpand->setDuration(120);
    connect(m_animationExpand, &QPropertyAnimation::finished, this, [this]{m_isInAnimationExpand = false;});

    m_animationAlert = new QPropertyAnimation(this, "alertWidgetBGColorAlpha", this);
    m_animationAlert->setDuration(2500);
    m_animationAlert->setStartValue(0.0);
    m_animationAlert->setEndValue(1.0);
    m_animationAlert->setLoopCount(-1);//无限循环
    m_alertWidgetStyleSheet = QString(
                     "background-color: qradialgradient("
                     "spread:pad, "
                     "cx:0.5, cy:0.5, radius:0.5, "
                     "fx:0.5, fy:0.5, "
                     "stop:0 rgba(200, 0, 0, 0), "
                     "stop:1 rgba(255, 255, 255, 0));"
        );

    ui->btnClose->setVisible(false);
    connect(ui->btnOpen, &QPushButton::clicked, this, &AlarmEventRealTimeDock::onBtnClicked_open);
    connect(ui->btnClose, &QPushButton::clicked, this, &AlarmEventRealTimeDock::onBtnClicked_close);
    connect(ui->btnConfirmAll, &QPushButton::clicked, this, &AlarmEventRealTimeDock::onBtnClicked_confirmAll);
    connect(ui->btnCheckAll, &QPushButton::clicked, this, &AlarmEventRealTimeDock::onBtnClicked_checkAll);
    connect(ui->btnSetting, &QPushButton::clicked, this, &AlarmEventRealTimeDock::onBtnClicked_setting);

    /*if(parent)
    {
        int parentWidth = parent->width();
        int parentHeight = parent->height();
        int selfWidth = parentWidth * 0.8;
        int x = (parentWidth - selfWidth) * 0.5;
        int y = parentHeight - ui->tobBar->height();
        setGeometry(x, y, selfWidth, height());
    }*/
}

AlarmEventRealTimeDock::~AlarmEventRealTimeDock()
{}

void AlarmEventRealTimeDock::expand()
{
    if(m_isInAnimationExpand || m_curState == "expand" )
        return;

    QWidget* parent = this->parentWidget();
    if(parent)
    {
        QRect startRect(x(), y(), width(), height());
        QRect endRect(x(), parent->height() - height() - bottomMargin, width(), height());
        m_animationExpand->setStartValue(startRect);
        m_animationExpand->setEndValue(endRect);
        m_animationExpand->start();
        m_curState = "expand";
        m_isInAnimationExpand = true;

        stopAlarmAlert();
    }
}

void AlarmEventRealTimeDock::collapse()
{
    if(m_isInAnimationExpand || m_curState == "collapse" )
        return;

    QWidget* parent = this->parentWidget();
    if(parent)
    {
        QRect startRect(x(), y(), width(), height());
        QRect endRect(x(), parent->height() - ui->tobBar->height() - ui->alertWidget->height(), width(), height());
        m_animationExpand->setStartValue(startRect);
        m_animationExpand->setEndValue(endRect);
        m_animationExpand->start();
        m_curState = "collapse";
        m_isInAnimationExpand = true;
    }
}

void AlarmEventRealTimeDock::updateGeometry()
{
    QWidget* parent = this->parentWidget();
    if(parent)
    {
        int parentWidth = parent->width();
        int parentHeight = parent->height();
        int selfWidth = parentWidth * 0.8;
        int x = (parentWidth - selfWidth) * 0.5;
        int y = parentHeight - ui->tobBar->height() - ui->alertWidget->height();
        if(m_curState == "expand")
            y = parentHeight - this->height() - bottomMargin;
        setGeometry(x, y, selfWidth, height());
    }
}

void AlarmEventRealTimeDock::startAlarmAlert()
{
    if(m_isInAnimationAlert || m_curState == "expand")
        return;

    //播放音频(音频长度要和动画时间一致)
    //if(g_alarmCfgResults.notify_sound)
    m_animationAlert->start();
    m_isInAnimationAlert = true;
}

void AlarmEventRealTimeDock::stopAlarmAlert()
{
    if(!m_isInAnimationAlert)
        return;

    m_animationAlert->stop();
    m_isInAnimationAlert = false;

    m_alertWidgetStyleSheet = QString(
        "background-color: qradialgradient("
        "spread:pad, "
        "cx:0.5, cy:0.5, radius:0.5, "
        "fx:0.5, fy:0.5, "
        "stop:0 rgba(200, 0, 0, 0), "
        "stop:1 rgba(255, 255, 255, 0));"
        );
    ui->alertWidget->setStyleSheet(m_alertWidgetStyleSheet);
}

void AlarmEventRealTimeDock::setAlertWidgetBGColorAlpha(float value)
{
    m_alertWidgetBGColorAlpha = value;
    updateAlertWidgetBGColorAlpha();
}

void AlarmEventRealTimeDock::updateAlertWidgetBGColorAlpha()
{
    // 计算透明度 (从0到200再到0)
    int alpha = 200 * (1 - qAbs(2 * m_alertWidgetBGColorAlpha - 1));
    if(alpha == 1) //1表示不透，具体原因未探究，舍弃该值
        return;
    QString styleSheet = QString(
        "background-color: qradialgradient("
        "spread:pad, "
        "cx:0.5, cy:0.5, radius:0.5, "
        "fx:0.5, fy:0.5, "
        "stop:0 rgba(200, 0, 0, %1), "
        "stop:1 rgba(255, 255, 255, 0));"
                         ).arg(alpha);

    if(m_alertWidgetStyleSheet != styleSheet)
    {
        //qDebug() << alpha;
        ui->alertWidget->setStyleSheet(styleSheet);
        m_alertWidgetStyleSheet = styleSheet;
    }
}

void AlarmEventRealTimeDock::onBtnClicked_open()
{
    expand();
    ui->btnOpen->setVisible(false);
    ui->btnClose->setVisible(true);
}

void AlarmEventRealTimeDock::onBtnClicked_close()
{
    collapse();
    ui->btnOpen->setVisible(true);
    ui->btnClose->setVisible(false);
}

void AlarmEventRealTimeDock::onBtnClicked_confirmAll()
{
    //stopAlarmAlert();
    m_tableView->model()->confirmCurPageEvents();
}

void AlarmEventRealTimeDock::onBtnClicked_checkAll()
{
    emit viewAllRealTimeEvents();
}

void AlarmEventRealTimeDock::onBtnClicked_setting()
{
    emit openConfigDialog();
}

void AlarmEventRealTimeDock::onSIG_receivedNewAlarm(const EventData& event)
{
    //判断是否需要自动弹窗
    if(g_alarmCfgResults.notify_autoPop && m_curState == "collapse")
    {
        //告知
        if(event.priority == 1 && g_alarmCfgResults.autoPop_nofity)
            onBtnClicked_open();
        //预警
        else if(event.priority == 4 && g_alarmCfgResults.autoPop_warining)
            onBtnClicked_open();
        //事故
        else if(event.priority == 7 && g_alarmCfgResults.autoPop_accident)
            onBtnClicked_open();
    }

    startAlarmAlert();

    m_alarmDataStatics.allCount++;
    ui->alarmCount->setText(QString::number(m_alarmDataStatics.allCount));
    if(event.severity == "事故")
    {
        m_alarmDataStatics.accidentCount++;
        ui->accidentCount->setText(QString::number(m_alarmDataStatics.accidentCount));
    }
    else if(event.severity == "异常")
    {
        m_alarmDataStatics.abnormalCount++;
        ui->abnormalCount->setText(QString::number(m_alarmDataStatics.abnormalCount));
    }
    else if(event.severity == "预警")
    {
        m_alarmDataStatics.alertCount++;
        ui->alertCount->setText(QString::number(m_alarmDataStatics.alertCount));
    }
    else if(event.severity == "告知")
    {
        m_alarmDataStatics.informCount++;
        ui->informCount->setText(QString::number(m_alarmDataStatics.informCount));
    }
}

