// __author__ = Juan Carlos De Abreu (jcabreur@gmail.com)

/*
 * TODO(jcabreur): finish write a general comment here ...
 * This scene define a ejercise for the project LAPAROS.
 *
 * The ejercise consist in put a ball in a box of the same color
 * the advantage of this way of ejersice is that can be 
 * edited easy.
 */

// TODO: el modelo de la pinza deberia ser cargado por partes
// es decir la punta de la pinza aparte para poder detectar 
// colision correctamente, por lo que debo cambiar la especificacion
// del arbol de escena.
 
// Define the times for do the ejersise.
nBalls = 16;							// Number of balls for the ejercise.
putBallTime = 10000;					// Time for put a specific ball(10 sec).
ejerciseTime = nBalls * putBallTime;	// Total time for do the ejersice.

// Path of all the models 
pathBall = "Data/Model/Ball.obj";
pathBox = "Data/Model/box.obj";
pathTable = "Data/Model/table.obj";
pathOpenPinza = "Data/Model/openPinza.obj";
pathClosePinza = "Data/Model/closePinza.obj";
pathPinzaWithBall = "Data/Model/pinzaWithBall.obj";

// Define all the models used by the app.
ball = Model(pathBall);
box = Model(pathBox);
table = Model(pathTable);					// TODO: Change name.
closePinza = Model(pathOpenPinza);			// TODO: Change name.
openPinza = Model(pathClosePinza);			// TODO: Change name.
pinzaWithBall = Model(pathPinzaWithBall);	// TODO: Change name.

def main() {
	// Add the materials specific for this scene.
	materials.add("Data/Materials/Balls.mat");

	// Create the grid of boxes.
	boxes = ();
	for (i = 0; i < 8; ++i) {
		for (j = 0; j < 8; ++j) {
			boxes.add(group(translate(i, 0, j), box));
		}
	}
	
	// Add all the balls.
	// TODO: Hacer que la forma en la cual se asigna el color
	// de la esfera sea cableada....
	balls = group();
	tmpBall = ball;
	for (i = 0; i < nBalls; ++i) {
		posx = rand(0.0, 10.0);
		posz = rand(0.0, 10.0);
		tmpBall.mat = materials[rand(0, materials.size())];
		balls.add(group(translate(posx, 0, posz), ball));
	}
	
	// Disable the draw of the other types of pinza.
	openPinza.draw = false;
	pinzaWithBall.draw = false;
	
	// Define the initial state of the scene tree.
	scene = group(
		light(),
		translate(0, 0, -3),
		group(
			translate(...),
			scale(...),
			rotate(...),
			group(closePinza, openPinza, pinzaWithBall)
		),
		group(translate(...), scale(...), rotate(...), table),
		group(translate(...), scale(...), rotate(...), boxes),
		group(translate(...), scale(...), rotate(...), balls)
	);
 
	// Define the inputs for the app.
	KEY_SCAPE = exit();
	RASTER_MOVE = updatePinza();
	RASTER_PRESS_UP = closePinza();
	RASTER_PRESS_DOWN = openPinza();
 
	// Set the timers for the application.
	Timer(ejerciseTime, putBallTime, changeBox(), NULL);
}

// This method update the position of the pinza with the values of the raster
def updatePinza() {
	// Update the values for the raster.
	scene.nodes[0].node[0] = RASTER_POSITION;
	scene.nodes[0].node[1] = RASTER_ORIENTATION;
}

// Method to change the box objective.
def changeBox() {
	posx = rand(0, 8);
	posy = rand(0, 8);
	tmpMat = materials[rand(0, materials.size)];
	tmp = scene.nodes[3].nodes[3].nodes[posy * 8 + posx].nodes[1];
	lastBox.mat = lastBox.resetMaterial; // TODO: Material Origial
	lastBox = tmp;
}

isBallTaken = false;	// To know if the pinza have a ball
lastBox = box;			// Last box that was ussed.

// This method manage the event when the user press the botton of the pinza.
def openPinza() {
	if (isBallTaken) {
		isBallTaken = false;
		// TODO: Detect colission between the pinza and the box.
		if (colission) LOG("Correcto");
		else LOG("Fallo");
	}
	
	// Update the model that will be display.
	closePinza.draw = false;
	openPinza.draw = true;
	pinzaWithBall.draw = false;
}

def closePinza() {
	// Disable draw the open pinza.
	openPinza.draw = false;
	
	objectDoItColission = detectColission(closePinza);
	if (objectDoItColission != NULL) {
		isBallTaken = true;
		
		// TODO: Check the id of the object that do colission.
		
		objectDoItColission.draw = false;
		// Update the material of the ball taken by the pinza.
		//scene.nodes[0].nodes[3].nodes[2].mat["ball"] = objectDoItColission.mat;
		pinzaWithBall.mat["ball"] = objectDoItColission.mat;

		// Enable draw the pinza with the ball.
		pinzaWithBall.draw = true;
	}
	else {
		// Enable draw the pinza closed.
		closePinza.draw = true;
	}
}
