#include "clNoticePopWidget.h"
#include "ui_clNoticePopWidget.h"

#include "CAppInfo.h"

#include <QGraphicsDropShadowEffect>

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

    setStyleSheet(g_appInfoPtr->m_sQssStr);
}

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

void clNoticePopWidget::setUI(const int nLeft, const int nTop, const int nWidth, const int nHeight)
{
    setGeometry(nLeft, nTop, nWidth, nHeight);
    ui->widget_npw_BG->setGeometry(g_appInfoPtr->m_fRate*5, g_appInfoPtr->m_fRate*5, width()-g_appInfoPtr->m_fRate*10, height()-g_appInfoPtr->m_fRate*10);
    ui->label_noticeIcon->setGeometry(g_appInfoPtr->m_fRate*20, g_appInfoPtr->m_fRate*24,
                                      g_appInfoPtr->m_fRate*16, g_appInfoPtr->m_fRate*12);
    ui->label_noticeTitle->adjustSize();
    ui->label_noticeTitle->setGeometry(ui->label_noticeIcon->x() + ui->label_noticeIcon->width() + g_appInfoPtr->m_fRate*8,
                                       ui->label_noticeIcon->y() + (ui->label_noticeIcon->height() - ui->label_noticeTitle->height())/2,
                                       ui->label_noticeTitle->width(), ui->label_noticeTitle->height());
    ui->txtb_noticeContent->setGeometry(ui->label_noticeIcon->x(), ui->label_noticeTitle->y() + ui->label_noticeTitle->height() + g_appInfoPtr->m_fRate*10,
                                        g_appInfoPtr->m_fRate*260, g_appInfoPtr->m_fRate*60);
    ui->btn_detail->setGeometry(ui->widget_npw_BG->width() - g_appInfoPtr->m_fRate*(20 + 80),
                                ui->widget_npw_BG->height() - g_appInfoPtr->m_fRate*(20 + 30),
                                g_appInfoPtr->m_fRate*80, g_appInfoPtr->m_fRate*30);
    ui->btn_ignore->setGeometry(ui->btn_detail->x() - g_appInfoPtr->m_fRate*10 - ui->btn_detail->width(), ui->btn_detail->y(),
                                ui->btn_detail->width(), ui->btn_detail->height());

    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_npw_BG->setGraphicsEffect(effect);
}

void clNoticePopWidget::addNotice(const CNoticeInfo ni)
{
	std::vector<CNoticeInfo>::iterator it = 
		std::find_if(vIgnoreNL.begin(), vIgnoreNL.end(), [&ni](const CNoticeInfo &temp) {
			return temp.nId == ni.nId;
		});
	if (it != vIgnoreNL.end())
	{
		return;
	}

	vNL.push_back(ni);
	if (vNL.size() == 1)
	{
		showNotice(ni);
	}	
}

void clNoticePopWidget::setNoticeRead(__int64 nId)
{
	std::vector<CNoticeInfo>::iterator it =
		std::find_if(vNL.begin(), vNL.end(), [&nId](const CNoticeInfo &temp) {
		return temp.nId == nId;
	});

	if (it != vNL.end())
	{
		vNL.erase(it);
		if (vNL.size() <= 0)
		{
			hide();
		}
		else
		{
			showNotice(*vNL.begin());
		}
		
	}
}

void clNoticePopWidget::showNotice(const CNoticeInfo ni)
{
	if (ni.bHasRecalled)
	{
		ui->label_noticeTitle->setText(QString::fromLocal8Bit("发送者已撤回消息：%1").arg(ni.sTitle));
		ui->label_noticeTitle->adjustSize();
		ui->txtb_noticeContent->insertPlainText(QString::fromLocal8Bit("该消息已经被发送者撤回。"));
	}
	else
	{
		ui->label_noticeTitle->setText(ni.sTitle);
		ui->label_noticeTitle->adjustSize();
		ui->txtb_noticeContent->insertHtml(ni.sContent);
	}
	
}

int clNoticePopWidget::noticeCount()
{
	return vNL.size();
}

void clNoticePopWidget::on_btn_detail_clicked()
{
    emit showNoticeDetail(vNL.begin()->nId);
    on_btn_ignore_clicked();
}

void clNoticePopWidget::on_btn_ignore_clicked()
{
	vIgnoreNL.push_back(*vNL.begin());
    vNL.erase(vNL.begin());
    if(vNL.size() <= 0)
    {
        hide();
    }
}
