package ei.engine.effects;

import ei.engine.math.Vector2f;
import ei.engine.math.Vector4f;
import ei.engine.scene.Box;
import ei.engine.scene.Node;
import ei.engine.scene.Sprite;
import ei.engine.texture.Texture;

public class ProgressBar{
	private Node progressNode;
	private Sprite value;
	private Box bar;
	private int max;
	private float width;
	private float height;
	private Vector4f[] valueColor = new Vector4f[100];
	
	/**
	 * Creates a selection box with health bar
	 * 
	 * @param x The size of the progressbar
	 * @param y The size of the progressbar
	 * @param max The max health of the object
	 */
	public ProgressBar(String name, float x, float y, int max){
		this.max = max;
		this.width = x;
		this.height = y;
		progressNode = new Node(name+"Node");
		
		Texture tex = new Texture();
		tex.setColor(new Vector4f(0.5f, 1.0f, 0.5f,1));

		Texture tex2 = new Texture();
		tex2.setColor(new Vector4f(0.3f, 1.0f, 0.3f, 0.6f));
				
		value = new Sprite(name+"_Value",tex2);
		value.setSize(new Vector2f(width,height));
		progressNode.add(value);
		
		bar = new Box(name+"_Bar",tex);
		bar.setSize(new Vector2f(width,height));
		progressNode.add(bar);
				
	}
	
	public void setBarColor(Vector4f c){
		bar.getTexture().setColor(c);
	}
	
	public void setValueColor(int procent, Vector4f c){
		procent--;
		if(procent >= 0 && procent <= 99){
			valueColor[procent] = c;
		}
	}
	
	public Node getNode(){
		return progressNode;
	}
	
	/**
	 * Sets the value of the health bar
	 * 
	 * @param v The value of the health bar
	 */
	public void setValue(int v){
		if(v < 0)v = 0;
		if(v > max)v = max;
		value.setSize(new Vector2f(((float)v/max)*width,height));//(v/max)*width
		value.setLocation(new Vector2f(-width/2+(((float)v/max)*width)/2,0));
		
		for(int i=0; i<valueColor.length ;i++){
			if(valueColor[i] != null && ((float)v/max) < (float)i/100){
				value.getTexture().setColor(valueColor[i]);
				break;
			}
		}
	}

}
