//Copyright (C) Ben Parbury - All Rights Reserved. Unauthorized copying of this file, via any medium is strictly prohibited. Written by Ben Parbury <ben@gridlockinteractive.com> 2014

var gMenuSystem = new MenuSystem();

var gPauseMenu;
var gOptionsMenu;

function MenuSystem()
{
	this.menus = new Array();
	this.drawnBackground = false;
		
	this.pauseCloseSound = gSounds.Get("pauseClose")
	this.pauseOpenSound = gSounds.Get("pauseOpen")
	this.Init = function()
	{
		gConsole.Log(3, "[MENU] Init");

		gPauseMenu = new PauseMenu();
		gOptionsMenu = new OptionsMenu();
	}
	
	this.Resize = function()
	{
		gPauseMenu.Resize();
		gOptionsMenu.Resize();
		for(var i = 0; i < this.menus.length; i++)
		{
			this.menus[i].Resize();
		}
	}

	this.Register = function(menu)
	{
		this.menus.push(menu);
		menu.Registered();
	}

	this.Unregister = function(menu)
	{
		for(var i = 0; i < this.menus.length; i++)
		{
			if(this.menus[i] == menu)
			{
				menu.Redraw();
				menu.DrawClear();
				menu.Unregistered();
				this.menus.splice(i, 1);
				return;
			}
		}
	}

	this.FindMenu = function(name)
	{
		for(var i = 0; i < this.menus.length; i++)
		{
			if(this.menus[i].name == name)
			{
				return this.menus[i];
			}
		}

		return null;
	}

	this.KeyPress = function(key)
	{
		switch(key)
		{
			case EKeyCode.escape:
			case EKeyCode.p:
				this.Pause(false);
				break;
		}
	}
	
	this.Pause = function(skipAllMenus, backOnly)
	{
		if(this.menus.length == 0 && !gLevelSystem.IsMenu())
		{
			if(!backOnly)
			{
				this.Register(gPauseMenu);
				this.pauseOpenSound.Play();
			}
		}
		else if(gPauseMenu.isRegistered)
		{
			this.pauseCloseSound.Play();
			this.Unregister(gPauseMenu);
		}
		else if(gOptionsMenu.isRegistered)
		{
			this.Unregister(gOptionsMenu);
			if (!skipAllMenus)
			{
				this.Register(gOptionsMenu.parentMenu);
			}
		}
		else
		{
			var levelName = gLevelSystem.GetLevelName();
			if((levelName == "title" || levelName == "demo") && gLevelSystem.GetLevelSet() == "menu")
			{
				if(gCommands)
				{
					Command("quit");
				}
			}
			else
			{
				if(gDemo)
				{
					gLevelSystem.LoadLevel("menu", "demo");
				}
				else
				{
					gLevelSystem.LoadLevel("menu", "title");
				}
			}
			
		}
	}
	
	this.CanRestart = function()
	{
		var menu = gLevelSystem.GetLevelSet() == "menu"
		var hub = (gLevelSystem.GetLevelName() == "hub")
		return !menu && !hub;
	}
	
	this.Restart = function()
	{
		if(this.CanRestart())
		{
			gMenuSystem.Unregister(gPauseMenu);
			gLevelSystem.Restart();
		}
	}
	
	this.CanHub = function()
	{
		if(gDemo)
		{
			return false;
		}
		var hub = (gLevelSystem.GetLevelName() == "hub")
		var set = gLevelSystem.GetLevelSet();
		var hubhub = IsHubSet(set);
		var menu = set == "menu"
		var arcade = gLevelSystem.IsArcade();
		return (!menu && !hubhub && !(arcade && hub))
	}
	
	this.Hub = function()
	{
		if(this.CanHub())
		{
			gMenuSystem.Unregister(gPauseMenu);
			gLevelSystem.Hub();
		}
	}
	
	this.KeyDown = function(key)
	{
		var pressed = false;
		for(var i = 0; i < this.menus.length; i++)
		{
			pressed = this.menus[i].KeyDown(key);
			if(pressed)
			{
				return;
			}
		}
		return;
	}
	
	this.KeyUp = function(key)
	{
		var pressed = false;
		for(var i = 0; i < this.menus.length; i++)
		{
			pressed = this.menus[i].KeyUp(key);
			if(pressed)
			{
				return;
			}
		}
		return;
	}
	
	this.InteractMove = function(x, y)
	{
		var interacted = false;
		for(var i = 0; i < this.menus.length; i++)
		{
			interacted = this.menus[i].InteractMove(x, y);
			if(interacted)
			{
				return true;
			}
		}
		
		return false;
	}


	this.InteractRelease = function(x, y)
	{
		var pressed = false;
		for(var i = 0; i < this.menus.length; i++)
		{
			pressed = this.menus[i].InteractRelease(x, y);
			if(pressed)
			{
				return true;
			}
		}
		
		return false;
	}
	
	this.Redraw = function()
	{
		for(var i = 0; i < this.menus.length; i++)
		{
			this.menus[i].Redraw();
		}
	}

	this.DrawClear = function()
	{
		this.DrawClearBackground();
		
		for(var i = 0; i < this.menus.length; i++)
		{
			this.menus[i].DrawClear();
		}
	}
	
	this.Draw = function()
	{
		this.DrawBackground();
		
		for(var i = 0; i < this.menus.length; i++)
		{
			this.menus[i].Draw();
		}
	}
	
	this.DrawClearBackground = function()
	{
		if (this.drawnBackground)
		{
			gDraw.menubackground.clearRect(0, 0, gDraw.canvas.width, gDraw.canvas.height);
			this.drawnBackground = false;
		}
	}
	
	this.DrawBackground = function()
	{
		if (this.menus.length > 0 && !gLevelSystem.IsMenu())
		{
			gBackground.DrawContext(gDraw.menubackground);
			this.drawnBackground = true;
		}
	}
	
	this.Update = function()
	{
		for(var i = 0; i < this.menus.length; i++)
		{
			this.menus[i].Update();
		}
	}
}
