#include "popMsgBox.h"
#include "ui_popMsgBox.h"

#include "CAppInfo.h"

#include <QDesktopWidget>
#include <QGraphicsDropShadowEffect>

popMsgBox::popMsgBox(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::popMsgBox)
{
    ui->setupUi(this);

    setWindowFlags(Qt::Dialog | Qt::FramelessWindowHint);
    setAttribute(Qt::WA_TranslucentBackground);


    setStyleSheet(g_appInfoPtr->m_sQssStr);

    QGraphicsDropShadowEffect* effect = new QGraphicsDropShadowEffect(this);
    effect->setColor(QColor(0, 0, 0, 0.08*255));
    effect->setOffset(g_appInfoPtr->m_fRate*2, g_appInfoPtr->m_fRate*2);
    effect->setBlurRadius(g_appInfoPtr->m_fRate*12);

    setGraphicsEffect(effect);
}

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

void popMsgBox::setBtnText(QString sBtnYesText, QString sBtnNoText)
{
    ui->btn_yes->setText(sBtnYesText);
    ui->btn_no->setText(sBtnNoText);
}

void popMsgBox::setMsg(QString sMsg, QString sTitle)
{
    ui->label_pmb_content->setText(sMsg);
    ui->label_pmb_title->setText(sTitle);
}

void popMsgBox::initUI(POP_MSG_BTN btn)
{
    int nCW = g_appInfoPtr->m_fRate*352;
    QDesktopWidget *dekwiget = QApplication::desktop();
    setGeometry((dekwiget->width() - nCW)/2, (dekwiget->height() - g_appInfoPtr->m_fRate*265)/2, nCW, g_appInfoPtr->m_fRate*265);
    ui->widget_pmb_bg->setGeometry(g_appInfoPtr->m_fRate*6, g_appInfoPtr->m_fRate*6 , nCW - g_appInfoPtr->m_fRate*12, g_appInfoPtr->m_fRate*253);
    ui->label_pmb_icon->setGeometry(g_appInfoPtr->m_fRate*30, g_appInfoPtr->m_fRate*30,
                                   g_appInfoPtr->m_fRate*40, g_appInfoPtr->m_fRate*40);
    ui->label_pmb_title->adjustSize();
    ui->label_pmb_title->setGeometry(ui->label_pmb_icon->x() + ui->label_pmb_icon->width() + g_appInfoPtr->m_fRate*16, g_appInfoPtr->m_fRate*30,
                                     ui->label_pmb_title->width(), ui->label_pmb_title->height());
    ui->label_pmb_content->setFixedWidth(nCW - ui->label_pmb_icon->x() - ui->label_pmb_icon->width() - g_appInfoPtr->m_fRate*16*2 - g_appInfoPtr->m_fRate*12);
    ui->label_pmb_content->adjustSize();
    ui->label_pmb_content->setGeometry(ui->label_pmb_icon->x() + ui->label_pmb_icon->width() + g_appInfoPtr->m_fRate*16,
                                       ui->label_pmb_title->y() + ui->label_pmb_title->height() + g_appInfoPtr->m_fRate*8,
                                   ui->label_pmb_content->width(), ui->label_pmb_content->height());
	if (btn == POP_MSG_BTN::pmb_yes)
	{
		ui->btn_no->setVisible(false);
	}
	ui->btn_yes->setGeometry(ui->widget_pmb_bg->width() - g_appInfoPtr->m_fRate*(20 + 80),
                             ui->widget_pmb_bg->height() - g_appInfoPtr->m_fRate*(20 + 30),
                             g_appInfoPtr->m_fRate*80, g_appInfoPtr->m_fRate*30);
    ui->btn_no->setGeometry(ui->btn_yes->x() - g_appInfoPtr->m_fRate*15 - ui->btn_yes->width(),
                            ui->btn_yes->y(), ui->btn_yes->width(), ui->btn_yes->height());
}

int popMsg(QString sMsg, QString sTitle, QWidget *parent, QString sBtnYesText, QString sBtnNoText)
{
    popMsgBox msgDlg(parent);

    msgDlg.setMsg(sMsg, sTitle);
    msgDlg.initUI(POP_MSG_BTN::pmb_yse_no);
    msgDlg.setBtnText(sBtnYesText, sBtnNoText);

    return msgDlg.exec();
}

int popMsg(QString sMsg, QString sTitle, QWidget *parent, POP_MSG_BTN btn, int nStayTime)
{
    popMsgBox msgDlg(parent);

    msgDlg.setMsg(sMsg, sTitle);
    msgDlg.initUI(btn);
	if (nStayTime != 0)
	{
		msgDlg.setStayTime(nStayTime);
	}
    return msgDlg.exec();
}

void popMsgBox::setStayTime(int nStayTime)
{
	m_timePtr = std::make_shared<QTimer>();
	m_timePtr->setInterval(nStayTime * 1000);
	connect(m_timePtr.get(), &QTimer::timeout, this, [&](){
		accept();
	});
	m_timePtr->start();
}

void popMsgBox::on_btn_yes_clicked()
{
	if (m_timePtr != nullptr)
	{
		m_timePtr->stop();
	}
    accept();
}

void popMsgBox::on_btn_no_clicked()
{
	if (m_timePtr != nullptr)
	{
		m_timePtr->stop();
	}
    reject();
}
