package ei.engine.scene;

import java.awt.Rectangle;

import org.lwjgl.opengl.GL11;

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

/**
 * Implementation of sprite that uses an OpenGL quad and a texture
 * to render a given image to the screen.
 * 
 * @author Kevin Glass
 * @author Brian Matzon
 * @author Ziver Koc
 */
public class Sprite extends Entity {
	/** The texture that stores the image for this sprite */
	private Texture texture;
	/** The size of this entity */
	private Vector2f size;
	/**
	 * Create a new empty sprite
	 * 
	 * @param window The window in which the sprite will be displayed
	 * @param ref A reference to the image on which this sprite should be based
	 */
	public Sprite(String name) {
		super(name);		
		texture = new Texture();
		size = new Vector2f();
	}
	
	/**
	 * Create a new sprite from a specified texture.
	 * 
	 * @param name The name of the sprite
	 * @param texture The texture to use
	 */
	public Sprite(String name, Texture texture) {
		super(name);
		this.texture = texture;
		size = new Vector2f(texture.getImageWidth(),texture.getImageHeight());
	}
	
	/**
	 * Create a new sprite from a specified image.
	 * 
	 * @param name The name of the sprite
	 * @param ref A reference to the image on which this sprite should be based
	 */
	public Sprite(String name, String ref) {
		super(name);
		this.texture = TextureLoader.getTextureLoaderInstance().getTexture(ref);
		size = new Vector2f(texture.getImageWidth(),texture.getImageHeight());
	}
	

	/**
	 * Returns a vector2f with the size
	 * 
	 * @return a vector2f with the size
	 */
	public Vector2f getSize(){
		return size;
	}
	
	/**
	 * Sets the size to the given values
	 * 
	 * @param s A Vector2f with the size values
	 */
	public void setSize(Vector2f s){
		size = s;
	}
	
	/**
	 * Returns the texture
	 * 
	 * @return The texture
	 */
	public Texture getTexture(){
		return texture;
	}

	/**
	 * Draw the sprite
	 */
	public void render() {
		if(texture != null){
			// store the current model matrix
			GL11.glPushMatrix();
	
			//Sets the location
			super.setTranslationGL();
			//the rotation
			super.setRotationGL(size.getX()/2,size.getY()/2);
			//Sets the scale of the sprite
			super.setScaleGL();
			
			// bind to the appropriate texture for this sprite
			texture.bindGL();
			
			// draw a quad textured to match the sprite
			GL11.glBegin(GL11.GL_QUADS);
			{
				//Vertex at the upper left
				GL11.glTexCoord2f(0, 0);
				GL11.glVertex2f(0, 0);
				//Vertex at the down left
				GL11.glTexCoord2f(0, texture.getHeight());
				GL11.glVertex2f(0, size.getY());
				//Vertex at the upper right
				GL11.glTexCoord2f(texture.getWidth(), texture.getHeight());
				GL11.glVertex2f(size.getX(),size.getY());
				//Vertex at the down right
				GL11.glTexCoord2f(texture.getWidth(), 0);
				GL11.glVertex2f(size.getX(),0);
			}
			GL11.glEnd();
			
			// restore the model view matrix to prevent contamination
			GL11.glPopMatrix();
		}
	}
	
	/**
	 * Returns the bound of this class
	 */
	public Rectangle getBound() {
		return new Rectangle(
				(int)getLocation().getX(),
				(int)getLocation().getY(),
				(int)size.getX(),
				(int)size.getY());
	}
	
}