package ei.engine.texture;

import java.util.HashMap;


/**
 * Uses a sequence of images to "Animate"
 * 
 * @author Ziver Koc
 */
public class AnimatedTexture extends Texture {
	/** The texture that stores the image for this sprite */
	private HashMap<String,Texture[]> textures;
	private String currentAnimation;
	private int textureId;

	/**
	 * Create a new empty AnimatedSprite
	 * 
	 * @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 AnimatedTexture(String name) {
		super();
		textures = new HashMap<String,Texture[]>();
		currentAnimation = null;
		textureId = -1;
	}

	/**
	 * Adds a vector of textures to the AnimationSprite
	 * 
	 * @param name The name of the animation
	 * @param t The vector of textures
	 * @return true if successful else false
	 */
	public boolean addAnimation(String name, Texture[] t){
		if(!textures.containsKey(name)){
			textures.put(name,t);
			return true;
		}
		return false;
	}

	/**
	 * Adds a vector of textures to the AnimationSprite
	 * 
	 * @param name The name of the animation
	 * @param t The path to the textures
	 * @return true if successful else false
	 */
	public boolean addAnimation(String name, String[] s){
		Texture[] t = new Texture[s.length];
		
		for(int i=0; i<s.length ;i++){
			t[i] = TextureLoader.getTextureLoaderInstance().getTexture(s[i]);
		}
		
		return addAnimation(name,t);
	}
	
	/**
	 * Play an animation
	 * 
	 * @param name The name of the animation
	 * @return true if the animation exists else false
	 */
	public boolean playAnimation(String name){
		if(textures.containsKey(name)){
			currentAnimation = name;
			textureId = 0;
			return true;
		}
		return false;
	}
	   
    /**
     * Bind the specified GL context to a texture
     *
     * @param gl The GL context to bind to
     */
    public void bindGL() {
    	if(currentAnimation != null){
    		textures.get(currentAnimation)[textureId].bindGL();
    		textureId++;
    		if(textures.get(currentAnimation).length >= textureId){
    			textureId = 0;
    		}
    	}
    }
    
    /**
     * Set the height of the image
     *
     * @param height The height of the image
     */
    public void setHeight(int height) {
        textures.get(currentAnimation)[textureId].setHeight(height);
    }
    
    /**
     * Set the width of the image
     *
     * @param width The width of the image
     */
    public void setWidth(int width) {
    	textures.get(currentAnimation)[textureId].setWidth(width);
    }
    
    /** 
     * Get the target of the buffer
     *
     * @return The target of the buffer
     */
    public int getGLTarget() {
        return textures.get(currentAnimation)[textureId].getGLTarget();
    }
    
    /**
     * Get the height of the original image
     *
     * @return The height of the original image
     */
    public int getImageHeight() {
        return textures.get(currentAnimation)[textureId].getImageHeight();
    }
    
    /** 
     * Get the width of the original image
     *
     * @return The width of the original image
     */
    public int getImageWidth() {
        return textures.get(currentAnimation)[textureId].getImageWidth();
    }
    
    /**
     * Get the height of the physical texture
     *
     * @return The height of physical texture
     */
    public float getHeight() {
        return textures.get(currentAnimation)[textureId].getHeight();
    }
    
    /**
     * Get the width of the physical texture
     *
     * @return The width of physical texture
     */
    public float getWidth() {
        return textures.get(currentAnimation)[textureId].getWidth();
    }
    
    /**
     * Set the height of this texture 
     *
     * @param texHeight The height of the texture
     */
    public void setTextureHeight(float texHeight) {
        textures.get(currentAnimation)[textureId].setTextureHeight(texHeight);
    }
    
    /**
     * Set the width of this texture 
     *
     * @param texWidth The width of the texture
     */
    public void setTextureWidth(float texWidth) {
    	textures.get(currentAnimation)[textureId].setTextureWidth(texWidth);
    }
}