package projectzombie.menu;

import java.io.FileOutputStream;
import java.util.zip.DeflaterOutputStream;

import bdf.types.BdfObject;
import projectzombie.Main;
import projectzombie.input.types.InputGUI;
import projectzombie.menu.gui.GUIButtonGroup;
import projectzombie.menu.gui.GUI;
import projectzombie.menu.gui.components.ButtonBasic;
import projectzombie.menu.gui.components.ButtonGroupPause;
import projectzombie.menu.gui.components.GUIBackToMenu;
import projectzombie.menu.gui.components.LabelPause;
import projectzombie.menu.gui.components.OverlayBackground;

public class MenuGamePause extends Menu
{
	private GUI gui;

	public MenuGamePause() {
		this.doGameloop = false;
		this.doGameRender = true;
		this.keepMouse = false;
		
		this.gui = new GUIBackToMenu(Main.menu);
		this.input = new InputGUI(this.gui);
		
		gui.add(new OverlayBackground());
		gui.add(new LabelPause("Game Paused"));
		
		GUIButtonGroup group = new ButtonGroupPause();
		
		group.add(new ButtonBasic("Save And Exit", button -> {
			saveAndQuit();
		}));
		
		gui.setSelected(group.get(0));
		gui.add(group);
	}
	
	@Override
	public void render() {
		gui.render();
	}
	
	public void saveAndQuit()
	{
		long now, cur = 0;
		
		BdfObject bdf = new BdfObject();
		
		now = System.currentTimeMillis();
		Main.world.BdfClassSave(bdf);
		cur = System.currentTimeMillis();
		System.out.println("Time for BdfClassSave: " + (cur - now));
		now = cur;
		
		System.out.println("Size on file: " + bdf.serialize().size());
		//System.out.println(bdf.serializeHumanReadable());
		
		try {
			
			/*
			
			FileOutputStream os = new FileOutputStream("./layer.hbdf");
			os.write(bdf.serializeHumanReadable(new BdfIndent("\t", "\n")).getBytes());
			os.close();
			
			cur = System.currentTimeMillis();
			System.out.println("Time for HBDF: " + (cur - now));
			now = cur;
			
			*/
			
			FileOutputStream os2 = new FileOutputStream("./layer.bdf");
			DeflaterOutputStream dos = new DeflaterOutputStream(os2);
			dos.write(bdf.serialize().getBytes());
			dos.close();
			os2.close();
			
			cur = System.currentTimeMillis();
			System.out.println("Time for BDF: " + (cur - now));
			now = cur;
		}
		
		catch(Exception e) {
			e.printStackTrace();
		}
		
		Main.menu = new MenuMain();
	}
}
