/***************************************************************************
 *   Copyright (C) 2010 by Volker Lanz <vl@fidra.de>                       *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA            *
 ***************************************************************************/

#include "veneer/messagebox.h"

#include <QMessageBox>
#include <QDebug>
#include <QString>
#include <QStringList>
#include <QAbstractButton>


static int showBox(QWidget* parent, const QString& text, const QString& caption, const QString& textYes, const QString& textNo, QMessageBox::Icon icon = QMessageBox::Question, const QString& textCancel = QString(), const QStringList& list = QStringList())
{
	QMessageBox mb(parent);

#if defined(Q_OS_MAC)
	mb.setText(caption);
	mb.setInformativeText(text);
#else
	mb.setWindowTitle(caption);
	mb.setText(text);
#endif

	if (!list.isEmpty())
		mb.setDetailedText(list.join("\n"));

	QMessageBox::StandardButtons buttons;

	if (icon == QMessageBox::Question)
	{
		buttons = QMessageBox::Yes | QMessageBox::No;

		if (!textCancel.isEmpty())
			buttons |= QMessageBox::Cancel;
	}
	else
		buttons = QMessageBox::Ok;

	mb.setStandardButtons(buttons);

	if (icon == QMessageBox::Question)
		mb.setDefaultButton(QMessageBox::No);

	if (!textYes.isEmpty() && mb.button(QMessageBox::Yes) != NULL)
		mb.button(QMessageBox::Yes)->setText(textYes);

	if (!textNo.isEmpty() && mb.button(QMessageBox::No) != NULL)
		mb.button(QMessageBox::No)->setText(textNo);

	if (!textCancel.isEmpty()  && mb.button(QMessageBox::Cancel) != NULL)
		mb.button(QMessageBox::Cancel)->setText(textCancel);

	mb.setIcon(icon);

	return mb.exec();
}

namespace Veneer
{

	int messageBoxQuestionYesNo(QWidget* parent, const QString& text, const QString& caption, const QString& textYes, const QString& textNo, const QString&)
	{
		return showBox(parent, text, caption, textYes, textNo);
	}

	int messageBoxQuestionYesNoCancel(QWidget* parent, const QString& text, const QString& caption, const QString& textYes, const QString& textNo, const QString& textCancel, const QString&)
	{
		return showBox(parent, text, caption, textYes, textNo, QMessageBox::Question, textCancel);
	}

	int messageBoxQuestionYesNoList(QWidget* parent, const QString& text, const QStringList& list, const QString& caption, const QString& textYes, const QString& textNo, const QString&)
	{
		return showBox(parent, text, caption, textYes, textNo, QMessageBox::Question, QString(), list);
	}

	void messageBoxInformation(QWidget* parent, const QString& text, const QString& caption, const QString&)
	{
		 showBox(parent, text, caption, QString(), QString(), QMessageBox::Information);
	}

	void messageBoxError(QWidget* parent, const QString& text, const QString& caption)
	{
		 showBox(parent, text, caption, QString(), QString(), QMessageBox::Critical);
	}

}
