package net.phatcode.rel;

import java.awt.Color;
import java.awt.Graphics2D;

import net.phatcode.rel.math.Utils;
import net.phatcode.rel.utils.AABB;
import net.phatcode.rel.utils.Explosion;
import net.phatcode.rel.utils.SpriteFont;

public class Letter extends Entity
{

	public 
	static char alphabet[] = { 'A','B','C','D','E',
							   'F','G','H','I','J',
							   'K','L','M','N','O',
							   'P','Q','R','S','T',
							   'U','V','W','X','Y',
							   'Z'};	
	
	boolean active = false;
	private String alpha = new String();
	int counter = 0;
	
	Letter()
	{
		height = 32;
		width = 24;
		x = -2000;
		y = -2000;
		dy = -5;
		baseFrame = 0;
		frame = 0;
		active = false;
	}
	
	void randomize()
	{
		
		dx = -2 + (int)(Math.random()* 5.0 );
		dy = -2 + (int)(Math.random()* 5.0 );
		x = 10 + (int)(Math.random() * (Constants.SCREEN_WIDTH-20) );
		y = 10 + (int)(Math.random() * (Constants.SCREEN_HEIGHT/2) );
		Integer a = (int)(Math.random()* 26 );
		alpha = "" + alphabet[a];
		alpha = alpha.toUpperCase();
		active = true;
	}
	
	public void update()
	{
		counter++;
		x += dx;
		y += dy;
		
		if( x < 0)
		{
			dx = -dx;
		}
		
		if( x > Constants.SCREEN_WIDTH )
			
		{
			dx = -dx;
		}
		
		if( y < 0 )
		{
			dy = -dy;
		}
		
		if( y > (Constants.SCREEN_HEIGHT/2) + 10 )
			
		{
			dy = -dy;
		}
		
		collisionBox.init(x, y, width, height);
		
	}
	
	public void render( Screen screen, Graphics2D g,
						SpriteFont font )
	{
		int screenX = (int)(x - width/2.0f);
		int screenY = (int)(y - height/2.0f);
		
		font.print( screenX, screenY, alpha, screen, g );
		
		g.setColor(Color.BLUE);
		//g.drawRect( screenX, screenY, (int)width, (int)height );
	
	}

	public void kill()
	{
		
		Explosion.getInstance().spawn( x, 
				   y, 
				   0,
				   0, 
				   42, 5 );	
		int max = 4 + (int)(Math.random() * 5);
		for( int i = 0; i < max; i++ )
		{
			Explosion.getInstance().spawn( x, 
								   y, 
								   counter + (2 * Utils.PI * i) / (float)max,
								   5, 
								   52, 4 );	

		}

		x = -2000;
		y = -2000;
		active = false;
		collisionBox.init(x, y, width, height);
	}
	
	public boolean isActive()
	{
		return active;
	}

	public void setActive(boolean active)
	{
		this.active = active;
	}

	public AABB getCollisionBox()
	{
		return collisionBox;
	}

	public void setCollisionBox(AABB collisionBox)
	{
		this.collisionBox = collisionBox;
	}

	public String getAlpha()
	{
		return alpha;
	}

	public void setAlpha( String alpha )
	{
		this.alpha = alpha;
	}
	
	
	
}
