#include "tycho.h"

Project::Project(ProjectModelType type) :
   QMdiSubWindow(),
   model(nullptr),
   view(nullptr)
{
   auto widget = new QWidget(this);

   setupUi(widget);

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

   switch(type) {
      case ProjectModelType::Map:
         model.reset(new MapModel(this));
         view = new MapView(getMapModel(), this);
         break;
   }

   listView->setModel(dynamic_cast<QAbstractItemModel *>(model.get()));
   verticalLayout->insertWidget(0, view);

   dbgPrintFunc();
}

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

std::shared_ptr<ProjectModel> Project::getModel()
{
   return model;
}

std::shared_ptr<MapModel> Project::getMapModel()
{
   return std::dynamic_pointer_cast<MapModel>(model);
}

void Project::closeEvent(QCloseEvent *event)
{
   if(model->isDirty()) {
      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:
            model->save();
            break;
         case QMessageBox::Discard:
            break;
         case QMessageBox::Cancel:
            event->ignore();
            return;
         default:
            Q_UNREACHABLE();
      }
   }

   event->accept();
}

// EOF
