package client.gui.options;

import java.util.List;

import client.Client;
import client.gui.Font;
import client.gui.Formatter;
import client.gui.element.Dropdown;
import client.gui.element.DropdownCallback;
import client.gui.element.Element;
import client.gui.element.Fill;
import client.gui.element.Slider;
import client.gui.element.SliderCallback;
import client.gui.element.Toggle;
import client.gui.element.ToggleCallback;
import client.window.Button;
import client.window.DisplayMode;
import client.window.Window;
import common.collect.Lists;
import common.util.ExtMath;
import common.util.Color;

public class GuiDisplay extends GuiOptions {
	private static final String[] DISTANCES = new String[] {"Gruselig", "Winzig", "Gering", "Normal", "Weit"};
	
	private Element distanceSlider;
	
	protected GuiDisplay() {
	}
	
	public void init(int width, int height) {
		this.add(new Toggle(0, 0, 240, 0, false, GuiDisplay.this.gm.fullscreen, new ToggleCallback() {
			public void use(Toggle elem, boolean value) {
				GuiDisplay.this.gm.full(value);
			}
		}, "Vollbild"));
		int maxModes = ExtMath.clampi((height - ((3 * 20 + 2) * 2 + 18 + 4)) / Font.HEIGHT, 4, 28);
		DisplayMode[] dmodes = Window.getDisplayModes();
		if(dmodes != null) {
			List<DisplayMode> modes = Lists.newArrayList();
			for(int z = dmodes.length - 1; z >= 0 && modes.size() < maxModes; z--) {
				DisplayMode mode = dmodes[z];
				if(mode.width() >= Client.MIN_WIDTH && mode.height() >= Client.MIN_HEIGHT)
					modes.add(0, mode);
			}
			if(modes.isEmpty()) {
				dmodes = null;
			}
			else {
				DisplayMode selected = modes.get(modes.size() - 1);
				for(DisplayMode mode : modes) {
					if(mode.equals(this.gm.vidMode))
						selected = mode;
				}
				this.add(new Dropdown<DisplayMode>(242, 0, 240, 0, false, modes.toArray(new DisplayMode[modes.size()]), modes.get(modes.size() - 1), selected, new DropdownCallback<DisplayMode>() {
					public void use(Dropdown<DisplayMode> elem, DisplayMode value) {
						GuiDisplay.this.gm.vidMode = value;
						GuiDisplay.this.gm.full(true);
					}
				}, (String)null));
			}
		}
		if(dmodes == null)
			this.add(new Fill(242, 0, 240, 0, Color.RED + "<?>"));

		this.add(new Slider(0, 20, 240, 0, 0, 0, 360 - 8, 0, (this.gm.sync < 0) ? (360 - 8) : (this.gm.sync != 0 ? ((this.gm.sync < 10) ? 1 : (this.gm.sync - 9)) : 0), new SliderCallback() {
			public void use(Slider elem, int value) {
				GuiDisplay.this.gm.getVar("win_sync").parse("" + ((value > 0 && value < 360 - 8) ? (value + 9) : (value != 0 ? -1 : 0)));
				GuiDisplay.this.gm.setDirty();
			}
		}, new Formatter<Slider>() {
			public String use(Slider elem) {
				int value = elem.getValue();
				return "Max. Bildrate: " + (value > 0 && value < (360 - 8) ? (value + 9) + " FPS" : (value != 0 ? "Unbegrenzt" : "VSync"));
			}
		}));
		this.addSelector("gl_vsync_flush", 242, 20, 240, 0);
		
		this.addSelector("overlay_enabled", 0, 50, 240, 0);
		this.addSelector("overlay_opacity", 242, 50, 240, 0);
		
		this.addSelector("overlay_fadeout", 0, 70, 240, 0);
		this.addSelector("chat_permanent", 242, 70, 240, 0);
		
		this.addSelector("console_size", 0, 90, 240, 0);
		this.addSelector("chat_size", 242, 90, 240, 0);
		
		this.addSelector("feed_size", 0, 110, 240, 0);
		this.addSelector("hotbar_size", 242, 110, 240, 0);

		this.addSelector("gl_fov", 0, 140, 240, 0);
		
		this.distanceSlider = this.addSelector("chunk_view_distance", 0, 160, 240, 0);
		this.addSelector("chunk_build_time", 242, 160, 240, 0);
		super.init(width, height);
	}
	
	public String getTitle() {
		return "Grafik und Anzeige";
	}
	
    private String getDistanceName() {
    	int distance = this.gm.renderDistance;
    	distance = distance > 16 ? 16 : distance;
    	String str = distance < 0 ? DISTANCES[0] : DISTANCES[(distance + 1) / 4];
    	if(distance > 2 && (((distance + 1) / 2) & 1) == 1)
    		str = str + "+";
    	return String.format("Sichtweite: %d [%d, %s]", this.gm.renderDistance, this.gm.renderDistance * 16, str);
    }
	
	public void updateScreen() {
		if(!Button.isMouseDown())
			this.distanceSlider.setText(this.getDistanceName());
	}
}
