/**
 *
 * @author Jesper Turbo
 * @version 1.0
 * @COPYLEFT ALL WRONGS REVERSED.
 */


public class FCHulkHoejgaard extends Team
{
	private final int NUM_OF_PLAYERS = 5;

	private int fieldWidth = -1;
	private int fieldHeight = -1;

	private double homeX = -1;													// Position of own goal
	private double homeY = -1;
	private double goalX = -1;													// Position of opponent's goal
	private double goalY = -1;

	private Player[] p;															// Holds the players

	public FCHulkHoejgaard( int team, int fieldWidth, int fieldHeight ) 		// Ku være rart at kende størrelsen på målet
	{
		this.team = team;														// It's good to know, what team you are! Aight!
		this.fieldWidth = fieldWidth;
		this.fieldHeight = fieldHeight;

		p = new Player[NUM_OF_PLAYERS];

		for( int i = 0; i < NUM_OF_PLAYERS; i++ )
		{
			p[i] = new Player( i );												// Player ID = index in array
		}

		// Position of the goals
		homeY = goalY = fieldHeight/2.;
		homeX = team == 1 ? fieldWidth : 0.;									// Team 0 = Left side, Team 1 = Right side
		goalX = team == 1 ? 0. : fieldWidth;
	}

	// Euclidian distance
	double dist( double x1, double y1, double x2, double y2 )
	{
		return Math.sqrt( (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2) );
	}

	double dist( Player p1, Player p2 )
	{
		return Math.sqrt( (p1.posX-p2.posX)*(p1.posX-p2.posX) + (p1.posY-p2.posY)*(p1.posY-p2.posY) );
	}

	// rand number between 0 to 2PI
	double randDir()
	{
		return Math.random() * 2*Math.PI;
	}

	// Direction to opponents goal
	double goalDir( Player p )
	{
		return getDir( p.posX, p.posY, goalX, goalY );
	}

	double ballDir( Player p, double[] ballPos )
	{
		return getDir( p.posX, p.posY, ballPos[0], ballPos[1] );
	}

	// Direction from (x1,y1) to (x2,y2)
	double getDir( double x1, double y1, double x2, double y2 )
	{
		return Math.atan2( y2-y1, x2-x1 );
	}

	double getDir( Player p1, Player p2 )
	{
		return Math.atan2( p2.posY-p1.posY, p2.posX-p1.posX );					// [-pi, pi]
	}

	public double[][] action( double[][][] playerPos, double[] ballPos, double time, int[] score )
	{
		double minBallDist = 100000;
		int playerNearBall = -1;

		for( int i = 0; i < NUM_OF_PLAYERS; i++ )
		{
			p[i].updatePlayer( playerPos[team][i], ballPos );
			p[i].runSpeed = 0;

			if( p[i].distToBall < minBallDist )									// Find the guy nearest the ball
			{
				minBallDist = p[i].distToBall;
				playerNearBall = i;
			}
		}

		// The Angry Attacker
		if( dist(p[1].posX, p[1].posY, fieldWidth*.25, goalY) > 25 )			// Only works (correctly) when playing as Team 2
		{
			p[1].dir = getDir( p[1].posX, p[1].posY, fieldWidth*.25, goalY );
			p[1].runSpeed = 25;
		}

		p[playerNearBall].dir = ballDir( p[playerNearBall], ballPos );			// Nearest player runs towards the ball
		p[playerNearBall].runSpeed = 25;

		// Save the new directions and return them
		double[][] a = new double[NUM_OF_PLAYERS][4];

		for( int i = 0; i < NUM_OF_PLAYERS; i++ )
		{
			a[i][0] = p[i].dir;			// run dir
			a[i][1] = p[i].runSpeed;	// run speed
			a[i][2] = p[i].kickDir;		// kick dir
			a[i][3] = 15;				// kick speed
		}

		return a;
	}

	private class Player
	{
		public int id;
		public double posX = 0;
		public double posY = 0;
		public double dir  = 0;													// running direction of each of the players [rad]
		public double kickDir = 0;												// shooting direction of each player [rad]
		public double runSpeed = 0;
		public double distToBall = -1;
		double distToGoal = -1;													// not curently used
		double distToBestPlayer = -1;											// not currently used

		public Player( int id )
		{
			this.id = id;
		}

		public void updatePlayer( double[] pos, double[] ballPos )
		{
			int bestPlayer = -1;
			
			this.posX = pos[0];
			this.posY = pos[1];

			distToBall = dist( posX, posY, ballPos[0], ballPos[1] );
			distToGoal = dist( posX, posY, goalX, goalY );
			bestPlayer = bestPlayer();

			if( bestPlayer != -1 )
			{
				kickDir = getDir( this, p[bestPlayer] );
			}
			else
			{
				kickDir = goalDir( this );
			}
		}

		int bestPlayer()
		{
			int best = -1;
			double minDist = 1000000;											// inf

			for( int i = 0; i < NUM_OF_PLAYERS; i++ )
			{
				if( p[i].posX < posX )											// player must be infront of me.. husk på angrebsretning...
				{
					double d = dist( this, p[i] );

					if( d < minDist )
					{
						minDist = d;
						distToBestPlayer = d;
						best = i;
					}
				}
			}

			return best;
		}

	}

	public static void main( String[] args )
	{
		FCHulkHoejgaard f = new FCHulkHoejgaard( 0, 370, 50 );
//		System.out.println( "Created FCHulkHoejgaard class." );

		//f.action()

	}

}

