package projectzombie.settings;

import projectzombie.util.ClassBdf;
import projectzombie.util.FileHelpers;
import projectzombie.worker.WorkerTasks;
import bdf.types.BdfNamedList;
import bdf.types.BdfObject;
import bdf.types.BdfReader;
import bdf.types.BdfTypes;
import projectzombie.display.DisplayRender;
import projectzombie.display.DisplayRenderUI;
import projectzombie.display.DisplayWindow;
import projectzombie.world.chunk.Chunk;

public class Settings implements ClassBdf
{
	public static final Settings SETTINGS = new Settings();
	private static BdfReader READER;

	@Override
	public void BdfClassLoad(BdfObject bdf) {
		BdfNamedList nl = bdf.getNamedList();
		
		if(nl.get("render_distance").getType() == BdfTypes.INTEGER) {
			Chunk.RENDER_DISTANCE = nl.get("render_distance").getInteger();
		} else {
			Chunk.RENDER_DISTANCE = 2;
		}
		
		if(nl.get("show_fps").getType() == BdfTypes.BOOLEAN) {
			DisplayRenderUI.showFPS = nl.get("show_fps").getBoolean();
		} else {
			DisplayRenderUI.showFPS = false;
		}
		
		if(nl.get("debug").getType() == BdfTypes.BOOLEAN) {
			DisplayRenderUI.debug = nl.get("debug").getBoolean();
		} else {
			DisplayRenderUI.debug = false;
		}
		
		if(nl.get("fullscreen").getType() == BdfTypes.BOOLEAN) {
			DisplayWindow.fullscreen = nl.get("fullscreen").getBoolean();
		} else {
			DisplayWindow.fullscreen = true;
		}
		
		if(nl.get("vsync").getType() == BdfTypes.BOOLEAN) {
			DisplayWindow.vsync = nl.get("vsync").getBoolean();
		} else {
			DisplayWindow.vsync = true;
		}
		
		if(nl.get("shadow_size").getType() == BdfTypes.INTEGER)
		{
			SettingQuality quality = DisplayRender.getShadowQuality();
			
			switch(nl.get("shadow_size").getInteger())
			{
			case 0:
				quality = SettingQuality.LOWEST;
				break;
			case 1:
				quality = SettingQuality.LOW;
				break;
			case 2:
				quality = SettingQuality.MEDIUM;
				break;
			case 3:
				quality = SettingQuality.HIGH;
				break;
			case 4:
				quality = SettingQuality.EXTREME;
				break;
			default:
				quality = SettingQuality.LOW;
				break;
			}
			
			DisplayRender.setShadowQuality(quality);
		}
		
		else {
			DisplayRender.setShadowQuality(SettingQuality.LOW);
		}
	}

	@Override
	public void BdfClassSave(BdfObject bdf)
	{
		int shadow_size;
		
		switch(DisplayRender.getShadowQuality())
		{
		case LOWEST:
			shadow_size = 0;
			break;
		case LOW:
			shadow_size = 1;
			break;
		case MEDIUM:
			shadow_size = 2;
			break;
		case HIGH:
			shadow_size = 3;
			break;
		case EXTREME:
			shadow_size = 4;
			break;
		default:
			shadow_size = 1;
			break;
		}
		
		BdfNamedList nl = bdf.getNamedList();
		nl.set("render_distance", bdf.newObject().setInteger(Chunk.RENDER_DISTANCE));
		nl.set("show_fps", bdf.newObject().setBoolean(DisplayRenderUI.showFPS));
		nl.set("debug", bdf.newObject().setBoolean(DisplayRenderUI.debug));
		nl.set("shadow_size", bdf.newObject().setInteger(shadow_size));
		nl.set("fullscreen", bdf.newObject().setBoolean(DisplayWindow.fullscreen));
		nl.set("vsync", bdf.newObject().setBoolean(DisplayWindow.vsync));
	}
	
	public static void init() {
		READER = FileHelpers.readBDF("./settings.bdf.gz");
		SETTINGS.BdfClassLoad(READER.getObject());
	}
	
	public static void update() {
		SETTINGS.BdfClassSave(READER.getObject());
		WorkerTasks.saveToFile("./settings.bdf.gz", READER);
	}

}
