#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);
    setFilesDisplayStyle(getInterfaceDisplay()->style);

	addGuidesTools(tabs);
    setGuidesDisplayStyle(getInterfaceDisplay()->addDisplayStyle());

    addStitchTools(tabs);
    setStitchDisplayStyle(getInterfaceDisplay()->addDisplayStyle());

    addThreadTools(tabs);
    setThreadDisplayStyle(getInterfaceDisplay()->addDisplayStyle());

    addTechnicalTools(tabs);
    setTechnicalDisplayStyle(getInterfaceDisplay()->addDisplayStyle());

    addMachineTools(tabs);
    setMachineDisplayStyle(getInterfaceDisplay()->addDisplayStyle());

    connect(tabs, SIGNAL(currentChanged(int)), this, SLOT(currentTabChanged(int)));

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

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

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

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

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


QtMaterialIconButton* OE_ui_toolBox::setIconButton(QString iconFile, const QObject *receiver = NULL, const char * method = NULL)
{
    QIcon icon;
    icon.addFile(iconFile, QSize(), QIcon::Normal, QIcon::Off);

    QtMaterialIconButton* button = new QtMaterialIconButton(icon);
    button->setUseThemeColors(false);
    button->setIconSize(QSize(30,30));
    button->setRole(Material::Primary);
    if (method)
        connect(button, SIGNAL(clicked()), receiver, method);
    return button;
}

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", getInterfaceDisplay(), SLOT(openFile())));
	toolLayout->addWidget(setButton("Append"));
    toolLayout->addWidget(setButton("Save as",":/resources/save.svg", getInterfaceDisplay(), SLOT(saveFile())));

	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", getInterfaceDisplay(), SLOT(closeSelectedCurve())));
    //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(setIconButton(":/resources/turn_left.svg"));
    turnLayout->addWidget(setIconButton(":/resources/turn_180.svg"));
    turnLayout->addWidget(setIconButton(":/resources/turn_right.svg"));

	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",":/resources/line_stitch.svg", getInterfaceDisplay(), SLOT(newLinestitch())));
    toolLayout->addWidget(setButton("New birail",":/resources/birail_stitch.svg", getInterfaceDisplay(), SLOT(newBirailstitch())));
    toolLayout->addWidget(setButton("New fill",":/resources/fill_stitch.svg", getInterfaceDisplay(), SLOT(newFillstitch())));
	//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);
    QtMaterialButton* button = new QtMaterialButton("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::setFilesDisplayStyle(OE_display::OE_displayStyle* style){};
void OE_ui_toolBox::setGuidesDisplayStyle(OE_display::OE_displayStyle* style)
{
    style->selectStitches = false;
}
void OE_ui_toolBox::setStitchDisplayStyle(OE_display::OE_displayStyle* style)
{
    style->selectCurves = false;
}
void OE_ui_toolBox::setThreadDisplayStyle(OE_display::OE_displayStyle* style){};
void OE_ui_toolBox::setTechnicalDisplayStyle(OE_display::OE_displayStyle* style){};
void OE_ui_toolBox::setMachineDisplayStyle(OE_display::OE_displayStyle* style)
{
     style->drawGrid = false;
     style->drawCurves = false;
     style->drawStitches = false;
     style->drawCommands = true;
};

OE_controller* OE_ui_toolBox::getController()
{
    return this->root->interfaceDisplay->getController();
}

OE_interfaceDisplay* OE_ui_toolBox::getInterfaceDisplay()
{
    return this->root->interfaceDisplay;
}

OE_document* OE_ui_toolBox::getDocument()
{
    return this->root->interfaceDisplay->getDocument();
}

void OE_ui_toolBox::currentTabChanged(int tabId)
{
    getInterfaceDisplay()->setDisplayStyle(tabId);
    getInterfaceDisplay()->update();
}

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

void OE_ui_toolBox::on_actionPattern1_triggered() { getInterfaceDisplay()->SetSelectedStitchPattern(0);}
void OE_ui_toolBox::on_actionPattern2_triggered() { getInterfaceDisplay()->SetSelectedStitchPattern(1);}
void OE_ui_toolBox::on_actionPattern3_triggered() { getInterfaceDisplay()->SetSelectedStitchPattern(2);}
void OE_ui_toolBox::on_actionPattern4_triggered() { getInterfaceDisplay()->SetSelectedStitchPattern(3);}
void OE_ui_toolBox::on_actionPattern5_triggered() { getInterfaceDisplay()->SetSelectedStitchPattern(4);}
void OE_ui_toolBox::on_actionPattern6_triggered() { getInterfaceDisplay()->SetSelectedStitchPattern(5);}
void OE_ui_toolBox::on_actionPattern7_triggered() { getInterfaceDisplay()->SetSelectedStitchPattern(6);}
void OE_ui_toolBox::on_actionPattern8_triggered() { getInterfaceDisplay()->SetSelectedStitchPattern(7);}
void OE_ui_toolBox::on_actionPattern9_triggered() { getInterfaceDisplay()->SetSelectedStitchPattern(8);}


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);
}


