/**
 * @author Richard Eric M. Lope (Relminator)
 * http://rel.phatcode.net
 *
 */

package net.phatcode.rel.gameframework;

import net.phatcode.rel.math.Vector2D;
import net.phatcode.rel.utils.AABB;
import net.phatcode.rel.utils.ImageAtlas;
import net.phatcode.rel.utils.SpriteBatcher;
import net.phatcode.rel.utils.SpriteGL;

public abstract class Entity
{

	public static enum RenderMode
	{
		NORMAL,
		WHITE
	}

	public Vector2D position = new Vector2D();
	public Vector2D direction = new Vector2D();
	public Vector2D gravity = new Vector2D();
	public Vector2D target = new Vector2D();
	public Vector2D velocity = new Vector2D();
	
	protected float speed;
	protected float acceleration = 0.1f;
	protected float angle; 
	
	protected boolean active = false;
	protected int counter = 0;
	
	
	protected Animator animator = new Animator();;
	
	protected AABB hitBox = new AABB();
	
	protected Behavior behavior = new Behavior();
	
	public Entity()
	{
	
	}
	
	public Entity( Vector2D position,
				   Vector2D direction,
				   float speed, 
				   Animator animator,
				   AABB hitBox )
	{
	
		this.position = position;
		this.direction = direction;
		this.speed = speed;
		
		this.animator = animator;
		
		this.hitBox = new AABB( hitBox );
		
	}
	
	public void update()
	{
		counter++;
		behavior.update( this );
		
	}
	
	public void render( SpriteBatcher spriteBatcher, ImageAtlas imageAtlas )
	{
		if( active )
		{
			SpriteGL sprite = imageAtlas.getSprite(animator.getFrame());
			
			spriteBatcher.sprite( (int)position.x - sprite.width/2, 
								  (int)position.y - sprite.height/2, 
								  SpriteGL.FLIP_NONE, 
								  sprite );
			
			//BufferedImage sprite = imageAtlas.getSprite(animator.getFrame());
			//g.drawImage( sprite, 
			//		 	 (int)position.x - sprite.getWidth()/2,
			//		 	 (int)position.y - sprite.getHeight()/2,
			//		 	 screen );
			
		}
		
	}

	public void kill()
	{
		active = false;
	}
	
	public void explode()
	{
		//Explosion.getInstance().spawn( position.x, 
		//							   position.y, 
		//							   0,
		//							   0, 
		//							   42, 5 );	
		kill();
	}
	
	
	public void initHitBox( float x, float y, float width, float height )
	{
		hitBox.init( x, y, width, height );
	}
	
	public void updateHitBox()
	{
		hitBox.init( position.x - hitBox.width/2 - 1, position.y - hitBox.height/2 - 1,
				     hitBox.width, hitBox.height );
	}
	
	public boolean collidesWith( Entity entity )
	{
		if( hitBox.intersects( entity.hitBox )) return true;
		return false;
	}

	public boolean isActive()
	{
		return active;
	}

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

	public int getCounter()
	{
		return counter;
	}

	public void setCounter( int counter )
	{
		this.counter = counter;
	}

	public Animator getAnimator()
	{
		return animator;
	}

	public void setTarget(Vector2D target)
	{
		this.target = target;
	}

	public void setGravity(Vector2D gravity)
	{
		this.gravity = gravity;
	}

	public void setAnimator( Animator animator )
	{
		this.animator = animator;
	}
	
	public void setBehavior( Behavior behavior )
	{
		this.behavior = behavior;
	}

	public float getAngle()
	{
		return angle;
	}

	public void setAngle(float angle)
	{
		this.angle = angle;
	}

	public float getSpeed()
	{
		return speed;
	}

	public float getAcceleration()
	{
		return acceleration;
	}

	public void setSpeed(float speed)
	{
		this.speed = speed;
	}

	public void setAcceleration(float acceleration)
	{
		this.acceleration = acceleration;
	}

	public Vector2D getPosition()
	{
		return position;
	}

	public Vector2D getDirection()
	{
		return direction;
	}

	public Vector2D getGravity()
	{
		return gravity;
	}

	public Vector2D getTarget()
	{
		return target;
	}

	public AABB getHitBox()
	{
		return hitBox;
	}

	public Behavior getBehavior()
	{
		return behavior;
	}

	public void setPosition(Vector2D position)
	{
		this.position = position;
	}

	public void setDirection(Vector2D direction)
	{
		this.direction = direction;
	}

	public void setHitBox(AABB hitBox)
	{
		this.hitBox = hitBox;
	}
	
	
	
}
