package client.gui.options;

import client.gui.Style;
import client.gui.element.ActButton;
import client.gui.element.ButtonCallback;
import client.gui.element.Element;
import client.gui.element.Field;
import client.gui.element.PressType;
import client.vars.CVar;
import client.vars.ColorVar;

public class GuiStyle extends GuiOptions {
	private static final String[] STYLE_CVARS = {
		"color_button_top",
		"color_button_btm",
		"color_button_text",
		
		"color_textbox_top",
		"color_textbox_btm",
		"color_textbox_text",
		
		"color_border_top",
		"color_border_btm",
		null,

		"color_label_text",
		null,
		null
	};
	
	protected GuiStyle() {
	}
	
	protected Element addSelector(String cvar, int x, int y, int w, int h) {
		CVar cv = this.gm.getVar(cvar);
		if(cv instanceof ColorVar color) {
			this.add(color.label(x, y, w));
			if(this.gm.style != Style.CUSTOM)
				return this.add(new Field(x, y, w, h, color.getFieldValue(this.gm.style)));
			else
				return this.add(color.editor(x, y, w, h));
		}
		return super.addSelector(cvar, x, y, w, h);
	}
	
	public void init(int width, int height) {
		for(int z = 0; z < STYLE_CVARS.length; z++) {
			if(STYLE_CVARS[z] != null)
				this.addSelector(STYLE_CVARS[z], (z / 3) * 120, (z % 3) * 34, 118, 0);
		}
		
		this.addSelector("gui_theme", 0, 3 * 34, 240, 0);
		if(this.gm.style != Style.CUSTOM)
			this.add(new ActButton(242, 3 * 34, 240, 0, new ButtonCallback() {
				public void use(ActButton elem, PressType action) {
					if(GuiStyle.this.gm.style != Style.CUSTOM) {
						GuiStyle.this.gm.style.copyToCustom();
						GuiStyle.this.gm.style = Style.CUSTOM;
						GuiStyle.this.gm.setDirty();
						GuiStyle.this.gm.show(GuiStyle.this);
					}
				}
			}, "In 'Angepasst' kopieren"));
		else
			this.add(new ActButton(242, 3 * 34, 240, 0, new ButtonCallback() {
				public void use(ActButton elem, PressType action) {
					GuiStyle.this.gm.style = Style.CUSTOM;
					for(String cvar : STYLE_CVARS) {
						if(cvar != null)
							GuiStyle.this.gm.getVar(cvar).setDefault();
					}
					GuiStyle.this.gm.setDirty();
					GuiStyle.this.gm.show(GuiStyle.this);
				}
			}, "'Angepasst' zurücksetzen"));

		this.addSelector("gui_scale", 0, 3 * 34 + 20, 240, 0);
		this.addSelector("gui_font", 242, 3 * 34 + 20, 240, 0);
		this.addSelector("gui_scale_items", 0, 3 * 34 + 40, 240, 0);
		this.addSelector("gui_scale_hotbar", 242, 3 * 34 + 40, 240, 0);
		super.init(width, height);
	}

	public String getTitle() {
		return "Benutzeroberfläche";
	}
}
