/**
 * 
 */
package ei.engine.scene;

import java.awt.Rectangle;

import org.lwjgl.opengl.GL11;

import ei.engine.math.Vector2f;
import ei.engine.math.Vector3f;
import ei.engine.texture.Texture;

/**
 * This class is the root class of all the objects that
 * will show on the scrren. This class handles the position
 * and rotation and the colitions of the objects.
 * @author Ziver
 */
public abstract class Entity {
	/** The name of this sprite */
	private String name;  
	
	/** The location of this entity in pixels*/
	private Vector3f location;	
	  
	/** The rotation of this entity in pixels*/
	private Vector3f rotation;
	
	/** The size of this entity in pixels*/
	private Vector3f size;
	
	/**
	 * creates a new entity
	 * 
	 * @param name The name of the entity
	 */
	public Entity(String name){
		this.name = name;
		location = new Vector3f();
		rotation = new Vector3f();
		size = new Vector3f(1,1,1);
	}
	
	/**
	 * @return The name of the entity
	 */
	public String getName() {
		return name;
	}
	
	/**
	 * Get the Location of the entity
	 * 
	 * @return The Location of the entity
	 */
	public Vector3f getLocation() {
		return location;
	}
	
	/**
	 * set the location of this entity in pixels
	 * 
	 * @param l The location of the entity
	 */
	public void setLocation(Vector3f l) {
		location = l;
	}
	
	/**
	 * set the location of this entity in pixels
	 * 
	 * @param l The location of the entity
	 */
	public void setLocation(Vector2f l) {
		location = new Vector3f(l.getX(),l.getY(),getLocation().getZ());
	}
	
	/**
	 * Get the rotation of the entity
	 * 
	 * @return The Location of the entity
	 */
	public Vector3f getRotation() {
		return rotation;
	}
	
	/**
	 * set the rotation of this entity in pixels
	 * 
	 * @param r The rotation of the entity
	 */
	public void setRotation(Vector3f r) {
		rotation = r;
	}
	
	/**
	 * Get the scale of the entity
	 * 
	 * @return The Location of the entity
	 */
	public Vector3f getScale() {
		return size;
	}
	
	/**
	 * set the scale of this entity in pixels
	 * 
	 * @param s The size of the entity
	 */
	public void setScale(Vector3f s) {
		size = s;
	}

	/**
	 * Sets the location of the entity in opengl
	 * before rendering.
	 */
	protected void setTranslationGL(){
		// translate to the right location and prepare to draw
		GL11.glTranslatef(location.getX(), location.getY(), location.getZ());
	}
	
	protected void setScaleGL(){
		// translate to the right location and prepare to draw
		GL11.glScalef(size.getX(), size.getY(), size.getZ());
	}
	
	/**
	 * Sets the rotation of the entity in opengl
	 * before rendering.
	 * 
	 * @param rx The x coordinate of the center of rotation
	 * @param ry The y coordinate of the center of rotation
	 */
	protected void setRotationGL(float rx, float ry){
		//GL11.glTranslatef(rx,ry, location.getZ());
		// rotating the entity
        GL11.glRotatef(rotation.getX(), 1.0f, 0.0f, 0.0f); // Rotate On The X Axis
        GL11.glRotatef(rotation.getY(), 0.0f, 1.0f, 0.0f); // Rotate On The Y Axis
        GL11.glRotatef(rotation.getZ(), 0.0f, 0.0f, 1.0f); // Rotate On The Z Axis
        GL11.glTranslatef(-rx,-ry, location.getZ());
	}

	
	public boolean intersects(Entity e){
		return getBound().intersects(e.getBound());
	}
	
	/**
	 * the render method can be implemented for every entity
	 */
	public void render(){}
	
	/**
	 * the update method can be implemented for every entity
	 */
	public void update(){}
	
	public abstract Rectangle getBound();
	
	public abstract Texture getTexture();
}
