package ei.engine.effects;

import java.awt.Rectangle;

import ei.engine.LWJGLGameWindow;
import ei.engine.math.Vector2f;
import ei.engine.math.Vector3f;
import ei.engine.scene.Entity;
import ei.engine.scene.Sprite;
import ei.engine.texture.Texture;

/**
 * This class is a progress meter
 * 
 * @author Ziver
 *
 */

public class ProgressBar extends Entity{
	// Temp things for the loadingbar
	private int value;
	private int max;
	private int min;
	// The textures
	private Sprite bar;
	private Sprite progress;
	private Sprite background;
	
	public ProgressBar(String name){
		super(name);
		value = 0;
		min = 0;
		max = 100;
	}
	
	/**
	 * Set the texture for the bar
	 * 
	 * @param s The texture for the bar
	 */
	public void setBarTexture(Sprite s){
		bar = s;
	}
	
	/**
	 * Set the texture for the progressbar
	 * 
	 * @param s The texture for the progressbar
	 */
	public void setProgressTexture(Sprite s){
		progress = s;
	}
	
	/**
	 * Set the texture for the background
	 * 
	 * @param s The texture for the background
	 */
	public void setBackgroundTexture(Sprite s){
		background = s;
	}
	
	/**
	 * Centers the bar to the screen
	 *
	 */
	public void centerToScreen(){
		setLocation(new Vector3f(
				(LWJGLGameWindow.getWidth()/2),
				(LWJGLGameWindow.getHeight()/2),0.0f));
	}
	
	/**
	 * set the location of this entity in pixels
	 * 
	 * @param l The location of the entity
	 */
	public void setLocation(Vector3f l) {
		super.setLocation(l);
		updateLocation();
	}
	
	/**
	 * set the location of this entity in pixels
	 * 
	 * @param l The location of the entity
	 */
	public void setLocation(Vector2f l) {
		super.setLocation(l);
		updateLocation();
	}
	
	private void updateLocation(){
		if(bar != null){
			bar.setLocation(getLocation());
		}
		if(progress != null){
			progress.setLocation(new Vector3f(
					(LWJGLGameWindow.getWidth()/2)-progress.getSize().getX(),
					(LWJGLGameWindow.getHeight()/2)+3,0.0f));
		}
		if(background != null){
			background.setLocation(getLocation());
		}
	}
	
	/**
	 * Sets the max value
	 * 
	 * @param max The max value
	 */
	public void setMax(int max){
		this.max = max;
		if(value > max){
			value = max;
		}
	}
	
	/**
	 * Returns the max value
	 * 
	 * @return The max value
	 */
	public int getMax(){
		return max;
	}
	
	/**
	 * Set the value of progress
	 * 
	 * @param v The value of progress
	 */
	public void setValue(int v){
		value = v;
		if(min > value){
			value = min;
		}
		if(value > max){
			value = max;
		}
	}
	
	/**
	 * Returns the value of the progress
	 * 
	 * @return The value of the progress
	 */
	public int getValue(){
		return value;
	}
	
	/**
	 * Set the min value
	 * 
	 * @param m The min value
	 */
	public void setMin(int m){
		min = m;
		if(min > value){
			value = min;
		}
	}
	
	/**
	 * Returns the min value
	 * 
	 * @return The min value
	 */
	public int getMin(){
		return min;
	}
	
	/**
	 * Returns the per cent of the progress
	 * 
	 * @return The per cent of the progress
	 */
	public int getProcent(){
		return (int)(((float)(value-min)/(max-min))*100);
	}
	
	/**
	 * Updates the progressbar
	 */
	public void update(){
		updateLocation();
		if(background != null)background.update();
		if(progress != null)progress.update();
		if(bar != null)bar.update();
	}
	
	/**
	 * Render the load bar
	 */
	public void render() {
		// Calculate the procentage
		float procent = (float)(value-min)/(max-min);
		//render the bar
		if(background != null)background.render();
		if(progress != null){
			progress.setLocation(new Vector3f(
					getLocation().getX()-progress.getSize().getX()+(progress.getSize().getX()*procent),
					getLocation().getY()+6,0.0f));
			progress.render();
		}
		if(bar != null)	bar.render();
	}

	@Override
	public Rectangle getBound() {
		if(bar != null){
			return bar.getBound();
		}
		else if(progress != null){
			return progress.getBound();
		}
		else{
			return null;
		}
	}

	@Override
	public Texture getTexture() {
		return bar.getTexture();
	}
}
