package client.gui.options;

import client.gui.Style;
import client.gui.element.ActButton;
import client.gui.element.ButtonCallback;
import client.gui.element.Dropdown;
import client.gui.element.DropdownCallback;
import client.gui.element.Element;
import client.gui.element.SelectableButton;
import client.gui.element.Slider;
import client.gui.element.SliderCallback;
import client.gui.element.Switch;
import client.gui.element.SwitchCallback;
import client.gui.element.FieldAction;
import client.gui.element.Field;
import client.gui.element.FieldCallback;
import client.gui.element.PressType;
import client.vars.CVar;
import client.vars.ColorVar;
import client.gui.element.Toggle;
import client.gui.element.ToggleCallback;

public class GuiStyle extends GuiOptions implements DropdownCallback<String>, ButtonCallback, ToggleCallback, SwitchCallback<String>, SliderCallback, FieldCallback {
	private static final String[] STYLE_CVARS = {
		"color_button_top",
		"color_textbox_top",
		"color_border_top",
		
		"color_button_btm",
		"color_textbox_btm",
		"color_border_btm",

		"color_button_text",
		"color_textbox_text",
		"color_label_text"
	};
	
	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) {
			if(!color.getDisplay().isEmpty())
				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) {
		int z;
		for(z = 0; z < STYLE_CVARS.length; z++) {
			this.addSelector(STYLE_CVARS[z], (z % 3) * 122, z / 3 * 34, 120, 0);
		}
		z = 0;
		for(Style theme : Style.values()) {
			ButtonCallback callback = new ButtonCallback() {
				public void use(ActButton elem, PressType action) {
					if(GuiStyle.this.gm.style != theme) {
						GuiStyle.this.gm.style = theme;
						GuiStyle.this.gm.setDirty();
						GuiStyle.this.gm.displayGuiScreen(GuiStyle.this);
					}
				}
			};
			this.add( // theme == this.gm.style ? new SelectedButton(10 + (z % 3) * 320, 360 + (z / 3) * 40, 300, 0, callback, theme.name) : 
					new SelectableButton((z % 3) * 122, 4 * 34 + (z / 3) * 20, 120, 0, callback, theme.name, theme == this.gm.style));
			z++;
		}

		this.addSelector("gui_scale", 0, 5 * 34 + (Style.values().length / 3) * 20, 181, 0);
		this.addSelector("gui_font_tiny", 183, 5 * 34 + (Style.values().length / 3) * 20, 181, 0);
		
		if(this.gm.style != Style.CUSTOM) {
			this.add(new ActButton(0, 3 * 34, 364, 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.displayGuiScreen(GuiStyle.this);
					}
				}
			}, "In angepasstes Design kopieren"));
		}
		else {
			this.add(new ActButton(0, 3 * 34, 364, 0, new ButtonCallback() {
				public void use(ActButton elem, PressType action) {
					GuiStyle.this.gm.style = Style.CUSTOM;
					for(String cvar : STYLE_CVARS) {
						GuiStyle.this.gm.getVar(cvar).setDefault();
					}
					GuiStyle.this.gm.setDirty();
					GuiStyle.this.gm.displayGuiScreen(GuiStyle.this);
				}
			}, "Angepasstes Design zurücksetzen"));
		}
		super.init(width, height);
	}

	public String getTitle() {
		return "Benutzeroberfläche";
	}
	
	public void use(Field elem, FieldAction value) {
	}
	public void use(Slider elem, int value) {
	}
	public void use(Switch<String> elem, String value) {
	}
	public void use(Toggle elem, boolean value) {
	}
	public void use(ActButton elem, PressType action) {
	}
	public void use(Dropdown<String> elem, String value) {
	}
}
