#include "Msgbox.h"
#include "QDebug"

MsgBox::MsgBox(QObject * parent )
   :QObject(parent), m_btnres(QMessageBox::NoButton) {
   //qRegisterMetaType<QMessageBox::StandardButtons>("QMessageBox::StandardButtons");
   //qRegisterMetaType<QMessageBox::StandardButton>("QMessageBox::StandardButton");
   connect(this, SIGNAL(msgbox_sig(MSGBOXTYPE, QWidget *, const QString, const QString, QMessageBox::StandardButtons, QMessageBox::StandardButton)), SLOT(on_msgbox(MSGBOXTYPE, QWidget *, const QString , const QString, QMessageBox::StandardButtons , QMessageBox::StandardButton )), Qt::BlockingQueuedConnection);
}

void MsgBox::about(QWidget * parent, const QString &title, const QString &text) {
   emit msgbox_sig(mbt_about, parent, title, text, QMessageBox::NoButton, QMessageBox::NoButton);
}

void MsgBox::aboutQt(QWidget *parent, const QString &title) {
   emit msgbox_sig(mbt_aboutqt, parent, title, tr(""), QMessageBox::NoButton, QMessageBox::NoButton);
}

int MsgBox::critical(QWidget * parent, const QString &title, const QString &text, QMessageBox::StandardButtons buttons , QMessageBox::StandardButton defaultButton ) {
   emit msgbox_sig(mbt_critical, parent, title, text, buttons, defaultButton);
   return m_btnres;
}

int MsgBox::information(QWidget * parent, const QString &title, const QString &text, QMessageBox::StandardButtons buttons , QMessageBox::StandardButton defaultButton )
{
   emit msgbox_sig(mbt_information, parent, title, text, buttons, defaultButton);
   return m_btnres;
}

int MsgBox::question(QWidget * parent, const QString &title, const QString &text, QMessageBox::StandardButtons buttons , QMessageBox::StandardButton defaultButton) {
   emit msgbox_sig(mbt_question, parent, title, text, buttons, defaultButton);
   return m_btnres;
}

int MsgBox::warning(QWidget * parent, const QString &title, const QString &text, QMessageBox::StandardButtons buttons , QMessageBox::StandardButton defaultButton) {
   emit msgbox_sig(mbt_warning, parent, title, text, buttons, defaultButton);
   return m_btnres;
}


 void MsgBox::on_msgbox(MSGBOXTYPE type, QWidget * parent, const QString &title, const QString &text, QMessageBox::StandardButtons buttons, QMessageBox::StandardButton defaultButton) {
    switch(type) {
    case mbt_about:
       QMessageBox::about(parent,title,text);
       break;
    case mbt_aboutqt:
       QMessageBox::aboutQt(parent, title);
       break;
    case mbt_critical:
       m_btnres = QMessageBox::critical(parent, title, text, buttons, defaultButton);
       break;
    case mbt_information:
       m_btnres = QMessageBox::information(parent, title, text, buttons, defaultButton);
       break;
    case mbt_question:
       m_btnres = QMessageBox::question(parent, title, text, buttons, defaultButton);
       break;
    case mbt_warning:
       m_btnres = QMessageBox::warning(parent, title, text, buttons, defaultButton);
       break;
    default:
       Q_ASSERT_X(false,"QMessageBox in thread","invalid box type specified.");
    }
 }
