package projectzombie.menu;

import java.util.Random;


import bdf.types.BdfArray;
import bdf.types.BdfNamedList;
import bdf.types.BdfObject;
import bdf.types.BdfReader;
import gl_engine.vec.Vec2d;
import projectzombie.Main;
import projectzombie.init.Models;
import projectzombie.input.types.InputGUI;
import projectzombie.menu.gui.GUI;
import projectzombie.menu.gui.GUIAlignment;
import projectzombie.menu.gui.GUIBackToMenu;
import projectzombie.menu.gui.GUIButtonBasic;
import projectzombie.menu.gui.GUILabel;
import projectzombie.menu.gui.GUITextBox;
import projectzombie.util.FileHelpers;
import projectzombie.worker.WorkerTasks;
import projectzombie.world.World;

public class MenuWorldNew extends Menu
{
	private static final Random rand = new Random();
	
	private Menu parent;
	private String name;
	private long seed;
	
	private GUI gui;
	private GUITextBox world_name;
	
	public MenuWorldNew(Menu parent)
	{
		this.parent = parent;
		
		name = "World";
		seed = rand.nextLong();
		keepMouse = false;
		
		doGameloop = parent.doGameloop;
		doGameRender = parent.doGameRender;
		showIngameGUI = parent.showIngameGUI;
		
		gui = new GUIBackToMenu(parent);
		input = new InputGUI(gui);
		
		GUIButtonBasic buttonBack = new GUIButtonBasic("Back", button -> {
			Main.menu = parent;
		});
		
		GUIButtonBasic buttonCreate = new GUIButtonBasic("Generate", button -> {
			generateWorld();
		});
		
		buttonBack.setAlign(GUIAlignment.RIGHT);
		buttonCreate.setAlign(GUIAlignment.LEFT);
		
		buttonBack.setPos(new Vec2d(-0.5, -8));
		buttonCreate.setPos(new Vec2d(0.5, -8));
		
		{
			// New world title label
			GUILabel label = new GUILabel();
			label.setText("Create New World");
			label.setSize(new Vec2d(1, 1));
			label.setPos(new Vec2d(0, 6.8));
			gui.add(label);
		}
		
		{
			// Set the world name
			
			double offset = -Models.UI_TEXT_BOX.getWidth() / 2;
			
			GUILabel label = new GUILabel();
			label.setAlign(GUIAlignment.RIGHT);
			label.setText("World Name");
			label.setSize(new Vec2d(0.4, 0.4));
			label.setPos(new Vec2d(offset + Models.UI_TEXT_BOX.getWidth() * 2 / 128.0, 4));
			gui.add(label);
			
			world_name = new GUITextBox(new Vec2d(offset, 3.8 - Models.UI_TEXT_BOX.getHeight()), true);
			world_name.setText(name);
			gui.add(world_name);
		}
		
		gui.add(buttonBack);
		gui.add(buttonCreate);
	}
	
	private void generateWorld()
	{
		name = world_name.getText();
		
		String path = name;
		path = path.toLowerCase();
		
		char[] path_chars = path.toCharArray();
		
		for(int i=0;i<path_chars.length;i++)
		{
			char c = path_chars[i];
			
			if((c >= '0' && c <= '9') || (c >= 'a' && c <= 'z')) {
				path_chars[i] = c;
			} else {
				path_chars[i] = '_';
			}
		}
		
		path = new String(path_chars);
		
		if(name.length() == 0) {
			return;
		}
		
		BdfObject bdf = Main.bdf_saves.getObject();
		BdfNamedList nl = bdf.getNamedList();
		BdfArray array = nl.get("saves").getArray();
		
		// Find a unique name and path
		
		String name_new = name;
		String path_new = path;
		
		boolean duplicate = true;
		
		for(int i=1;duplicate;i++)
		{
			duplicate = false;
			
			for(BdfObject save_bdf : array)
			{
				BdfNamedList save_nl = save_bdf.getNamedList();
				String save_name = save_nl.get("name").getString();
				String save_path = save_nl.get("path").getString();
				
				if(i > 1) {
					name_new = name + " (" + i + ")";
					path_new = path + "_" + i;
				}
				
				if(name_new.contentEquals(save_name) || path_new.contentEquals(save_path)) {
					duplicate = true;
					break;
				}
			}
		}
		
		BdfObject save_bdf = bdf.newObject();
		BdfNamedList save_nl = save_bdf.getNamedList();
		
		save_nl.set("name", bdf.newObject().setString(name_new));
		save_nl.set("path", bdf.newObject().setString(path_new));
		
		array.add(save_bdf);
		WorkerTasks.saveToFile("./saves.bdf.gz", Main.bdf_saves);
		
		World.createWorld(path_new, seed);
		Main.menu = new MenuLoadingWorld();
	}
	
	@Override
	public void update()
	{
		super.update();
		parent.update();
	}

	@Override
	public void render() {
		gui.render();
	}

}
