#include "OE_ui_toolbox.h"
#include <QtWidgets/QFormLayout>
#include <qtmaterialtooltabs_internal.h>
#include "ui_mainwindow.h"

OE_ui_toolBox::OE_ui_toolBox(MainWindow* window, OE_root* root)
{
	this->root = root;
	this->window = window;
	buttonFont = QFont("Roboto", 8, QFont::Medium);

	setFixedWidth(150);
	setSizePolicy(QSizePolicy ::Fixed , QSizePolicy ::Expanding );

	QVBoxLayout *drawerLeftLayout = new QVBoxLayout;
	setLayout(drawerLeftLayout);
	drawerLeftLayout->setMargin(0);
	drawerLeftLayout->setContentsMargins(0,0,0,0);

	QtMaterialToolTabs *tabs = new QtMaterialToolTabs;

	tabs->setSizePolicy(QSizePolicy ::Expanding , QSizePolicy ::Expanding );
	drawerLeftLayout->addWidget(tabs);
	tabs->setFont(buttonFont);

	addFilesTools(tabs);
	addGuidesTools(tabs);
	addStitchTools(tabs);
	addThreadTools(tabs);
	addTechnicalTools(tabs);
	addMachineTools(tabs);


	drawerLeftLayout->setStretch(0,1);
	setContentsMargins(0, 0, 0, 0);
}

QtMaterialFlatButton* OE_ui_toolBox::setButton(QString name, const QObject *receiver = NULL, const char * method = NULL)
{
	QtMaterialFlatButton* button = new QtMaterialFlatButton(name);
	button->setFont(buttonFont);
	button->setUseThemeColors(true);

	button->setHaloVisible(false);
	button->setTextAlignment(Qt::AlignLeft);

	button->setMinimumHeight(0);
	if (method)
        connect(button, SIGNAL(clicked(bool)), receiver, method);
	return button;
}

QtMaterialFlatButton* OE_ui_toolBox::setButton(QString name,QString iconFile, const QObject *receiver = NULL, const char * method = NULL)
{
    QtMaterialFlatButton* newButton = setButton(name, receiver, method);
	QIcon icon;
	icon.addFile(iconFile, QSize(), QIcon::Normal, QIcon::Off);
	newButton->setIcon(icon);
	//newButton->setIconPlacement(Material::ButtonIconPlacement::RightIcon);
	return newButton;
}

void OE_ui_toolBox::addFilesTools(QtMaterialToolTabs *tabs)
{
	QtMaterialToolTab* curTab  = new QtMaterialToolTab("Files");
	tabs->addTab(curTab);
	QVBoxLayout *toolLayout = new QVBoxLayout(curTab->content());
	toolLayout->setMargin(0);
	toolLayout->setSpacing(0);
	toolLayout->setContentsMargins(0,0,0,0);

    toolLayout->addWidget(setButton("New",":/resources/new.svg", this, SLOT(on_actionNew_triggered())));
    toolLayout->addWidget(setButton("Open",":/resources/open.svg", this, SLOT(on_actionOpen_triggered())));
	toolLayout->addWidget(setButton("Append"));
    toolLayout->addWidget(setButton("Save as",":/resources/save.svg", this, NULL));

	toolLayout->addStretch();
}

void OE_ui_toolBox::addGuidesTools(QtMaterialToolTabs *tabs)
{
	QtMaterialToolTab* curTab  = new QtMaterialToolTab("Curves");
	tabs->addTab(curTab);
	QVBoxLayout *toolLayout = new QVBoxLayout(curTab->content());
	toolLayout->setMargin(0);
	toolLayout->setSpacing(0);
	toolLayout->setContentsMargins(0,0,0,0);

    toolLayout->addWidget(setButton("Close", this, SLOT(on_actionCloseCurve_triggered())));
    //toolLayout->addWidget(setButton("Reverse", this, SLOT(on_actionReverse_curve_R_triggered())));

	QLabel* label = new QLabel("Turn: ");
	label->setFont(buttonFont);
	label->setAlignment(Qt::AlignCenter);
	toolLayout->addWidget(label);
    QHBoxLayout *turnLayout = new QHBoxLayout();//curTab->content());
	toolLayout->addLayout(turnLayout);
	turnLayout->addWidget(setButton("Right"));
	turnLayout->addWidget(setButton("Left"));
	turnLayout->addWidget(setButton("360°"));

	toolLayout->addWidget(setButton("Add curve"));


	toolLayout->addStretch();
}

void OE_ui_toolBox::addStitchTools(QtMaterialToolTabs *tabs)
{
	QtMaterialToolTab* curTab  = new QtMaterialToolTab("Stitches");
	tabs->addTab(curTab);
	QVBoxLayout *toolLayout = new QVBoxLayout(curTab->content());
	toolLayout->setMargin(0);
	toolLayout->setSpacing(0);
	toolLayout->setContentsMargins(0,0,0,0);

    toolLayout->addWidget(setButton("New line", this, SLOT(on_actionLine_triggered())));
    toolLayout->addWidget(setButton("New birail", this, SLOT(on_actionBirail_triggered())));
    toolLayout->addWidget(setButton("New fill", this, SLOT(on_actionFill_triggered())));
	//toolLayout->addWidget(setButton("Reverse selected", SLOT(on_actionReverse_curve_R_triggered())));

	toolLayout->addStretch();
	QLabel* label = new QLabel("-- favorite Paterns --");
	label->setFont(buttonFont);
	label->setAlignment(Qt::AlignCenter);
	toolLayout->addWidget(label);
	QGridLayout * paternLayout = new QGridLayout();
	toolLayout->addLayout(paternLayout);
    paternLayout->addWidget(setButton("1", this, SLOT(on_actionPattern1_triggered())),0,0,1,1);
    paternLayout->addWidget(setButton("2", this, SLOT(on_actionPattern2_triggered())),0,1,1,1);
    paternLayout->addWidget(setButton("3", this, SLOT(on_actionPattern3_triggered())),0,2,1,1);
	/*paternLayout->addWidget(setButton("3",SLOT(on_actionPattern3_triggered())),0,2,1,1);
	QtMaterialFlatButton* button = new QtMaterialFlatButton("3");
	button->setFont(buttonFont);
	button->setUseThemeColors(true);

	button->setHaloVisible(false);
	button->setTextAlignment(Qt::AlignLeft);

	button->setMinimumHeight(0);
	button->addAction(window->actionPattern3);*/

    paternLayout->addWidget(setButton("4", this, SLOT(on_actionPattern4_triggered())),0,3,1,1);
    paternLayout->addWidget(setButton("5", this, SLOT(on_actionPattern5_triggered())),0,4,1,1);
    paternLayout->addWidget(setButton("6", this, SLOT(on_actionPattern6_triggered())),1,0,1,1);
    paternLayout->addWidget(setButton("7", this, SLOT(on_actionPattern7_triggered())),1,1,1,1);
    paternLayout->addWidget(setButton("8", this, SLOT(on_actionPattern8_triggered())),1,2,1,1);
    paternLayout->addWidget(setButton("9", this, SLOT(on_actionPattern9_triggered())),1,3,1,1);
    paternLayout->addWidget(setButton("10", this, SLOT(on_actionPattern9_triggered())),1,4,1,1);
}

void OE_ui_toolBox::addThreadTools(QtMaterialToolTabs *tabs)
{
	QtMaterialToolTab* curTab  = new QtMaterialToolTab("Threads");
	tabs->addTab(curTab);

    //curTab->button()->setIcon(icon);
    //curTab->button()->setIconSize(QSize(22, 22));

	QVBoxLayout *toolLayout = new QVBoxLayout(curTab->content());
	toolLayout->setMargin(0);
	toolLayout->setSpacing(0);
	toolLayout->setContentsMargins(0,0,0,0);

	toolLayout->addWidget(setButton("New thread from lib"));
	toolLayout->addStretch();
	QLabel* label = new QLabel("-- Used threads --");
	label->setFont(buttonFont);
	label->setAlignment(Qt::AlignCenter);
	toolLayout->addWidget(label);
	QGridLayout * paternLayout = new QGridLayout();
	toolLayout->addLayout(paternLayout);
    paternLayout->addWidget(setButton("1", this, SLOT(on_actionPattern1_triggered())),0,0,1,1);
    paternLayout->addWidget(setButton("2", this, SLOT(on_actionPattern2_triggered())),0,1,1,1);
    paternLayout->addWidget(setButton("3", this, SLOT(on_actionPattern3_triggered())),0,2,1,1);
    paternLayout->addWidget(setButton("4", this, SLOT(on_actionPattern4_triggered())),0,3,1,1);
    paternLayout->addWidget(setButton("5", this, SLOT(on_actionPattern5_triggered())),0,4,1,1);
    paternLayout->addWidget(setButton("6", this, SLOT(on_actionPattern6_triggered())),1,0,1,1);
    paternLayout->addWidget(setButton("+", this, SLOT(on_actionPattern5_triggered())),1,1,1,1);

}
void OE_ui_toolBox::addTechnicalTools(QtMaterialToolTabs *tabs)
{
	QtMaterialToolTab* curTab  = new QtMaterialToolTab("Technical");
	tabs->addTab(curTab);
	QVBoxLayout *toolLayout = new QVBoxLayout(curTab->content());
	toolLayout->setMargin(0);
	toolLayout->setSpacing(0);
	toolLayout->setContentsMargins(0,0,0,0);

	toolLayout->addWidget(setButton("underlay"));
	toolLayout->addWidget(setButton("Push/pull"));
	toolLayout->addWidget(setButton("cut link"));
	toolLayout->addWidget(setButton("set speed"));

	toolLayout->addStretch();
}

void OE_ui_toolBox::addMachineTools(QtMaterialToolTabs *tabs)
{
	QtMaterialToolTab* curTab  = new QtMaterialToolTab("Machine control");
	tabs->addTab(curTab);
	QVBoxLayout *toolLayout = new QVBoxLayout(curTab->content());
	toolLayout->setMargin(0);
	toolLayout->setSpacing(0);
	toolLayout->setContentsMargins(0,0,0,0);

    toolLayout->addWidget(setButton("Connect", window, SLOT(on_actionConnectToMachine_triggered(bool))));
    toolLayout->addWidget(setButton("Push", this, SLOT(on_actionMachineLoad_triggered())));
    toolLayout->addWidget(setButton("Run", this, SLOT(on_actionMachineRun_triggered())));

	toolLayout->addStretch();


    connect(window, SIGNAL(machineConnectionChanged(bool)), this, SLOT(on_machineConnectionChanged(bool)));
}

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

void OE_ui_toolBox::on_actionNew_triggered()
{
	window->addTab();
}

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

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

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

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

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

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

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

/*void OE_ui_toolBox::on_actionReverse_curve_R_triggered()
{
	sendKeyPulse(Qt::Key_R, Qt::KeyboardModifier::NoModifier);
}*/

void OE_ui_toolBox::on_actionMachineRun_triggered()
{
	window->machineRun();
}

void OE_ui_toolBox::on_actionMachineLoad_triggered()
{
	window->machineLoad();
}

void OE_ui_toolBox::on_machineConnectionChanged(bool connected)
{
//	ui->actionMachineRun->setEnabled(connected);
//	ui->actionMachineLoad->setEnabled(connected);
}


