package client.gui.element;

import client.gui.Formatter;
import client.renderer.Drawing;
import client.window.Button;
import common.util.Util;

public class Toggle extends Element {
	private final ToggleCallback func;
	private final boolean def;
	
	private boolean value;

	public Toggle(int x, int y, int w, int h, boolean def, boolean init, ToggleCallback callback, Formatter<Toggle> formatter) {
		super(x, y, w, h, formatter);
		this.func = callback;
		this.def = def;
		this.value = init;
		this.formatText();
	}

	public Toggle(int x, int y, int w, int h, boolean def, boolean init, ToggleCallback callback, final String text) {
		this(x, y, w, h, def, init, callback, new Formatter<Toggle>() {
			public String use(Toggle elem) {
				return String.format("%s: %s", text, elem.value ? "An" : "Aus");
			}
		});
	}

	
	public void mouse(Button btn, int x, int y, boolean ctrl, boolean shift) {
		boolean prev = this.value;
		if((this.value = (ctrl || (btn == Button.MOUSE_MIDDLE)) ? this.def : !this.value) != prev) {
//			this.type = this.value != 0 ? ElemType.TOGGLE_ON : ElemType.TOGGLE_OFF;
//			gui_update_style(this, 1);
//			this.r_dirty = true;
			if(this.func != null)
				this.func.use(this, this.value);
			this.formatText();
			this.playSound();
		}
	}
	
	protected void drawBackground() {
		if(this.value) {
			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));
		}
		else
			super.drawBackground();
	}
	
	public void setValue(boolean value) {
		if(this.value != value) {
			this.value = value;
			this.formatText();
		}
	}
	
	public boolean getValue() {
		return this.value;
	}
}
