package client.gui.element;

import client.gui.Font;
import client.renderer.Drawing;
import common.util.ExtMath;
import common.util.Util;

public class Bar extends Fill {
	private final Font font;
	
	private int color = 0x00ff00;
	private int amount = 0;
	
	public Bar(int x, int y, int w, int h, Font font, boolean top, boolean left) {
		super(x, y, w, h, top, left);
		this.font = font;
		this.setText("");
	}

	public Bar(int x, int y, int w, int h, Font font, boolean left) {
		this(x, y, w, h, font, false, left);
	}
	
	public Bar(int x, int y, int w, int h, Font font) {
		this(x, y, w, h, font, false, false);
	}

	protected void drawBackground() {
		if(this.enabled)
			Drawing.drawGradientBorder(this.pos_x, this.pos_y, this.size_x, this.size_y, this.gm.style.fill_btm, this.gm.style.fill_top, 0xff000000, this.gm.style.brdr_top, this.gm.style.brdr_btm);
		else
			Drawing.drawGradientBorder(this.pos_x, this.pos_y, this.size_x, this.size_y, Util.mulColor(this.gm.style.fill_btm, 0.5f), Util.mulColor(this.gm.style.fill_top, 0.5f), 0xff000000, Util.mulColor(this.gm.style.brdr_top, 0.5f), Util.mulColor(this.gm.style.brdr_btm, 0.5f));
		if(this.amount > 0) {
			if(this.enabled)
				Drawing.drawGradient(this.pos_x + 2, this.pos_y + 2, this.amount, this.size_y - 4, this.color | 0xff000000, Util.mixColor(this.color | 0xff000000, 0xff000000));
			else
				Drawing.drawGradient(this.pos_x + 2, this.pos_y + 2, this.amount, this.size_y - 4, Util.mulColor(this.color | 0xff000000, 0.5f), Util.mulColor(Util.mixColor(this.color | 0xff000000, 0xff000000), 0.5f));
		}
	}
	
	protected void drawForeground(int x1, int y1, int x2, int y2) {
		Font.set(this.font);
		Drawing.drawText(this.text, x1 + this.text_x, y1 + this.text_y, this.enabled ? this.gm.style.text_label : Util.mulColor(this.gm.style.text_label, 0.5f));
		Font.unset();
	}
	
	public void updateText() {
		Font.set(this.font);
		super.updateText();
		Font.unset();
	}
	
	protected int getMarginX() {
		return 0;
	}
	
	protected int getMarginY() {
		return 0;
	}
	
	public Bar setColor(int color) {
		this.color = color;
		return this;
	}
	
	public void setProgress(int total, int progress) {
		this.amount = progress < 0 || total <= 0 ? 0 : ExtMath.clampi((int)(((float)progress / (float)total) * (float)(this.size_x - 4)), 0, this.size_x - 4);
	}
}
