#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "main.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
	ui->setupUi(this);
	addTab();
	ui->tabWidget->removeTab(0);
}

MainWindow::~MainWindow()
{
	delete ui;
}

void MainWindow::keyPressEvent(QKeyEvent* event)
{
	QWidget* widget = ui->tabWidget->currentWidget();
	OE_root* root = static_cast<OE_root*>(widget->property("OEroot").value<void*>());
	root->display->keyPressEvent(event);
}

void MainWindow::keyReleaseEvent(QKeyEvent* event)
{
	QWidget* widget = ui->tabWidget->currentWidget();
	OE_root* root = static_cast<OE_root*>(widget->property("OEroot").value<void*>());
	root->display->keyReleaseEvent(event);
}

void MainWindow::addTab()
{
	QWidget* newtab = new QWidget();
	newtab->setObjectName(QStringLiteral("newtab"));
	QGridLayout * newGridLayout = new QGridLayout(newtab);
	newGridLayout->setSpacing(6);
	newGridLayout->setContentsMargins(11, 11, 11, 11);
	newGridLayout->setObjectName(QStringLiteral("newGridLayout"));
	newGridLayout->setHorizontalSpacing(0);
	newGridLayout->setContentsMargins(0, 0, 0, 0);
	OE_interfaceDisplay* newOpenGLWidget = new OE_interfaceDisplay(newtab);
	newOpenGLWidget->setObjectName(QStringLiteral("newOpenGLWidget"));
	newOpenGLWidget->setMouseTracking(true);
	newOpenGLWidget->setAcceptDrops(true);
	newGridLayout->addWidget(newOpenGLWidget, 0, 0, 1, 1);
	int newIndex = ui->tabWidget->addTab(newtab, QString("newtab"));
	ui->tabWidget->setTabText(ui->tabWidget->indexOf(newtab), QApplication::translate("MainWindow", "Design", nullptr));

	OE_root* newRoot = new OE_root();
	  OE_document* document = new OE_document();
	newRoot->interfaceDisplay = newOpenGLWidget;
	  newRoot->interfaceDisplay->setDocument(document);
	newRoot->display = newRoot->interfaceDisplay;
	  newRoot->controller = new OE_controller(newRoot->display, document);
	newRoot->controller->initNewDocument();
	newRoot->display->setController(newRoot->controller);
	QVariant rootPointerProperty(QVariant::fromValue(static_cast<void*>(newRoot)));
	ui->tabWidget->widget(newIndex)->setProperty("OEroot", rootPointerProperty);

	ui->tabWidget->setCurrentIndex(newIndex);
	update();
}

OE_interfaceDisplay* MainWindow::getItfDisplay()
{
	QWidget* widget = ui->tabWidget->currentWidget();
	OE_root* root = static_cast<OE_root*>(widget->property("OEroot").value<void*>());
	return root->interfaceDisplay;
}

void MainWindow::sendKeyPulse(Qt::Key key, Qt::KeyboardModifiers modifiers)
{
	QKeyEvent fakeKeyPress(QKeyEvent::Type::KeyPress, key, modifiers);
	getItfDisplay()->key(&fakeKeyPress);
	QKeyEvent fakeKeyRelease(QKeyEvent::Type::KeyRelease, key, modifiers);
	getItfDisplay()->key(&fakeKeyRelease);
}

void MainWindow::on_tabWidget_tabCloseRequested(int index)
{
	// TODO some brain and warn if loss of data
	if (ui->tabWidget->count() == 1)
		addTab();
	QWidget* widget = ui->tabWidget->widget(index);
	OE_root* root = static_cast<OE_root*>(widget->property("OEroot").value<void*>());
	delete root->controller->getDocument();
	delete root->controller;
	delete root;
	ui->tabWidget->removeTab(index);
}

void MainWindow::on_actionNew_triggered()
{
	addTab();
}

void MainWindow::on_actionClose_triggered()
{
	on_tabWidget_tabCloseRequested(ui->tabWidget->currentIndex());
}

void MainWindow::on_actionOpen_triggered()
{ // TODO real call to a load function, not a fake key event
	sendKeyPulse(Qt::Key_O, Qt::KeyboardModifier::ControlModifier);
}

void MainWindow::on_actionSave_triggered()
{ // TODO real call to a save function, not a fake key event
	sendKeyPulse(Qt::Key_S, Qt::KeyboardModifier::ControlModifier);
}

void MainWindow::on_actionLine_triggered()
{ // TODO real call to a save function, not a fake key event
	sendKeyPulse(Qt::Key_L, Qt::KeyboardModifier::NoModifier);
}

void MainWindow::on_actionUndo_triggered()
{
	sendKeyPulse(Qt::Key_Z, Qt::KeyboardModifier::ControlModifier);
}

void MainWindow::on_actionRedo_triggered()
{
	sendKeyPulse(Qt::Key_Z, Qt::KeyboardModifier::ControlModifier|Qt::KeyboardModifier::ShiftModifier);
}

void MainWindow::on_actionBirail_triggered()
{
	sendKeyPulse(Qt::Key_B, Qt::KeyboardModifier::NoModifier);
}

void MainWindow::on_actionPattern1_triggered() { sendKeyPulse(Qt::Key_1, Qt::KeyboardModifier::KeypadModifier); }
void MainWindow::on_actionPattern2_triggered() { sendKeyPulse(Qt::Key_2, Qt::KeyboardModifier::KeypadModifier); }
void MainWindow::on_actionPattern3_triggered() { sendKeyPulse(Qt::Key_3, Qt::KeyboardModifier::KeypadModifier); }
void MainWindow::on_actionPattern4_triggered() { sendKeyPulse(Qt::Key_4, Qt::KeyboardModifier::KeypadModifier); }
void MainWindow::on_actionPattern5_triggered() { sendKeyPulse(Qt::Key_5, Qt::KeyboardModifier::KeypadModifier); }
void MainWindow::on_actionPattern6_triggered() { sendKeyPulse(Qt::Key_6, Qt::KeyboardModifier::KeypadModifier); }
void MainWindow::on_actionPattern7_triggered() { sendKeyPulse(Qt::Key_7, Qt::KeyboardModifier::KeypadModifier); }
void MainWindow::on_actionPattern8_triggered() { sendKeyPulse(Qt::Key_8, Qt::KeyboardModifier::KeypadModifier); }
void MainWindow::on_actionPattern9_triggered() { sendKeyPulse(Qt::Key_9, Qt::KeyboardModifier::KeypadModifier); }

void MainWindow::on_tabWidget_currentChanged(int index)
{
	if (index >= 0)
	{ // Discard previous modifiers on that widget
		QKeyEvent fakeKey(QKeyEvent::Type::KeyRelease, Qt::Key_Escape, Qt::KeyboardModifier::NoModifier);
		getItfDisplay()->key(&fakeKey);
	}
}

void MainWindow::on_actionCloseCurve_triggered()
{
	sendKeyPulse(Qt::Key_C, Qt::KeyboardModifier::NoModifier);
}

void MainWindow::on_actionReverse_curve_R_triggered()
{
	sendKeyPulse(Qt::Key_R, Qt::KeyboardModifier::NoModifier);
}

void MainWindow::on_actionFill_triggered()
{
	sendKeyPulse(Qt::Key_F, Qt::KeyboardModifier::NoModifier);
}
