package speiger.src.coreengine.rendering.guiOld.components;

import java.util.function.IntFunction;
import java.util.function.IntSupplier;

import speiger.src.coreengine.rendering.guiOld.GuiComponent;
import speiger.src.coreengine.rendering.guiOld.helper.box.IGuiBox;
import speiger.src.coreengine.rendering.guiOld.helper.box.ParentBox;
import speiger.src.coreengine.rendering.guiOld.helper.constrains.Constrains;
import speiger.src.coreengine.rendering.guiOld.renderer.UIRenderer;

public class ProgressBarComponent extends GuiComponent
{
	IGuiBox inner = new ParentBox(1F);
	TextComponent text = new TextComponent();
	int color;
	int value;
	int max;
	IntFunction<String> name;
	IntSupplier valueGenerator;
	
	public ProgressBarComponent(int color, int value, int max)
	{
		this(color, value, max, null);
	}
	
	public ProgressBarComponent(int color, float textScale, int value, int max)
	{
		this(color, textScale, value, max, null);
	}
	
	public ProgressBarComponent(int color, int value, int max, IntFunction<String> name)
	{
		this(0F, 0F, 0F, 0F, color, value, max, name);
	}
	
	public ProgressBarComponent(int color, float textScale, int value, int max, IntFunction<String> name)
	{
		this(0F, 0F, 0F, 0F, color, textScale, value, max, name);
	}
	
	public ProgressBarComponent(float x, float y, float width, float height, int color, int value, int max)
	{
		this(x, y, width, height, color, value, max, null);
	}
	
	public ProgressBarComponent(float x, float y, float width, float height, int color, float textScale, int value, int max)
	{
		this(x, y, width, height, color, textScale, value, max, null);
	}
	
	public ProgressBarComponent(float x, float y, float width, float height, int color, int value, int max, IntFunction<String> name)
	{
		this(x, y, width, height, color, 1F, value, max, name);
	}
	
	public ProgressBarComponent(float x, float y, float width, float height, int color, float textScale, int value, int max, IntFunction<String> name)
	{
		super(x, y, width, height);
		this.name = name;
		text.setTextScale(textScale);
		setColor(color);
		setMax(max);
		setValue(value);
	}
	
	@Override
	public void init()
	{
		addBox(inner);
		text.setZOffset(0.1F);
		text.setText(stringify());
		addChild(text, Constrains.parent());
	}
	
	public TextComponent getText()
	{
		return text;
	}
	
	public ProgressBarComponent setColor(int color)
	{
		this.color = color;
		return this;
	}
	
	public ProgressBarComponent setValueGenerator(IntSupplier valueGenerator)
	{
		this.valueGenerator = valueGenerator;
		return this;
	}
	
	public ProgressBarComponent setTextGenerator(IntFunction<String> name)
	{
		this.name = name;
		return this;
	}
	
	public ProgressBarComponent setValue(int newValue)
	{
		if(value != newValue)
		{
			value = newValue;
			text.setText(stringify());
		}
		return this;
	}
	
	public ProgressBarComponent setMax(int newMax)
	{
		if(max != newMax)
		{
			max = newMax;
			if(value > max) setValue(max);
		}
		return this;
	}
	
	protected String stringify()
	{
		if(name != null) return name.apply(value);
		else if(max == 0) return "0%";
		return Integer.toString((int)(((float)value / (float)max) * 100F))+"%";
	}
	
	protected float getProgress()
	{
		return max == 0 ? 0F : (float)value / (float)max;
	}
	
	public int getValue()
	{
		return value;
	}
	
	public int getMax()
	{
		return max;
	}
	
	@Override
	protected boolean fixedUpdateSelf()
	{
		if(valueGenerator != null) setValue(valueGenerator.getAsInt());
		return true;
	}
	
	@Override
	protected boolean renderSelf(int mouseX, int mouseY, float particalTicks)
	{
		float extra = (inner.getMaxX() - inner.getMinX()) * getProgress();
		UIRenderer render = getRenderer();
		render.setBrightness(getActiveBrightness() * 0.75F).drawQuad(getBox(), color);
		render.setBrightness(getActiveBrightness()).drawQuad(inner, 0.01F, color);
		render.setBrightness(getActiveBrightness() * 1.25F).drawQuad(inner.getMinX(), inner.getMinY(), inner.getMinX() + extra, inner.getMaxY(), 0.02F, color);
		text.setBrightness(getActiveBrightness());
		return true;
	}
}
