/*
* This file is part of project OpenEmbroidery. It's copyrighted by
* the contributors recorded in the version control history of the file.
* Original project location https://code.electrolab.fr/openEmbroidery/openEmbroidery_software
*
* SPDX-License-Identifier: CECILL-2.1
* License-Filename: Licence_CeCILL_V2.1-en.txt
*/

#include "OE_controller.h"
#include <iostream>
#include <typeinfo>

#include "actions/OE_actionsCurves.h"
#include "actions/OE_actionsStitchs.h"
#include "actions/OE_actionsLineStitchs.h"
#include "actions/OE_actionsBirailStitchs.h"
#include "actions/OE_actionsFillStitchs.h"
#include "actions/OE_actionsLinkStitchs.h"
#include "actions/OE_actionsSelection.h"
#include "actions/OE_actionsThreads.h"

#include <machine.h>

// comm("/dev/ttyUSB0", 80, 10)
// comm("/dev/tty", 80, 10)
// comm("20:15:05:11:28:40", 80, 30)
// comm("/dev/rfcomm1", 80, 30)
OE_controller::OE_controller(OE_display* display, OE_document* document) : display(display), document(document)
{
	//OE_linestitch::initPatterns();

	// TODO remove
	if (document && document->threads.empty())
	{
		document->addThread(new OE_thread(OE_color(.4, .4, .4), 2));
	}
}

OE_controller::~OE_controller()
{
}

void OE_controller::setDocument(OE_document* document)
{
	this->document = document;
}

/*
void OE_controller::newSubCurve(OE_curve* curve, float curveStart, float curveEnd, bool reverse)
{
	addAction(new OE_actionNewSubCurve(curve, curveStart, curveEnd, reverse));
}

void OE_controller::newSubCurve(int ncurve, float curveStart, float curveEnd, bool reverse)
{
	newSubCurve(getCurve(ncurve), curveStart, curveEnd, reverse);
}
*/

void OE_controller::newBirailStitch(bool reverse1, bool reverse2, float offset1, float offset2, float len)
{
	clearUndoActionStack();
	addAction(new OE_actionNewBirailStitch(document, reverse1, reverse2, offset1, offset2, len));
}

void OE_controller::newLineStitch(float len, float width, OE_pattern* pattern, unsigned patternSteps, float xOffset, float yOffset)
{
	clearUndoActionStack();
	addAction(new OE_actionNewLineStitch(document, len, width, pattern, patternSteps, xOffset, yOffset));
}

void OE_controller::newFillStitch(float len, float width, OE_pattern* pattern)
{
	clearUndoActionStack();
	addAction(new OE_actionNewFillStitch(document, len, width, pattern));
}

void OE_controller::setStitchThread(OE_stitchs* stitch, OE_thread* thread)
{
	addAction(new OE_actionSetStitchThread(document, stitch, thread));
}

void OE_controller::setStitchThread(int nstitch, int nthread)
{
	setStitchThread(getStitch(nstitch), getThread(nthread));
}

void OE_controller::setCloseCurve(OE_curve* curve, bool closed)
{
	addAction(new OE_actionSetCloseCurve(document, curve, closed));
}

void OE_controller::setCloseCurve(int ncurve, bool closed)
{
	setCloseCurve(getCurve(ncurve), closed);
}

void OE_controller::toggleCloseSelectedCurve()
{
	OE_curve* curve = document->selectedCurves.back();
	if (curve)
	{
		setCloseCurve(curve, !curve->getClosed());
	}
}

bool OE_controller::initNewDocument()
{
	if (!document)
	{
		return false;
	}

	addAction(new OE_actionNewThread(document, new OE_thread(OE_color(.9, .5, .1), 2)));

	document->setZeroPoint(vector_2d(0, 0));
	return true;
}

bool OE_controller::setDefault()
{
	OE_birailstitch::defaultLen = 1;
	OE_birailstitch::defaultMaxlen = 0;
	return true;
}

OE_curve* OE_controller::getCurve(int index)
{
	if (document && index<0)
	{
		index = document->curves.size() + index;
	}
	if (document && 0<=index && index<document->curves.size())
	{
		std::list<OE_pointcurve*>::iterator curve = document->curves.begin();
		for (unsigned i=0; i<index; i++)
		{
			curve++;
		}
		return *curve;
	}

	return nullptr;
}

OE_pointcurve* OE_controller::getPointcurve(int index)
{
	return dynamic_cast<OE_pointcurve*>(getCurve(index));
}

OE_subcurve* OE_controller::getSubcurve(int index)
{
	return dynamic_cast<OE_subcurve*>(getCurve(index));
}

OE_joincurve* OE_controller::getJoincurve(int index)
{
	return dynamic_cast<OE_joincurve*>(getCurve(index));
}

OE_stitchs* OE_controller::getStitch(int index)
{
	if (document && index<0)
	{
		index = document->stitchs.size() + index;
	}
	if (document && 0<=index && index<document->stitchs.size())
	{
		std::list<OE_stitchs*>::iterator stitch = document->stitchs.begin();
		for (unsigned i=0; i<index;i++)
		{
			stitch++;
		}
		return *stitch;
	}

	return nullptr;
}

OE_linestitch* OE_controller::getLineStitch(int index)
{
	return dynamic_cast<OE_linestitch*>(getStitch(index));
}

OE_birailstitch* OE_controller::getBirailStitch(int index)
{
	return dynamic_cast<OE_birailstitch*>(getStitch(index));
}

OE_fillstitch* OE_controller::getFillStitch(int index)
{
	return dynamic_cast<OE_fillstitch*>(getStitch(index));
}

OE_linkstitch* OE_controller::getLinkStitch(int index)
{
	return dynamic_cast<OE_linkstitch*>(getStitch(index));
}

OE_thread* OE_controller::getThread(int index)
{
	if (document && index<0)
	{
		index = document->threads.size() + index;
	}
	if (document && 0<=index && index<document->threads.size())
	{
		std::list<OE_thread*>::iterator thread = document->threads.begin();
		for (int i=0; i<index; i++)
		{
			thread++;
		}
		return *thread;
	}

	return nullptr;
}


OE_pattern* OE_controller::getPattern(int index)
{
	if (document)
	{
		if (index<0)
		{
			index += document->patterns.size();
		}

		if (0<=index && index<document->patterns.size())
		{
			std::list<OE_pattern*>::iterator pattern = document->patterns.begin();
			for (int i=0; i<index; i++)
			{
				pattern++;
			}
			return *pattern;
		}
	}
	return nullptr;
}

void OE_controller::clearUndoActionStack()
{
	if (document)
	{
		OE_document::ScopeLock lock(*document);
		//flush the undoActionStack
		while (!document->undoActionsStack.empty())
		{
			delete document->undoActionsStack.back();
			document->undoActionsStack.pop_back();
		}
	}
}

void OE_controller::addAction(OE_actions* action)
{
	if (document)
	{
		OE_document::ScopeLock lock(*document);
		//flush the undoActionStack
		while (!(document->undoActionsStack.empty()))
		{
			delete document->undoActionsStack.back();
			document->undoActionsStack.pop_back();
		}

		//push the new action to the active stack
		document->activeActionsStack.push_front(action);
	}
}

void OE_controller::undoAction()
{
	if (document)
	{
		OE_document::ScopeLock lock(*document);
		if (document->activeActionsStack.size())
		{
			// get and undo the last action from active action stack
			OE_actions* action = *document->activeActionsStack.begin();
			if (action)
			{
				action->undo();

				// move the action from active to undo action stack
				document->activeActionsStack.pop_front();
				document->undoActionsStack.push_front(action);
			}
		}
	}
}

void OE_controller::redoAction()
{
	if (document)
	{
		OE_document::ScopeLock lock(*document);
		if (document->undoActionsStack.size())
		{
			// get and redo the last action from undo action stack
			OE_actions* action = *document->undoActionsStack.begin();

			action->redo();

			// move the action from undo to active action stack
			document->undoActionsStack.pop_front();
			document->activeActionsStack.push_front(action);
		}
	}
}

bool OE_controller::selectCurve(OE_pointcurve* curve, bool replace)
{
	std::list<OE_pointcurve*> tmp;
	tmp.push_back(curve);

	return selectCurves(tmp, replace);
}

bool OE_controller::selectCurves(std::list<OE_pointcurve*> curves, bool replace)
{
	if (document)
	{
		addAction(new OE_actionSelectionAddCurves(document, curves, replace));
		return true;
	}
	return false;
}

bool OE_controller::unselectCurves(std::list<OE_pointcurve*> curves)
{
	if (document)
	{
		addAction(new OE_actionSelectionRemoveCurves(document, curves));
		return true;
	}
	return false;
}

bool OE_controller::clearSelectCurves()
{
	if (document)
	{
		addAction(new OE_actionSelectionClearCurves(document));
		return true;
	}
	return false;
}

bool OE_controller::selectStitch(OE_stitchs* stitch, bool replace)
{
	std::list<OE_stitchs*> tmp;
	tmp.push_back(stitch);

	return selectStitches(tmp, replace);
}

bool OE_controller::selectStitches(std::list<OE_stitchs*> stitches, bool replace)
{
	if (document)
	{
		addAction(new OE_actionSelectionAddStitches(document, stitches, replace));
		return true;
	}
	return false;
}

bool OE_controller::selectStitches(std::list<int> stitchesId, bool replace)
{
    if (document)
    {
        std::list<OE_stitchs*> stitches;

        std::list<int>::iterator stitchId = stitchesId.begin();
        while (stitchId != stitchesId.end())
        {
            auto l_front = document->stitchs.begin();
            std::advance(l_front, *stitchId);
            stitches.push_back(*l_front);
            stitchId++;
        }
        addAction(new OE_actionSelectionAddStitches(document, stitches, replace));
        return true;
    }
    return false;
}

bool OE_controller::unselectStitches(std::list<OE_stitchs*> stitches)
{
	if (document)
	{
		addAction(new OE_actionSelectionRemoveStitches(document, stitches));
		return true;
	}
	return false;
}

bool OE_controller::clearSelectStitches()
{
	if (document)
	{
		addAction(new OE_actionSelectionClearStitches(document));
		return true;
	}
	return false;
}

bool OE_controller::clearSelection()
{
	if (document)
	{
		addAction(new OE_actionSelectionClear(document));
		return true;
	}
	return false;
}

BoundingBox OE_controller::getSelectionBoundingBox()
{
	BoundingBox boundingBox;
	if (!document->selectedCurves.empty() or !document->selectedStitchs.empty())
	{
		for (auto curve : document->selectedCurves)
		{
			boundingBox += curve->getBound();
		}
		for (auto stitch : document->selectedStitchs)
		{
			boundingBox += stitch->getBound();
		}
	}
	else
	{
		// Fallback to all curves bounding box
		for (auto curve : document->curves)
		{
			boundingBox += curve->getBound();
		}
		for (auto stitch : document->stitchs)
		{
			boundingBox += stitch->getBound();
		}
	}
	return boundingBox;
}

bool OE_controller::moveInListSelectedStitches(int posPrevious)
{
    if (document)
    {
        addAction(new OE_actionMoveInListSelectedStitches(document, posPrevious));
        return true;
    }
    return false;
}

OE_actions* OE_controller::getLastAction()
{
	if (document && document->activeActionsStack.size())
	{
		return *document->activeActionsStack.begin();
	}
	return nullptr;
}

bool OE_controller::editActionMovePointCurve(vector_2d offset)
{
	OE_actionMovePointCurve* actionMovePointCurve = dynamic_cast<OE_actionMovePointCurve*>(getLastAction());
	if (actionMovePointCurve)
	{
		actionMovePointCurve->setMove(offset);
		return true;
	}
	return false;
}

bool OE_controller::editActionMovePointLinkStitch(vector_2d offset)
{
	OE_actionAddPointLinkStitch* actionAddPointLinkStitch = dynamic_cast<OE_actionAddPointLinkStitch*>(getLastAction());
	OE_actionMovePointLinkStitch* actionMovePointLinkStitch = dynamic_cast<OE_actionMovePointLinkStitch*>(getLastAction());
	if (actionAddPointLinkStitch)
	{
		actionAddPointLinkStitch->setOffset(offset);
		return true;
	}
	else if (actionMovePointLinkStitch)
	{
		actionMovePointLinkStitch->setOffset(offset);
		return true;
	}

	return false;
}

bool OE_controller::editActionMoveGridPointFillStitch(vector_2d offset)
{
	OE_actionMovePointFillStitch* actionMovePointFillStitch = dynamic_cast<OE_actionMovePointFillStitch*>(getLastAction());
	if (actionMovePointFillStitch)
	{
		actionMovePointFillStitch->setOffset(offset);
		return true;
	}

	return false;
}


bool OE_controller::editActionMoveSelection(vector_2d offset)
{
	OE_actionMoveSelection* actionMoveSelection = dynamic_cast<OE_actionMoveSelection*>(getLastAction());
	if (actionMoveSelection)
	{
		actionMoveSelection->setMove(offset);
		return true;
	}
	return false;
}

bool OE_controller::editActionScaleSelection(vector_2d ratio)
{
	OE_actionScaleSelection* actionScaleSelection = dynamic_cast<OE_actionScaleSelection*>(getLastAction());
	if (actionScaleSelection)
	{
		actionScaleSelection->setScale(ratio);
		return true;
	}
	return false;
}

bool OE_controller::editActionSetSubcurvePos(float val)
{
	OE_actionSetSubcurvePos* actionSetSubcurvePos = dynamic_cast<OE_actionSetSubcurvePos*>(getLastAction());
	if (actionSetSubcurvePos)
	{
		actionSetSubcurvePos->setPos(val);
		return true;
	}
	return false;
}

bool OE_controller::editActionSetSubcurvePosSwitchDir()
{
	OE_actionSetSubcurvePos* actionJoincurveSetSubcurve = dynamic_cast<OE_actionSetSubcurvePos*>(getLastAction());
	if (actionJoincurveSetSubcurve)
	{
		actionJoincurveSetSubcurve->setInvertDir(!actionJoincurveSetSubcurve->getInvertDir());
		return true;
	}
	return false;
}

bool OE_controller::editActionAddSubcurvePosEnd(float val)
{
	OE_actionJoincurveAddSubCurve* actionJoincurveAddSubcurve = dynamic_cast<OE_actionJoincurveAddSubCurve*>(getLastAction());
	if (actionJoincurveAddSubcurve)
	{
		actionJoincurveAddSubcurve->setEnd(val);
		return true;
	}
	return false;
}

bool OE_controller::editActionAddSubcurvePosSwitchDir()
{
	OE_actionJoincurveAddSubCurve* actionJoincurveAddSubcurve = dynamic_cast<OE_actionJoincurveAddSubCurve*>(getLastAction());
	if (actionJoincurveAddSubcurve)
	{
		actionJoincurveAddSubcurve->setDir(!actionJoincurveAddSubcurve->getDir());
		return true;
	}
	return false;
}

bool OE_controller::editActionSetLinestitchWidth(float val, bool offset)
{
	OE_actionSetLineStitchWidth* actionSetLineStitchWidth = dynamic_cast<OE_actionSetLineStitchWidth*>(getLastAction());
	if (actionSetLineStitchWidth)
	{
		if (offset)
		{
			actionSetLineStitchWidth->offsetWidth(val);
		}
		else
		{
			actionSetLineStitchWidth->setWidth(val);
		}
		return true;
	}
	return false;
}

bool OE_controller::refreshStitchs(float dpi, bool force)
{
	if (document)
	{
		std::list<OE_stitchs*>::iterator stitch = document->stitchs.begin();
		
		while (stitch != document->stitchs.end())
		{
			(*stitch)->refresh(dpi, force);
			stitch++;
		}
		generateInstructions();
	}

	return true;
}

/*
bool OE_controller::setThreadColor(unsigned index, unsigned char r, unsigned char g, unsigned char b)
{
	OE_thread* tmpthread = getThread(index);
	if (!tmpthread)
	{
		return false;
	}
	
	tmpthread->setColor(r, g, b);

	return true;
}

bool OE_controller::setThreadWidth(unsigned index, float width)
{
	OE_thread* tmpthread = getThread(index);
	if (!tmpthread)
	{
		return false;
	}
	
	tmpthread->setWidth(width);

	return true;
}
*/

bool OE_controller::setsubcurvePos(int index, float start, float end)
{
	OE_subcurve* tmpcurve = getSubcurve(index);
	if (!tmpcurve)
	{
		return false;
	}
	
	tmpcurve->setStart(start);
	tmpcurve->setEnd(end);
	return true;
}

vector_2dt OE_controller::getClosestPoint(OE_curve* curve, vector_2d point)
{
	if (curve)
	{
		return curve->closestPoint(point);
	}
	return vector_2dt(vector_2d(0, 0), 0);
}

OE_curve* OE_controller::getClosestCurve(vector_2d point, vector_2dt &closestPoint)
{
	if (document)
	{
		std::list<OE_pointcurve*>::iterator curve = document->curves.begin();
		vector_2dt tmpPoint;
		//vector_2dt minPoint;
		double minDistance;
		double tmpDistance;
		OE_curve* minCurve = nullptr;

		if (curve != document->curves.end())
		{
			closestPoint = (*curve)->closestPoint(point);
			minDistance = (closestPoint.v-point).len();
			minCurve = *curve;
			curve++;
		}
		while (curve != document->curves.end())
		{
			tmpPoint = (*curve)->closestPoint(point);
			tmpDistance = (tmpPoint.v-point).len();
			if (tmpDistance < minDistance)
			{
				closestPoint = tmpPoint;
				minDistance = tmpDistance;
				minCurve = *curve;
			}
			curve++;
		}
		return minCurve;
	}
	return nullptr;
}

bool OE_controller::generateInstructions()
{
	if (!document)
	{
		return false;
	}

	document->instPoints.clear();
	for (unsigned i = 0; i < document->instCommand.size(); i++)
	{
		delete document->instCommand.at(i);
	}
	document->instCommand.clear();

	std::list<OE_stitchs*>::iterator stitch = document->stitchs.begin();

	OE_thread* instThread = document->threads.front();
	if (stitch != document->stitchs.end() && (*stitch)->getThread())
	{
		instThread = (*stitch)->getThread();
	}

	document->instCommand.push_back(new OE_start(document->instPoints.size(), instThread->getColor()));

	while (stitch != document->stitchs.end())
	{
		//(*stitch)->refresh(0, true);
		if ((*stitch)->check()&&(*stitch)->getNpts()>0)
		{
			OE_thread* curThread = (*stitch)->getThread();
			if (!curThread)
			{
				curThread = document->threads.front();
			}

			if (curThread!=instThread)
			{
				instThread = curThread;
				document->instCommand.push_back(new OE_waitcolor(document->instPoints.size(), instThread->getColor()));
			}
			const std::vector<vector_2d>& points = (*stitch)->getPoints();

			// Start stop
			document->instPoints.push_back(points[0]+vector_2d(0.1,0.1));
			// Points
			document->instPoints.insert(document->instPoints.end(), points.begin(), points.end());
			// End stop
			document->instPoints.push_back(points[points.size()-1]+vector_2d(-0.1,-0.1));

		}
		stitch++;
	}

	vector_2d zero = document->getZeroPoint();
	float size = document->getPulsePerMm();
	for (unsigned i = 0; i < document->instPoints.size(); i++)
	{
		document->instPoints.at(i) = (document->instPoints.at(i) - zero)*size;
	}

	return true;
}

uint8_t int7(int i) // transform int32 to int7
{
	if (i>=0)
	{
		return i;
	}
	int8_t i8 = 2*i;
	return (*(uint8_t*)&i8)>>1;
}

void OE_controller::sendInstPoint(Machine* machine)
{
	machine->loadpoints(document);
#if 0 // OE_HARDWARE_SUPPORT
#define TEST_COMM_ACK(errmsg) \
	if (!comm.ack()) \
	{\
		std::cerr << errmsg << std::endl;\
	}
	static bool running = false;
	static int oldX, oldY;
	static unsigned sndPoint;
	static uint8_t dxys[32];
	static bool started = false;
	if (!running)
	{
		std::cout << "sendInstPoint x " << document->instPoints.size() <<std::endl;
		comm.stopMove();
		TEST_COMM_ACK("Can't set pos");
		comm.setPos(0, 0);
		TEST_COMM_ACK("Can't set pos");
		comm.setNPoint(0);
		TEST_COMM_ACK("Can't set point");
		comm.deletePoints();
		TEST_COMM_ACK("Can't delete points");
		oldX = oldY = 0;
		sndPoint = 0;
		running = true;
	} else {
		document->curPoint = comm.info.nPoint;
		int x, y, dx, dy, bigdx, bigdy;
		unsigned cmdPoints = 0;
		if (!started && comm.info.spaceSize && comm.info.freeSpace<512)
		{
			comm.startMove();
			TEST_COMM_ACK("Can't start");
			started = true;
		}
		if (sndPoint<document->instPoints.size() && comm.info.spaceSize && comm.info.freeSpace>64)
		{
			while (sndPoint<document->instPoints.size())
			{
				x = roundf(document->instPoints.at(sndPoint).x);
				y = roundf(-document->instPoints.at(sndPoint).y);
				dx = x-oldX;
				dy = y-oldY;
				if ((dx<-64)||(63<dx)||(dy<-64)||(63<dy))
				{
					bigdx = (dx+(dx>=0?0:1))/64;
					bigdy = (dy+(dy>=0?0:1))/64;
					dx -= 64*bigdx;
					dy -= 64*bigdy;
					if (cmdPoints==15)
					{
						comm.addPoints(dxys, 15);
						TEST_COMM_ACK("Can't add points");
						return;
					}
					dxys[2*cmdPoints] = 0x80|int7(bigdx);
					dxys[2*cmdPoints+1] = int7(bigdy);
					cmdPoints++;
				}
				dxys[2*cmdPoints] = int7(dx);
				dxys[2*cmdPoints+1] = int7(dy);
				cmdPoints++;
				sndPoint++;
				oldX = x;
				oldY = y;
				if (cmdPoints==16)
				{
					comm.addPoints(dxys, 16);
					TEST_COMM_ACK("Can't add points");
					return;
				}
			}
			if (cmdPoints)
			{
				comm.addPoints(dxys, cmdPoints);
				TEST_COMM_ACK("Can't add points");
				return;
			}
		}
	}
#undef TEST_COMM_ACK
#endif
}
