/********** Simple go-to-closest-agent enemy *******/
function SimpleEnemy( controller, x, y )
{
	this.controller = controller;
	this.startX = x;
	this.startY = y;
	this.isAlive = true;
}

SimpleEnemy.prototype.getPosition = function( time )
{
	var positions = this.controller.getPositions();

	// Base case: start position.
    if ( time == 0 )
        return { x: this.startX, y: this.startY, isAlive: this.isAlive };

    var previous_location = this.getPosition( time - 1 );
    if (!previous_location.isAlive)
    {
    	console.log( "Dying: " + time );
    
    	//console.log(previous_location)
    	return {
    		isAlive: false
    		};
     }

	// Recursive step: go one step closer to the nearest agent.

	// Find the nearest agent.
	var agents = positions[time];
	var nearest_dist = 10000;
	var nearest_agent = agents[0];
	for ( var i = 0; i < agents.length; i++ )
	{
		// I think this is necessary but I'm not sure.
		if ( agents[i] == undefined )
			continue;

		var dx = previous_location.x - agents[i].x;
		var dy = previous_location.y - agents[i].y;
		var distance = ( dx * dx ) + ( dy * dy );

		if ( distance < nearest_dist )
		{
			nearest_dist = distance;
			nearest_agent = agents[i];
		}
	}
	
	
    var xDist = previous_location.x - nearest_agent.x;
    var yDist = previous_location.y - nearest_agent.y;
    var xOffset = 0;
    var yOffset = 0;

    if ( Math.abs( xDist ) > Math.abs( yDist ) )
	{
		if ( xDist == 0 )
			xOffset = 0;
		else
			xOffset = - xDist / Math.abs( xDist );
	}
    else
	{
		if ( yDist == 0 )
			yOffset = 0;
		else
			yOffset = - yDist / Math.abs( yDist );
	}
	
	// check if the enemy should die
	var sands = controller.getLevel().sand;
	for ( var i = 0; i < sands.length; i++ )
	{	
		var sand = sands[i];
		if( sand.x == previous_location.x + xOffset &&
			sand.y == previous_location.y + yOffset )
			{
			console.log("SANDDD!")
			return {
				isAlive: false
				};
			}
	}

    return { 
		x: previous_location.x + xOffset, 
		y: previous_location.y + yOffset,
		isAlive: true
	};
}

/********** Immortal go-to-closest-agent enemy *******/
// this enemy doesn't die when it steps in the sand.
// VERY DANGEROUS! FOR TESTING PURPOSES ONLY
function ImmortalEnemy( controller, x, y )
{
	this.controller = controller;
	this.startX = x;
	this.startY = y;
	this.isAlive = true;
}

ImmortalEnemy.prototype.getPosition = function( time )
{
	var positions = this.controller.getPositions();

	// Base case: start position.
    if ( time == 0 )
       return { x: this.startX, y: this.startY, isAlive: this.isAlive };

    var previous_location = this.getPosition( time - 1 );
    
	// Recursive step: go one step closer to the nearest agent.

	// Find the nearest agent.
	var agents = positions[time];
	var nearest_dist = 10000;
	var nearest_agent = agents[0];
	for ( var i = 0; i < agents.length; i++ )
	{
		// I think this is necessary but I'm not sure.
		if ( agents[i] == undefined )
			continue;

		var dx = previous_location.x - agents[i].x;
		var dy = previous_location.y - agents[i].y;
		var distance = ( dx * dx ) + ( dy * dy );

		if ( distance < nearest_dist )
		{
			nearest_dist = distance;
			nearest_agent = agents[i];
		}
	}
	
	
    var xDist = previous_location.x - nearest_agent.x;
    var yDist = previous_location.y - nearest_agent.y;
    var xOffset = 0;
    var yOffset = 0;

    if ( Math.abs( xDist ) > Math.abs( yDist ) )
	{
		if ( xDist == 0 )
			xOffset = 0;
		else
			xOffset = - xDist / Math.abs( xDist );
	}
    else
	{
		if ( yDist == 0 )
			yOffset = 0;
		else
			yOffset = - yDist / Math.abs( yDist );
	}	

    return { 
		x: previous_location.x + xOffset, 
		y: previous_location.y + yOffset,
		isAlive: true
	};
}

// this enemy has a phobia of sand
/********** Eremikophobic Enemy *******/
function EremikophobicEnemy( controller, x, y )
{
	this.controller = controller;
	this.startX = x;
	this.startY = y;
	this.isAlive = true;
}

EremikophobicEnemy.prototype.getPosition = function( time )
{
	var positions = this.controller.getPositions();

	// Base case: start position.
    if ( time == 0 )
        return { x: this.startX, y: this.startY, isAlive: this.isAlive };

    var previous_location = this.getPosition( time - 1 );
    if (!previous_location.isAlive)
    	return {
    		isAlive: false
    		};

	// Recursive step: go one step closer to the nearest agent.

	// Find the nearest agent.
	var agents = positions[time];
	var nearest_dist = 10000;
	var nearest_agent = agents[0];
	for ( var i = 0; i < agents.length; i++ )
	{
		// I think this is necessary but I'm not sure.
		if ( agents[i] == undefined )
			continue;

		var dx = previous_location.x - agents[i].x;
		var dy = previous_location.y - agents[i].y;
		var distance = ( dx * dx ) + ( dy * dy );

		if ( distance < nearest_dist )
		{
			nearest_dist = distance;
			nearest_agent = agents[i];
		}
	}
	
	
    var xDist = previous_location.x - nearest_agent.x;
    var yDist = previous_location.y - nearest_agent.y;
    var xOffset = 0;
    var yOffset = 0;

    if ( Math.abs( xDist ) > Math.abs( yDist ) )
	{
		if ( xDist == 0 )
			xOffset = 0;
		else
			xOffset = - xDist / Math.abs( xDist );
	}
    else
	{
		if ( yDist == 0 )
			yOffset = 0;
		else
			yOffset = - yDist / Math.abs( yDist );
	}
	
	// check if the enemy is about to step into sand
	var sands = controller.getLevel().sand;
	for ( var i = 0; i < sands.length; i++ )
	{	
		var sand = sands[i];
		if( sand.x == previous_location.x + xOffset &&
			sand.y == previous_location.y + yOffset )
			{
			return { // if you are about to step in sand, don't move.
				x: previous_location.x, 
				y: previous_location.y,
				isAlive: true
			};
			}
	}
		

    return { 
		x: previous_location.x + xOffset, 
		y: previous_location.y + yOffset,
		isAlive: true
	};
}

