#include "awMsgBox.h"
#include "ui_awMsgBox.h"

#include "CAppInfo.h"

#include <QDesktopWidget>
#include <QGraphicsDropShadowEffect>

std::list<std::shared_ptr<awMsgBox>> awMsgBox::vMsgList;
std::mutex awMsgBox::msgMutex;

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

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

    setStyleSheet(g_appInfoPtr->m_sQssStr);

	if(m_pTimer == nullptr)
	{ 
		m_pTimer = std::make_shared<QTimer>();
		m_pTimer->setInterval(3 * 1000);		
	}    
	
	connect(m_pTimer.get(), &QTimer::timeout, this, [&]() {
		m_pTimer->stop();

		if (vMsgList.size() > 0)
		{
			std::scoped_lock lock(msgMutex);					
			vMsgList.pop_front();
		}						
		size_t i = 0;
		for (std::list<std::shared_ptr<awMsgBox>>::iterator it = vMsgList.begin(); it != vMsgList.end(); ++it)
		{			
			(*it)->setGeometry((*it)->x(),
				g_appInfoPtr->m_fRate * 84 + i * g_appInfoPtr->m_fRate * 90,
				(*it)->width(), (*it)->height());
			++i;
		}		
	});

    m_pTimer->start();    
}

awMsgBox::~awMsgBox()
{
	m_pTimer->stop();
	
    delete ui;
}

void awMsgBox::clear(QWidget *parent)
{
    std::scoped_lock lock(msgMutex);

    for (std::list<std::shared_ptr<awMsgBox>>::iterator it = vMsgList.begin(); it != vMsgList.end(); )
    {
        if((*it)->parent() == parent || parent == nullptr)
        {
            it = vMsgList.erase(it);
        }
		else
		{
			++it;
		}
    }
	
}

void awMsgBox::initUI(MSG_ICON_TYPE type)
{
    QString sStyle = "";
    if(type == MSG_ICON_TYPE::mit_information)
    {
        sStyle = "border-image:url(:/images/icon-um-info.png);";
    }
    else if(type == MSG_ICON_TYPE::mit_succeed)
    {
        sStyle = "border-image:url(:/images/icon-env-test-pass.png);";
    }
    else if(type == MSG_ICON_TYPE::mit_error)
    {
        sStyle = "border-image:url(:/images/icon-um.png);";
    }

    ui->label_awmb_icon->setStyleSheet(sStyle);

    ui->label_awmb_content->adjustSize();
    int nCW = g_appInfoPtr->m_fRate*40 + g_appInfoPtr->m_fRate*40 + g_appInfoPtr->m_fRate*(28 + 10) + ui->label_awmb_content->width();
    QDesktopWidget *dekwiget = QApplication::desktop();

    setGeometry((dekwiget->width() - nCW - g_appInfoPtr->m_fRate*10)/2,
                g_appInfoPtr->m_fRate*84 + vMsgList.size()*g_appInfoPtr->m_fRate * 90, 
				nCW + g_appInfoPtr->m_fRate*10, g_appInfoPtr->m_fRate*90);
    ui->widget_awmb_BG->setGeometry(5, 5 , nCW, g_appInfoPtr->m_fRate*80);
    ui->label_awmb_icon->setGeometry(g_appInfoPtr->m_fRate*40, g_appInfoPtr->m_fRate*26,
                                   g_appInfoPtr->m_fRate*28, g_appInfoPtr->m_fRate*28);
    ui->label_awmb_content->setGeometry(ui->label_awmb_icon->x() + ui->label_awmb_icon->width() + g_appInfoPtr->m_fRate*10,
                                   (height() - ui->label_awmb_content->height())/2,
                                   ui->label_awmb_content->width(), ui->label_awmb_content->height());
    ui->btn_awmb_close->setGeometry(ui->widget_awmb_BG->width() - g_appInfoPtr->m_fRate*(10 + 16),
                                    g_appInfoPtr->m_fRate*10, g_appInfoPtr->m_fRate*16, g_appInfoPtr->m_fRate*16);
    QGraphicsDropShadowEffect *effect = new QGraphicsDropShadowEffect;
    effect->setOffset(g_appInfoPtr->m_fRate*4,g_appInfoPtr->m_fRate*4);
    effect->setColor(QColor(0,0,0,30));
    effect->setBlurRadius(g_appInfoPtr->m_fRate*16);
    ui->widget_awmb_BG->setGraphicsEffect(effect);
}

void ShowMsg(QString sMsg, QWidget *parent, MSG_ICON_TYPE type)
{
	std::shared_ptr<awMsgBox> msg = std::make_shared<awMsgBox>(parent);


    msg->setMsg(sMsg);
    msg->initUI(type);
    msg->show();
	std::scoped_lock lock(awMsgBox::msgMutex);
	awMsgBox::vMsgList.push_back(msg);
}

void awMsgBox::setMsg(QString sMsg)
{
    ui->label_awmb_content->setText(sMsg);
}

void awMsgBox::on_btn_awmb_close_clicked()
{
    reject();
}
