//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

function DrawGame()
{
	if(gPause.IsPaused(EPauseReason.Loading))
	{
		return
	}
	
	gBackground.Draw();

	var paused = gPause.IsPaused();
	if(!paused || gEditor)
	{
		//background
		if(gTileSystem.Redraw())
		{
			gTileSystem.Draw();
		}
		//~background
		
		//canvas
		gPlayerSystem.DrawClear();
		gBlockSystem.DrawClear();
		
		gPlayerSystem.Draw();
		gBlockSystem.Draw();
		//~canvas
		
		//particles
		gParticleSystem.DrawClear();
		gParticleSystem.Draw();
		//~particles
	}
	
	gLevelSystem.DrawTransition();
	
	//overlay
	gScoreOverlay.DrawClear();
	gObjectivesOverlay.DrawClear();
	gControlsOverlay.DrawClear();
	gPerformanceOverlay.DrawClear();
	gIconsOverlay.DrawClear();
	gHintsOverlay.DrawClear();
	gNextBlockOverlay.DrawClear();
	gCompleteOverlay.DrawClear();
	gMenuSystem.DrawClear();
	
	if(!paused)
	{		
		if(gLevelScript)
		{
			if(gLevelScript.Draw && !gLevelSystem.loadingTransition)
			{
				gLevelScript.DrawClear();
				gLevelScript.Draw();
			}
		}
	}
	gMenuSystem.Draw();
	gScoreOverlay.Draw();
	gControlsOverlay.Draw();
	gPerformanceOverlay.Draw();
	gIconsOverlay.Draw();
	gObjectivesOverlay.Draw();
	gCompleteOverlay.Draw();
	gHintsOverlay.Draw();
	gNextBlockOverlay.Draw();
	//~overlay
	
	//debug
	var urlVars = getUrlVars();
	if(urlVars["debug"] || urlVars["keyboard"] || urlVars["grid"] || urlVars["undo"] || urlVars["blocks"])
	{
		gDraw.debug.clearRect(0, 0, gDraw.canvas.width, gDraw.canvas.height);
	}
	
	if(urlVars["debug"])
	{
		gPerformanceOverlay.Debug();
		gRecordingSystem.Debug();
		gInteraction.Debug();
		gStatsSystem.Debug();
		gRulesSystem.Debug();
		gSpawnerManager.Debug();
		gStyles.Debug();
		gPlayerSystem.Debug();
		
		if(gLevelScript)
		{
			if(gLevelScript.Debug)
			{
				gLevelScript.Debug();
			}
		}
	}
	
	if (urlVars["keyboard"])
	{
		gKeyboard.Debug();
	}
	
	if (urlVars["undo"])
	{
		gUndoSystem.Debug();
	}
	
	if(urlVars["blocks"])
	{
		gBlockSystem.Debug();
	}

	if (urlVars["grid"])
	{
		var padding = kTileSize * 0.1;
		gDraw.debug.strokeStyle = "rgba(255, 255, 255, 0.2)";
		gDraw.debug.lineWidth = 1;

		gDraw.debug.beginPath();
		gDraw.debug.moveTo(padding, padding);
		gDraw.debug.lineTo(gDraw.canvas.width - padding, padding);
		gDraw.debug.lineTo(gDraw.canvas.width - padding, gDraw.canvas.height - padding);
		gDraw.debug.lineTo(padding, gDraw.canvas.height - padding);
		gDraw.debug.closePath();
		gDraw.debug.stroke();

		gDraw.debug.beginPath();
		gDraw.debug.moveTo(gDraw.canvas.width/2, 0);
		gDraw.debug.lineTo(gDraw.canvas.width/2, gDraw.canvas.height);
		gDraw.debug.moveTo(0, gDraw.canvas.height/2);
		gDraw.debug.lineTo(gDraw.canvas.width, gDraw.canvas.height/2);
		gDraw.debug.stroke();
	}

	//~debug
}

function RedrawGame()
{
	gBackground.Redraw();
	gTileSystem.redraw = true;
	gScoreOverlay.redraw = true;
	gObjectivesOverlay.redraw = true;
	gIconsOverlay.Redraw();
	gHintsOverlay.redraw = true;
	gCompleteOverlay.redraw = true;
	gControlsOverlay.redraw= true;
	gIconsOverlay.Resize();
	gMenuSystem.Redraw();
	gStyles.Resize();
	gParticleSystem.Resize();
	gBlockSystem.RebuildRenderCommands();
	gPlayerSystem.Resize();
	gMenuSystem.Resize();
	gUndoSystem.Resize();
	
	if(gLevelScript)
	{
		if(gLevelScript.Redraw)
		{
			gLevelScript.Redraw();
		}
	}

	
	DrawGame();
}

function ClearCanvas()
{
	gDraw.context.clearRect(0, 0, gDraw.canvas.width, gDraw.canvas.height);
}

function ClearPlayer()
{
	gDraw.player.clearRect(0, 0, gDraw.canvas.width, gDraw.canvas.height);
}

function ClearOverlay()
{
	gDraw.overlay.clearRect(0, 0, gDraw.canvas.width, gDraw.canvas.height);
}

function ClearHubs()
{
	gDraw.hubs.clearRect(0, 0, gDraw.canvas.width, gDraw.canvas.height);
}

function ClearMenu()
{
	gDraw.menu.clearRect(0, 0, gDraw.canvas.width, gDraw.canvas.height);
}
