package zero1hd.rhythmbullet.graphics.ui.components;

import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.scenes.scene2d.actions.Actions;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.WidgetGroup;

import zero1hd.rhythmbullet.entity.ally.PolyjetEntity;

public class HealthBar extends WidgetGroup {
	Image empty;
	Image filler;
	float health;
	float maxHealth;
	
	PolyjetEntity pje;
	public HealthBar(Skin skin, float maxHealth) {
		super();
		filler = new Image(skin.getPatch("bar-fill"));
		addActor(filler);

		empty = new Image(skin.getPatch("bar-empty"));
		addActor(empty);

		this.maxHealth = maxHealth;
		
	}
	
	public void setPolyjetEntity(PolyjetEntity pje) {
		this.pje = pje;
	}
	
	public void setHealth(float health) {
		this.health = health;
		
		filler.addAction(Actions.sizeTo(getWidth(), MathUtils.round((health/maxHealth)*getHeight()), 0.1f));;
	}
	
	@Override
	public void act(float delta) {
		if (pje != null) {
			health = pje.health;
		}
		super.act(delta);
	}
	
	@Override
	public void setSize(float width, float height) {
		empty.setSize(width, height);
		filler.setSize(width, height);
		super.setSize(width, height);
	}
	
	@Override
	public void setWidth(float width) {
		empty.setWidth(width);
		filler.setWidth(width);
		super.setWidth(width);
	}
	
	@Override
	public void setHeight(float height) {
		empty.setHeight(height);
		super.setHeight(height);
	}
	
	public float getMaxHealth() {
		return maxHealth;
	}
}
