#include "Behaviors/StateMachine.h"

$nodeclass EggsLine : VisualRoutinesStateNode {
	$nodeclass GetEggs : MapBuilderNode : doStart {
		mapreq.addObjectColor(ellipseDataType, "red");
		mapreq.addObjectColor(ellipseDataType, "green");
		mapreq.addObjectColor(ellipseDataType, "blue");
		mapreq.addObjectColor(lineDataType, "blue");
	}

	$nodeclass ExamineResult : VisualRoutinesStateNode : doStart {
		//Get all shapes and printout result
		NEW_SHAPEVEC(ellipse_shapes, EllipseData, select_type<EllipseData>(camShS));
		NEW_SHAPEVEC(line_shape, LineData, select_type<LineData>(camShS));
		
		//Counters used to count number of ellipses and line
		int left = 0;
		int right = 0;
		float maxLength = 0;
		Shape<LineData> maxLine = line_shape[0];

		//Iterate through lines to get the max one and save
		SHAPEVEC_ITERATE(line_shape, LineData, myLine)
			if(myLine->getLength() > maxLength) {
				maxLength = myLine->getLength();
				maxLine = myLine;
			} 
			camShS.deleteShape(myLine);
		END_ITERATE;
		
		//Add maxLine into camShs and set to infinite
		NEW_SHAPE(newLine, LineData,
  					new LineData(camShS, maxLine->getCentroid(),
					maxLine->getOrientation()));
		newLine->setInfinite(true);

		//Look to the left and right of maxLine and get the numbers
		SHAPEVEC_ITERATE(ellipse_shapes, EllipseData, myEllipse)
				if(myEllipse->getArea() > 120)  {
					if(newLine->pointIsLeftOf(myEllipse->getCentroid())) left++;
					else right++;
				}
				else {
					camShS.deleteShape(myEllipse);
				}
	 	END_ITERATE;
				cout << "The number of points to the left of line is " << left << endl;
				cout << "The number of points to the right of line is " << right << endl;
	}


	$setupmachine {
		GetEggs =C=> ExamineResult
	}
}
REGISTER_BEHAVIOR (EggsLine);
