#include "cmessagebox.h"
#include "iconhelper.h"
#include "fancytitlebar.h"
#include "fancybar.h"

#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QPushButton>
#include <QLabel>

CMessageBox::CMessageBox(QWidget *parent)
    : FancyDialog(parent)
{
    initialize();
    createConnections();
}

CMessageBox::~CMessageBox()
{

}

void CMessageBox::setMessageType(MessageType type)
{
    m_eMessageType = type;
    if(m_eMessageType == TipBox) {
         this->setWindowTitle("提示");
//        this->fancyBar()->setTitle("提示");
        m_pLogoLabel->setPixmap(QPixmap(":/res/image/messagebox/info.png"));
    } else if(m_eMessageType == WarningBox){
        this->setWindowTitle("警告");
//        this->fancyBar()->setTitle("警告");
        m_pLogoLabel->setPixmap(QPixmap(":/res/image/messagebox/warning.png"));
    } else if(m_eMessageType == ErrorBox) {
        this->setWindowTitle("错误");
//        this->fancyBar()->setTitle("错误");
        m_pLogoLabel->setPixmap(QPixmap(":/res/image/messagebox/error.png"));
    }
}

void CMessageBox::setButtonNum(int num)
{
    if(num == 1) {
        m_pSubmitButton->show();
        m_pCancelButton->hide();
    } else {
        m_pSubmitButton->show();
        m_pCancelButton->show();
    }
}

void CMessageBox::setText(const QString &text)
{
    m_pTextLabel->setText("       " + text);
}

int CMessageBox::exec()
{
    return FancyDialog::exec();
}

void CMessageBox::initialize()
{
    m_pCentralWidget = new QWidget;
    m_pMainVLayout = new QVBoxLayout;
    m_pCentralWidget->setLayout(m_pMainVLayout);

    m_pMiddlHLayout = new QHBoxLayout;
    m_pLogoLabel = new QLabel;
    m_pLogoLabel->setPixmap(QPixmap(":/res/image/messagebox/info.png"));
    m_pLogoLabel->setFixedSize(130, 130);
    m_pTextLabel = new QLabel;
    m_pTextLabel->setObjectName("MessageBoxTextLabel");
    m_pTextLabel->setText("消息框");
    m_pMiddlHLayout->addWidget(m_pLogoLabel);
    m_pMiddlHLayout->addWidget(m_pTextLabel);
    m_pMainVLayout->addLayout(m_pMiddlHLayout);

    m_pButtonHLayout = new QHBoxLayout;
    m_pSubmitButton = new QPushButton;
    m_pSubmitButton->setText("确定");
    m_pSubmitButton->setFixedSize(80, 40);
    m_pSubmitButton->setObjectName("MessageSubmitButton");
    m_pCancelButton = new QPushButton;
    m_pCancelButton->setText("取消");
    m_pCancelButton->setFixedSize(80, 40);
    m_pCancelButton->setObjectName("MessageCancelButton");
    m_pButtonHLayout->addStretch();
    m_pButtonHLayout->addWidget(m_pSubmitButton);
    m_pButtonHLayout->addWidget(m_pCancelButton);
    m_pMainVLayout->addLayout(m_pButtonHLayout);

    this->setCentralWidget(m_pCentralWidget);
    this->setBackgroundImage(":/res/image/messagebox/bg.png");
    QPixmap pix = IconHelper::Instance()->getPixmap("#FEFEFE", 0xf0b1, 30, 35, 35);
    this->setWindowIcon(QIcon(":/res/image/messagebox/logo.png"));
    this->setFixedSize(500, 300);
    this->setWindowTitle("消息框");

    m_eMessageType = TipBox;
    this->fancyBar()->titleBar()->hideMaximizeButton();
}

void CMessageBox::createConnections()
{
    connect(m_pSubmitButton, &QPushButton::clicked, this, &CMessageBox::slotClickButton);
    connect(m_pCancelButton, &QPushButton::clicked, this, &CMessageBox::slotClickButton);
}

void CMessageBox::slotClickButton()
{
    QPushButton* button = qobject_cast<QPushButton*>(this->sender());
    if(button == m_pSubmitButton) {
        QDialog::accept();
    } else if(button == m_pCancelButton) {
        QDialog::reject();
    }
}
