package projectzombie.menu;

import gl_engine.vec.Vec2d;
import projectzombie.Main;
import projectzombie.display.DisplayRender;
import projectzombie.display.DisplayRenderUI;
import projectzombie.display.DisplayWindow;
import projectzombie.input.types.InputGUI;
import projectzombie.menu.gui.GUI;
import projectzombie.menu.gui.GUIBackToMenu;
import projectzombie.menu.gui.GUIButton;
import projectzombie.menu.gui.GUIButtonBasic;
import projectzombie.menu.gui.GUIButtonGroup;
import projectzombie.menu.gui.GUIButtonSetting;
import projectzombie.menu.gui.GUILabel;
import projectzombie.menu.gui.GUISelectableDirection;
import projectzombie.settings.SettingQuality;
import projectzombie.settings.Settings;
import projectzombie.world.chunk.Chunk;

public class MenuSettings extends Menu
{
	public GUIButtonSetting buttonFullscreen;
	
	private GUI gui;
	private Menu menuOld;
	
	public static String render(SettingQuality setting) {
		switch(setting)
		{
		case HIGH: return "High";
		case MEDIUM: return "Medium";
		case LOW: return "Low";
		case EXTREME: return "Extreme";
		case LOWEST: return "Lowest";
		default: return "Unknown";
		}
	}
	
	public MenuSettings(Menu menuOld) {
		
		this.menuOld = menuOld;
		
		doGameloop = false;
		doGameRender = menuOld.doGameRender;
		showIngameGUI = menuOld.showIngameGUI;
		keepMouse = false;
		
		GUIButtonGroup group = new GUIButtonGroup();
		group.setPos(new Vec2d(-1, 4));
		
		group.add(new GUIButtonSetting("FPS: " + (DisplayRenderUI.showFPS ? "On" : "Off"), button -> {
			DisplayRenderUI.showFPS = !DisplayRenderUI.showFPS;
			button.setText("FPS: " + (DisplayRenderUI.showFPS ? "On" : "Off"));
			Settings.update();
		}));
		
		group.add(new GUIButtonSetting("VSync: " + (DisplayWindow.vsync ? "On" : "Off"), button -> {
			DisplayWindow.setVSync(!DisplayWindow.vsync);
			button.setText("VSync: " + (DisplayWindow.vsync ? "On" : "Off"));
			Settings.update();
		}));
		
		group.add(buttonFullscreen = new GUIButtonSetting("Fullscreen: " + (DisplayWindow.fullscreen ? "On" : "Off"),
				button -> {
			Main.window.toggleFullscreen();
			button.setText("Fullscreen: " + (DisplayWindow.fullscreen ? "On" : "Off"));
		}));
		
		group.add(new GUIButtonSetting("Debug mode: " + (DisplayRenderUI.debug ? "On" : "Off"),
				button ->
		{
			DisplayRenderUI.debug = !DisplayRenderUI.debug;
			button.setText("Debug mode: " + (DisplayRenderUI.debug ? "On" : "Off"));
			Settings.update();
		}));
		
		group.add(new GUIButtonSetting("Render distance: "+Chunk.RENDER_DISTANCE, button -> {
			Chunk.RENDER_DISTANCE += 1;
			if(Chunk.RENDER_DISTANCE > 8) {
				Chunk.RENDER_DISTANCE = 2;
			}
			button.setText("Render distance: "+Chunk.RENDER_DISTANCE);
			Settings.update();
		}));
		
		group.add(new GUIButtonSetting("Shadows: " + render(DisplayRender.getShadowQuality()), button ->
		{
			SettingQuality quality = DisplayRender.getShadowQuality();
			
			switch(quality)
			{
			case LOWEST:
				quality = SettingQuality.LOW;
				break;
			case LOW:
				quality = SettingQuality.MEDIUM;
				break;
			case MEDIUM:
				quality = SettingQuality.HIGH;
				break;
			case HIGH:
				quality = SettingQuality.EXTREME;
				break;
			case EXTREME:
				quality = SettingQuality.LOWEST;
				break;
			}
			
			quality = DisplayRender.setShadowQuality(quality);
			
			DisplayRender.updateShadowQuality();
			Settings.update();
			
			button.setText("Shadows: " + render(quality));
		}));
		
		/*group.add(new ButtonSetting("Dynamic Lighting: " + (LightingManager.lightingMode == 0 ? "Fast" : "Fancy"),
				button ->
		{
			if(LightingManager.lightingMode == 0) {
				LightingManager.lightingMode = 1;
				
			} else {
				LightingManager.lightingMode = 0;
			}
			
			button.setText("Dynamic Lighting: " + (LightingManager.lightingMode == 0 ? "Fast" : "Fancy"));
		}));*/
		
		gui = new GUIBackToMenu(menuOld);
		input = new InputGUI(gui);
		
		gui.add(group);
		
		GUILabel labelSettings = new GUILabel();
		labelSettings.setText("Settings");
		labelSettings.setSize(new Vec2d(1, 1));
		labelSettings.setPos(new Vec2d(0, 6.8));
		gui.add(labelSettings);
		
		GUIButton buttonBack = new GUIButtonBasic("Back", new Vec2d(0, -8), button -> {
			Main.menu = menuOld;
		});
		
		group.get(group.size() - 1).setNeighbour(buttonBack, GUISelectableDirection.DOWN);
		buttonBack.setNeighbour(group.get(group.size() - 1), GUISelectableDirection.UP);
		
		gui.add(buttonBack);
		gui.setSelected(buttonBack);
	}

	@Override
	public void render() {
		gui.render();
	}
	
	@Override
	public void update() {
		super.update();
		this.menuOld.update();
	}

}
