#include "tycho.h"

static
IProjectModel *makeModel(Project *proj)
{
	switch(proj->type()) {
		case Project::Map: return new MapModel(proj);
	}
}

static
IProjectView *makeView(Project *proj)
{
	switch(proj->type()) {
		case Project::Map: return new MapView(proj);
	}
}

Project::Project(Type type) :
	QMdiSubWindow(),
	m_type(type),
	m_model(makeModel(this)),
	m_view(makeView(this))
{
	auto widget = new QWidget(this);

	setupUi(widget);

	setWidget(widget);
	setAttribute(Qt::WA_DeleteOnClose);

	listView->setModel(m_model);
	verticalLayout->insertWidget(0, m_view);

	connect(listView,
	        SIGNAL(doubleClicked(QModelIndex const &)),
	        m_model,
	        SLOT(select(QModelIndex const &)));

	dbgPrintFunc();
}

Project::~Project()
{
	dbgPrintFunc();
}

Project::Type Project::type() const
{
	return m_type;
}

IProjectModel *Project::model() const
{
	return m_model;
}

MapModel *Project::mapModel() const
{
	return qobject_cast<MapModel *>(m_model);
}

void Project::closeEvent(QCloseEvent *event)
{
	if(m_model->dirty()) {
		QMessageBox msg;
		msg.setText(tr("Do you want to save your changes to this project before closing it?"));
		msg.setInformativeText(tr("Unsaved changes will be lost unless you save."));
		msg.setStandardButtons(QMessageBox::Save |
		                       QMessageBox::Discard |
		                       QMessageBox::Cancel);
		msg.setDefaultButton(QMessageBox::Save);

		switch(msg.exec()) {
			case QMessageBox::Save:
				m_model->save();
				break;
			case QMessageBox::Discard:
				break;
			case QMessageBox::Cancel:
				event->ignore();
				return;
			default:
				Q_UNREACHABLE();
		}
	}

	event->accept();
}

// EOF
