//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 gLevelScript = null;
var gKeyRemapping = false;
var gRecordVideo = false;
var gRecordVideoNumber = 0;
var gRecordIndex = 1;

EInputType =
{
	Keyboard : "key",
	Mouse : "mouse",
	Pad : "pad",
	Touch : "touch",
}

var gInputType;

function SetInputType(type)
{
	if(type != gInputType)
	{
		gInputType = type;
		gControlsOverlay.redraw = true;
		if(gOptionsMenu && gOptionsMenu.isRegistered && gOptionsMenu.optionsButtons.currentlyLoaded == 2)
		{
			gOptionsMenu.SetInput();
		}
		if(document.body)
		{
			document.body.style.cursor = (type == EInputType.Mouse ? "" : "none");
		}
	}
}

EKeyboardControls = 
{
	arrows : "arrows",
	wasd : "wasd",
}

var gKeyboardControls = EKeyboardControls.wasd;

function SetKeyboardControls(type)
{
	if(type != gKeyboardControls)
	{
		gKeyboardControls = type;
		gControlsOverlay.redraw = true;
	}
}

SetInputType(gMobile ? EInputType.Touch : EInputType.Keyboard);

function InitGame()
{
	gMenuSystem.Init();

	gTileSystem.Init();
	
	gStatsSystem.Init();
	
	gParticleSystem.Init();
	
	gPlayerSystem.Init();
	
	gBlockSystem.Init();
	
	gUnlockSystem.Init();

}

function PostInitGame()
{
	gInteraction.Init(MouseDown, MouseMove, MouseUp, MouseOut);
	gKeyboard.Init(KeyDown, KeyUp, KeyPress);
	gPad.Init(KeyDown, KeyUp, KeyPress);
	if(gCommands)
	{
		Command("setdisplay");
	}
	else
	{
		resizeCanvas();
	}
	gMusic.NextTrack();
}

function UpdateGame()
{
	if(gRecordVideo)
	{
		var link = document.createElement("a");
		gDraw.debug.translate(-gDraw.horizontalCenterOffset, -gDraw.verticalCenterOffset)
		gDraw.debug.clearRect(0, 0, gDraw.canvas.width, gDraw.canvas.height);
		var canvases = document.getElementsByTagName('canvas');
		for(var i = 0; i < canvases.length; i++)
		{
			
			gDraw.debug.drawImage(canvases[i], 0, 0, gDraw.canvas.width, gDraw.canvas.height);
		}
		var img = gDraw.debug.canvas.toDataURL('image/png');
		gDraw.debug.clearRect(0, 0, gDraw.canvas.width, gDraw.canvas.height);
		link.download = "Polyology " + gRecordVideoNumber + " " + gRecordIndex;
		link.href = img;
		link.click();
		gDraw.debug.translate(gDraw.horizontalCenterOffset, gDraw.verticalCenterOffset)
		gRecordIndex++;
	}
	if(gResizeCount == 0 && gResize)
	{
		gConsole.Log(1, "[RSZ] emergency resize")
		resizeCanvas();
	}
	gPad.Update();
	gMusic.Update();
	gSounds.Update();
	gScoreOverlay.Update();
	gPerformanceOverlay.Update();
	gObjectivesOverlay.Update();
	gCompleteOverlay.Update();
	gHintsOverlay.Update();
	gMenuSystem.Update();
	gLevelSystem.Update();
	gStatsSystem.Update();

	if(gPause.IsPaused())
	{
		return;
	}

	gRecordingSystem.Update();
	gPlayerSystem.Update();
	gBlockSystem.Update();
	gParticleSystem.Update();
	gSpawnerManager.Update();
	gRulesSystem.Update();
	gUndoSystem.Update();
	
	if(gLevelScript)
	{
		if(gLevelScript.Update)
		{
			gLevelScript.Update();
		}
	}
}

function MouseDown()
{
	if(gKeyRemapping)
	{
		return;
	}

	if(gEditor)
	{
		EditorAction(gInteraction.mouseX, gInteraction.mouseY);
	}
	else
	{
		gPlayerSystem.MouseDown(gInteraction.mouseX, gInteraction.mouseY);
	}
	
	gIconsOverlay.InteractDown(gInteraction.mouseX, gInteraction.mouseY)
}

function MouseUp()
{
	if(gKeyRemapping)
	{
		return;
	}

	if(!gEditor)
	{
		if(gMenuSystem.InteractRelease(gInteraction.mouseX, gInteraction.mouseY))
		{
			return;
		}

		gPlayerSystem.MouseUp(gInteraction.mouseX, gInteraction.mouseY);
	}
	
	gIconsOverlay.InteractRelease(gInteraction.mouseX, gInteraction.mouseY)
}

function MouseMove()
{
	if(gKeyRemapping)
	{
		return;
	}

	gMenuSystem.InteractMove(gInteraction.mouseX, gInteraction.mouseY)
	gIconsOverlay.InteractMove(gInteraction.mouseX, gInteraction.mouseY)
	if(gInteraction.mouseDown)
	{
		gPlayerSystem.InteractMove(gInteraction.mouseX, gInteraction.mouseY);
	}
}

function MouseOut()
{
	if(gKeyRemapping)
	{
		return;
	}

	gIconsOverlay.InteractMoveOut()
}

function KeyDown(key)
{
	if(gKeyRemapping)
	{
		return;
	}
	
	switch(key)
	{
		case EKeyCode.w:
		case EKeyCode.a:
		case EKeyCode.s:
		case EKeyCode.d:
		case EKeyCode.r:
		case EKeyCode.c:
		case EKeyCode.z:
		SetKeyboardControls(EKeyboardControls.wasd);
		break;
		case EKeyCode.left:
		case EKeyCode.right:
		case EKeyCode.up:
		case EKeyCode.down:
		case EKeyCode.home:
		case EKeyCode.pagedown:
		case EKeyCode.delete:
		SetKeyboardControls(EKeyboardControls.arrows);
		break;
	}
	
	gRecordingSystem.RecordKeyDown(key);
	gPlayerSystem.KeyDown(key);
	gMenuSystem.KeyDown(key);
	gUndoSystem.KeyDown(key);
	
	if(gLevelScript)
	{
		if(gLevelScript.KeyDown)
		{
			gLevelScript.KeyDown(key);
		}
	}
	
	if(key == EKeyCode.F12)
	{
		var link = document.createElement("a");
		gDraw.debug.translate(-gDraw.horizontalCenterOffset, -gDraw.verticalCenterOffset)
		gDraw.debug.clearRect(0, 0, gDraw.canvas.width, gDraw.canvas.height);
		var canvases = document.getElementsByTagName('canvas');
		for(var i = 0; i < canvases.length; i++)
		{
			
			gDraw.debug.drawImage(canvases[i], 0, 0, gDraw.canvas.width, gDraw.canvas.height);
		}
		var img = gDraw.debug.canvas.toDataURL('image/png');
		gDraw.debug.clearRect(0, 0, gDraw.canvas.width, gDraw.canvas.height);
		var date = new Date();
		link.download = "Polyology " + date.toLocaleString() + " " + date.getSeconds() + " " + date.getMilliseconds();
		link.href = img;
		link.click();
		gDraw.debug.translate(gDraw.horizontalCenterOffset, gDraw.verticalCenterOffset)
	}
	else if(key == EKeyCode.F8)
	{
		gRecordVideo = !gRecordVideo;
		if(gRecordVideo)
		{
			gRecordVideoNumber++;
			gRecordIndex = 1;
		}
	}
}

function KeyUp(key)
{
	if(gKeyRemapping)
	{
		KeyRemap(key);
		return;
	}
	
	gRecordingSystem.RecordKeyUp(key);
	gPlayerSystem.KeyUp(key);
	gMenuSystem.KeyUp(key);
	gUndoSystem.KeyUp(key);
	
	if(!gEditor)
	{
		switch(key)
		{
			case EKeyCode.r:
			case EKeyCode.pagedown:
				if(gPauseMenu.isRegistered == false && gMenuSystem.CanRestart())
				{
					gMenuSystem.Restart();
				}
				break;
			case EKeyCode.c:
			case EKeyCode.home:
				if(gPauseMenu.isRegistered == false && gMenuSystem.CanHub())
				{
					gMenuSystem.Hub();
				}
				break;
		}
	}
}

function KeyPress(key)
{
	if(gKeyRemapping)
	{
		return;
	}

	gMenuSystem.KeyPress(key);
	gCompleteOverlay.KeyPress(key);
}
