#include "main_window.h"
#include "mesh_processor.h"
#include <iostream>
#include <QSurfaceFormat>
#include <QApplication>


int main(int argc, char *argv[]) {
	using namespace std;
	QSurfaceFormat format;
	format.setRenderableType(QSurfaceFormat::OpenGL);
#ifndef QT_DEBUG
	qDebug("Debug build");
	format.setOption(QSurfaceFormat::DebugContext);
#endif
	QSurfaceFormat::setDefaultFormat(format);
	QApplication app(argc, argv);
	MeshProcessor *mesh_processor = nullptr;
	MainWindow main_window;
	MeshViewer *mesh_viewer = &main_window.mesh_viewer;
	QObject::connect(mesh_viewer, &MeshViewer::initialized,
					 [&]() {
						 if (mesh_processor) {
							 mesh_viewer->addMesh(mesh_processor->mesh);
							 mesh_viewer->addMesh(mesh_processor->patch);
						 }
					 });

	if (argc > 2) {
		qWarning("Utilisation : %s [MAILLAGE]", argv[0]);
		return 1;
	} else if (argc == 2) {
		mesh_processor = new MeshProcessor(argv[1]);
	}
	QObject::connect(&main_window, &MainWindow::open,
					 [&](const QString &path) {
						 if (mesh_processor) {
							 mesh_viewer->removeMesh(mesh_processor->mesh);
							 delete mesh_processor;
						 }
						 mesh_processor = new MeshProcessor(path);
						 mesh_viewer->addMesh(mesh_processor->mesh);
					 });
	main_window.show();
	return app.exec();
}
