#include "Behaviors/StateMachine.h"

$nodeclass SortLab {

  $nodeclass BuildWorld : VisualRoutinesStateNode : doStart {

		std::vector<Point> wallPts;
		wallPts.push_back(Point(-750, 1500,0,allocentric));
		wallPts.push_back(Point(-750,-1500,0,allocentric));
		wallPts.push_back(Point( 750,-1500,0,allocentric));
		wallPts.push_back(Point( 750, 1500,0,allocentric));
		NEW_SHAPE(worldBounds, PolygonData,
							new PolygonData(worldShS, wallPts, true));
		worldBounds->setObstacle(true);

		int const tagHeight = 8 * 25.4;
		MapBuilderRequest::defaultMarkerHeight = tagHeight;
		NEW_SHAPE(tagNorth, AprilTagData,
							new AprilTagData(worldShS, AprilTags::TagDetection(1),
															 Point( 750, 0,tagHeight,allocentric),
															 fmat::Quaternion::aboutZ(0)));
		NEW_SHAPE(tagSouth, AprilTagData,
							new AprilTagData(worldShS, AprilTags::TagDetection(3),
															 Point(-750, 0,tagHeight,allocentric),
															 fmat::Quaternion::aboutZ(0)));
		NEW_SHAPE(tagEast, AprilTagData,
							new AprilTagData(worldShS, AprilTags::TagDetection(2),
															 Point(0,-1500,tagHeight,allocentric),
															 fmat::Quaternion::aboutZ(M_PI)));
		NEW_SHAPE(tagWest, AprilTagData,
							new AprilTagData(worldShS, AprilTags::TagDetection(4),
															 Point(0, 1500,tagHeight,allocentric),
															 fmat::Quaternion::aboutZ(0)));
		tagNorth->setLandmark(true);
		tagSouth->setLandmark(true);
		tagEast->setLandmark(true);
		tagWest->setLandmark(true);
  }

  $nodeclass FindStuff : MapBuilderNode(MapBuilderRequest::worldMap) : doStart {
		mapreq.addObjectColor(cylinderDataType, "red");
		//mapreq.addObjectColor(polygonDataType, "blue");
		mapreq.addOccluderColor(polygonDataType, "red");
		mapreq.addMinBlobArea("red",1000);
		mapreq.setAprilTagFamily();
		mapreq.searchArea = Lookout::groundSearchPoints();
		mapreq.pursueShapes = true;
		mapreq.clearVerbosity = MapBuilder::MBVshapeMatch;
  }


  /*
  returns whether a cylinder is bounded by a goal.
  The goal points are hardcoded. Not the prettiest thing, but I used a functor!
  */
  struct CylBounded : public UnaryShapeRootPred {
	public: 
		CylBounded() {
			eastBR = Point(-250,-1500,0, allocentric);
                 	eastTL = Point(250,-1000,0, allocentric);
                 	westTL = Point(250,1500,0, allocentric);
                 	westBR = Point(-250,1000,0, allocentric);
		}

		bool operator() (const ShapeRoot &shape) const {
			Point center = shape->getCentroid();
			center.setRefFrameType(allocentric);
			//Contained by east?
			if(center.isLeftOf(eastBR) && center.isRightOf(eastTL)) {
				if(center.isAbove(eastBR) && center.isBelow(eastTL)) {
					return false;
				}
			}
			//Contained by west?
			if(center.isLeftOf(westBR) && center.isRightOf(westTL)) {
				if(center.isAbove(westBR) && center.isBelow(westTL)) {
					return false;
				}
			}
			return true;
		}


	private:
                Point eastBR; 
                Point eastTL; 
                Point westTL; 
                Point westBR;

  };

  //given a cylinder and tag list, return tag closest to cylinder
  static DualCoding::Shape<AprilTagData> tagPair(const ShapeRoot &shape, std::vector< DualCoding::Shape <AprilTagData> >& tags) {
	Point center = shape->getCentroid();
	Shape<AprilTagData> curTag = tags[0];
	float curDistance = curTag->getCentroid().xyDistanceFrom(center);
	SHAPEVEC_ITERATE(tags, AprilTagData, mytag)
		Point myCenter = mytag->getCentroid();
		float myDistance = myCenter.xyDistanceFrom(center);
		if (myDistance < curDistance) {
			curTag = mytag;
			curDistance = myDistance;
		}
	END_ITERATE;
	return curTag;
 }

  $nodeclass MoveCenter : PilotNode(PilotTypes::walk) : doStart {
    $reference MeasureDistance::initialPosition, MeasureDistance::initialOrientation;
    initialPosition = theAgent->getCentroid();
    initialOrientation = theAgent->getOrientation();
    NEW_SHAPE(destination, PointData, new PointData(worldShS, Point(0,0,0,allocentric))); // center of arena
    pilotreq.dx = 500;
  }

  // Requires: Assumes any cylinder in a goal belongs in that goal
  $nodeclass MoveOneCylinder : GrasperNode(GrasperRequest::moveTo) : doStart {
    		NEW_SHAPEVEC(cyls, CylinderData, select_type<CylinderData>(worldShS));
		NEW_SHAPEVEC(tags, AprilTagData, select_type<AprilTagData>(worldShS));	

		Point eastBR = Point(-250,-1500,0, allocentric);
                Point eastTL = Point(250,-1000,0, allocentric);
                Point westTL = Point(250,1500,0, allocentric);
                Point westBR = Point(-250,1000,0, allocentric);
		int numWest = 0;
		int numEast = 0;
		SHAPEVEC_ITERATE(cyls, CylinderData, shape) 
			Point center = shape->getCentroid();
			center.setRefFrameType(allocentric);
			//Contained by east?
			if(center.isLeftOf(eastBR) && center.isRightOf(eastTL)) {
				if(center.isAbove(eastBR) && center.isBelow(eastTL)) {
					numEast++;
				}
			}
			//Contained by west?
			if(center.isLeftOf(westBR) && center.isRightOf(westTL)) {
				if(center.isAbove(westBR) && center.isBelow(westTL)) {
					numWest++;
				}
			}
		END_ITERATE;	
		std::cout << "^^^In west: " << numWest << " in east: " << numEast << std::endl;



		NEW_SHAPE(westDestination, PointData, new PointData(worldShS, Point(0,1300 - (125)*numWest,0,allocentric))); 
		NEW_SHAPE(eastDestination, PointData, new PointData(worldShS, Point(0,-1300 + (125)*numEast,0,allocentric))); 
		westDestination->setObstacle(false);
		eastDestination->setObstacle(false);


		//no cylinders on map
		if ( cyls.empty() ) {
			cancelThisRequest();
			return;
		}

		//make list of cylinders not already in goal
		NEW_SHAPEVEC(remainingCyls, CylinderData, subset(cyls, CylBounded()));
		//all cylinders in goal, nothing left to do
		if ( remainingCyls.empty() ) {
			std::cout << "^^^No remaining Cylinders." << std::endl;
			cancelThisRequest();
			return;
		}
		std::cout << remainingCyls.size() << "^^ cyls remaining" << std::endl;
		//Select cylinder nearest its destination
		float curDistance;
		float bestDistance = -1;
		ShapeRoot targetCyl;
		int curTarget;
		SHAPEVEC_ITERATE(remainingCyls, CylinderData, curCyl) 
			curTarget = tagPair(curCyl, tags)->getTagID();
			Point curCenter = curCyl->getCentroid();
			curCenter.setRefFrameType(allocentric);
			//pure y distance, xyDistanceFrom causing issues?
			if (curTarget==10) {
				curDistance = curCenter.xyDistanceFrom(westDestination->getCentroid());
			}
			else {
				curDistance = curCenter.xyDistanceFrom(eastDestination->getCentroid());
			}
			if ( (bestDistance==-1) || (curDistance < bestDistance)) {
				targetCyl = curCyl;
				bestDistance = curDistance;
			}
			std::cout << "^^^X of:" << curCenter.coordX() << " Y of: " << curCenter.coordY() << "target: " << curTarget << " Distance: " << curDistance << std::endl;
		END_ITERATE;
		std::cout << "^^^Chose distance of:" << bestDistance << std::endl;

		graspreq.object = targetCyl;
		
		//figure out where it belongs
		int targetLoc = tagPair(targetCyl, tags)->getTagID();
		std::cout << "^^^Targeted Cylinder. Value of:" << targetLoc << std::endl;

		//tag = 10 -> west
		//tag = 11 -> east
		if (targetLoc == 10) {
			std::cout << "^^^Placing West." << std::endl;
			graspreq.targetLocation = westDestination;
		}
		else {
			std::cout << "^^^Placing East." << std::endl;
			graspreq.targetLocation = eastDestination;
		}
		
  }

  $setupmachine{
    BuildWorld =N=> ParkArm =C=> find
	build: BuildWorld =N=> ParkArm =C=> MoveCenter =C=> find
  	find: FindStuff =C=> MoveOneCylinder =C=> MoveOneCylinder =C=> build
  }
}

REGISTER_BEHAVIOR(SortLab);
