package client.gui.options;

import client.gui.Formatter;
import client.gui.element.ActButton;
import client.gui.element.ButtonCallback;
import client.gui.element.Label;
import client.gui.element.PressType;
import client.window.Bind;
import common.util.Color;

public class GuiBinds extends GuiOptions {
	protected GuiBinds() {
	}
	
	public void init(int width, int height) {
		int cols = 5;
		int y = 0;
		int x = 0;
		for(Bind bind : Bind.values()) {
			this.add(new Label(x * 152, 100 + y * 34, 150, bind.getDisplay()));
			this.add(new ActButton(x * 152, 100 + y * 34, 150, 0, new ButtonCallback() {
				public void use(ActButton elem, PressType action) {
					if(action == PressType.SECONDARY) {
						if(!bind.isDefault()) {
							bind.setDefault();
							GuiBinds.this.gm.setDirty();
							GuiBinds.this.reformat();
						}
					}
					else if(action == PressType.TERTIARY) {
						if(bind.getInput() != null) {
							bind.setInput(null);
							GuiBinds.this.gm.setDirty();
							GuiBinds.this.reformat();
						}
					}
					else {
						Bind.disableInput(bind);
					}
				}
			}, new Formatter<ActButton>() {
				public String use(ActButton elem) {
					if(bind == Bind.getWaiting())
						return Color.BLUE + "***";
					else
						return (bind.isDupe() ? Color.RED : Color.YELLOW) + (bind.getInput() == null ? "---" : bind.getInput().getDisplay());
				}
			}));
			if(++x == cols) {
				x = 0;
				y++;
			}
		}
		y += x != 0 ? 1 : 0;
		x = (150 * cols + 2 * (cols - 1) - 482) / 2;
		this.add(new ActButton(x, 96 + y * 34, 482, 0, new ButtonCallback() {
			public void use(ActButton elem, PressType action) {
				boolean flag = false;
				for(Bind bind : Bind.values()) {
					flag |= !bind.isDefault();
					bind.setDefault();
				}
				if(flag) {
					GuiBinds.this.gm.setDirty();
					GuiBinds.this.reformat();
				}
			}
		}, "Zurücksetzen"));
		this.addSelector("phy_sensitivity", x, 116 + y * 34, 240, 0);
		this.addSelector("gui_dclick_delay", x + 242, 116 + y * 34, 240, 0);
		super.init(width, height);
	}
	
	public String getTitle() {
		return "Tastenbelegung und Maus";
	}
}
