package client.gui.element;

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

public class Bar extends Fill {
	private int color = 0x00ff00;
	private int progress = 0;
	private int total = 0;
	private String desc = "";
	private String unit = "";
	
	public Bar(int x, int y, int w, int h, boolean top, boolean left) {
		super(x, y, w, h, "", top, left);
	}

	public Bar(int x, int y, int w, int h, boolean left) {
		super(x, y, w, h, "", left);
	}
	
	public Bar(int x, int y, int w, int h) {
		super(x, y, w, h, "");
	}

	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.progress > 0) {
			if(this.enabled)
				Drawing.drawGradient(this.pos_x + 2, this.pos_y + 2, this.progress, 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.progress, 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) {
		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));
	}
	
	protected int getMarginX() {
		return 0;
	}
	
	protected int getMarginY() {
		return 0;
	}
	
	public Bar setDescription(String desc, String unit, int total) {
		this.desc = desc == null ? "" : (desc + ": ");
		this.unit = unit == null ? "" : (" " + unit);
		this.total = total;
		return this;
	}
	
	public Bar setDescription(String desc, int total) {
		return this.setDescription(desc, null, total);
	}
	
	public Bar setDescription(String desc) {
		this.desc = desc == null ? "" : (desc + ": ");
		this.unit = "";
		this.total = 0;
		return this;
	}
	
	public Bar resetProgress() {
		this.setText("");
		this.progress = 0;
		this.setDescription(null);
		return this;
	}
	
	public Bar setProgressFill(int progress) {
		this.progress = ExtMath.clampi(progress, 0, this.size_x - 4);
		return this;
	}
	
	public Bar setProgressFill(float progress) {
		return this.setProgressFill((int)(progress * (float)(this.size_x - 4)));
	}
	
	public Bar setProgress(float progress) {
		if(this.total == 0)
			this.setText(String.format("%s%.1f %%", this.desc, progress * 100.0f));
		else
			this.setText(String.format("%s%d/%d%s (%.1f %%)", this.desc, (int)progress, this.total, this.unit, (progress / (float)this.total) * 100.0f));
		return this.setProgressFill(this.total == 0 ? progress : (progress / (float)this.total));
	}
	
	public Bar setColor(int color) {
		this.color = color;
		return this;
	}
}
