#include "noticeForm.h"
#include "QsLog.h"
#include "Control/flowControl.h"
#include "ui_noticeForm.h"

NoticeForm::NoticeForm(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::NoticeForm)
{
    ui->setupUi(this);
    m_timer =new QTimer(this);
    m_timer->setSingleShot(true);
    connect(m_timer,&QTimer::timeout,this,&NoticeForm::hideForm);
    connect(this, &NoticeForm::ackNoticeMsg, &FlowControl::GetInstance(), &FlowControl::onAckNoticeMsg);
    _Init();
}

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

void NoticeForm::SetContent(NoticeForm::Type type, const QString &title, const QString &msg, const int insertIdIndex)
{
    switch(type)
    {
    case SUCCESS:
    case MSGERROR:
    case LOADING:
    case BRIEF_TIPS:
    case SYNCDATA:
    case WARNNING:
        break;
    case ACK_MSG:
        ui->noticeBtnOk->show();
        ui->noticeLabWarn->show();
        ui->noticeLabWarn->setAlignment(Qt::AlignLeft);
        break;
    }

    if ( type == WARNNING ) {        
    } else if ( type == ACK_MSG) {

        m_currentIdIndex =  insertIdIndex;
        QStringList msgInfoList = msg.split('^');
        QString noticeTitle = title;
        QString noticeContent = msgInfoList.join("\n");

        ui->noticeTitleLab->setText(noticeTitle);
        ui->noticeTitleLab->setAlignment(Qt::AlignLeft);
        QFont    font1 ( "Microsoft YaHei",  16,   QFont::Bold);
        ui->noticeTitleLab->setFont(font1);
        QPalette palette1;
        palette1.setColor(QPalette::WindowText,Qt::red);
        ui->noticeTitleLab->setPalette(palette1);
        ui->noticeTitleLab->setWordWrap(true);
        ui->noticeTitleLab->show();

        ui->noticeLabWarn_textBs->clear();
        ui->noticeLabWarn_textBs->setText(noticeContent);
        ui->noticeLabWarn_textBs->setAlignment(Qt::AlignLeft);

        QFont    font2 ( "Microsoft YaHei",  14,   QFont::Bold);
        ui->noticeLabWarn_textBs->setFont(font2);
        QPalette palette2;
        palette2.setColor(QPalette::WindowText,Qt::black);
        ui->noticeLabWarn_textBs->setPalette(palette2);
        ui->noticeLabWarn_textBs->show();

    } else {
       ui->noticeLabWarn_textBs->hide();
       ui->noticeTitleLab->hide();
    }

    if ( type == WARNNING || type == ACK_MSG ) {
            m_timer->stop();
    } else if ( type == BRIEF_TIPS ) {
        if(!m_timer->isActive() )
            m_timer->start(3000);
    } else {
        if(!m_timer->isActive() )
            m_timer->start(20000);
    }

    return;
}

void NoticeForm::_Init()
{
    setWindowFlags(this->windowFlags() | Qt::FramelessWindowHint);
    setAttribute(Qt::WA_TranslucentBackground);
    this->setModal(true);

    m_currentIdIndex = 0;
    // 初始化Gif动画
    // m_movie = new QMovie(this);
    // m_movie->setFileName(":loading.gif");
    // ui->noticeLabIng->setMovie(m_movie);
    // ui->noticeLabIng->setScaledContents(true);
    // m_movie->start();
}

void NoticeForm::hideForm()
{
    this->hide();
}

void NoticeForm::on_noticeBtnOk_clicked()
{
    QLOG_INFO()<<"NoticeForm::on_noticeBtnOk_clicked -----------------> ";
    this->hide();
    // 同步 Notice 数据库 状态显示;
    emit ackNoticeMsg( ui->noticeTitleLab->text(), m_currentIdIndex );
    return;
}
