#include "main_window.h"
#include "mesh_processor.h"

#include <QApplication>
#include <QFileDialog>
#include <QMenuBar>
#include <QGroupBox>
#include <QPushButton>


MainWindow::MainWindow(QWidget *parent)
	:QMainWindow(parent),
	 toolbar(this),
	 mesh_viewer() {
	setCentralWidget(&mesh_viewer);
	// addToolBar(Qt::RightToolBarArea, &toolbar);

	// open_action = toolbar.addAction("Ouvrir…", [&](){
	// 	emit open(QFileDialog::getOpenFileName(this, "Ouvrir un maillage"));
	// });
	// toolbar_actions.append(toolbar.addAction("Fractionner", [&](){
	// 	QVector<QPair<MyMesh::Point, MyMesh>> fragments = shatter(mesh);
	// 	mesh_viewer.removeOpenGLMesh(glm);
	// 	for (auto &[pos, fragment] : fragments) {
	// 		fragment.triangulate();
	// 		QMatrix4x4 mat;
	// 		float scale = 1.2;
	// 		mat.translate(pos[0] * scale, pos[1] * scale, pos[2] * scale);
	// 		mesh_viewer.addOpenGLMeshFromOpenMesh(&fragment, mat);
	// 	}
	// }));

	QMenuBar *menu_bar = new QMenuBar();
	setMenuBar(menu_bar);

	// File menu
	QMenu *file_menu = new QMenu("Fichier");
	open_action = file_menu->addAction("Ouvrir…", [&](){
		emit open(QFileDialog::getOpenFileName(this, "Ouvrir un maillage"));
	});
	save_action = file_menu->addAction("Enregistrer sous…", [&]() {
		emit save(QFileDialog::getSaveFileName(this,
											   "Enregistrer un maillage"));
	});
	menu_bar->addMenu(file_menu);
	if (!mesh_viewer.isInitialized()) {
		open_action->setEnabled(false);
		connect(&mesh_viewer, &MeshViewer::initialized, [&]() {
			open_action->setEnabled(true);
		});
	}

	addToolBar(Qt::RightToolBarArea, &toolbar);

	// Hole filling tools
	QGroupBox *hole_box = new QGroupBox("Remplissage de trous");
	QLayout *hole_vbox = new QVBoxLayout();
	hole_box->setLayout(hole_vbox);
	QPushButton *fill_holes_dumb = new QPushButton("Remplir bêtement");
	connect(fill_holes_dumb, &QPushButton::clicked,
			this, &MainWindow::fillHolesDumbClicked);
	hole_vbox->addWidget(fill_holes_dumb);
	QPushButton *fill_holes_implicit =
		new QPushButton("Remplir par une surface implicite");
	connect(fill_holes_implicit, &QPushButton::clicked,
			this, &MainWindow::fillHolesImplicitClicked);
	hole_vbox->addWidget(fill_holes_implicit);
	toolbar.addWidget(hole_box);

	// Smoothing tools
	QGroupBox *smooth_box = new QGroupBox("Adoucissement");
	QLayout *smooth_vbox = new QVBoxLayout();
	smooth_box->setLayout(smooth_vbox);
	QPushButton *smooth = new QPushButton("Adoucir");
	connect(smooth, &QPushButton::clicked,
			this, &MainWindow::smoothClicked);
	smooth_vbox->addWidget(smooth);
	toolbar.addWidget(smooth_box);

	// Curvature tools
	QGroupBox *curvature_box = new QGroupBox("Analyse de courbure");
	QLayout *curvature_vbox = new QVBoxLayout();
	curvature_box->setLayout(curvature_vbox);
	QPushButton *patch_mode = new QPushButton(
		"Afficher le patch de la sélection");
	patch_mode->setCheckable(true);
	connect(patch_mode, &QPushButton::toggled,
			this, &MainWindow::patchViewToggled);
	curvature_vbox->addWidget(patch_mode);
	toolbar.addWidget(curvature_box);
}
