//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 gEditorData = new Array();
var gEditorTileBlockMove = true;
var gEditorTilePlayerMove = true;
var gEditorBlockSwitch = false;
var gEditorBlockFading = false;
var gEditorBlockConnected = false;

function SubmitToEditor()
{
	gEditorData = new Array();
	for (i = 0; i < document.forms["data"].elements.length; i++)
	{
		var element = document.forms["data"].elements[i];
		if ((element.checked) || (element.type != "radio" && element.type != "button"))
		{
			gEditorData[element.name] = element.value;
			//gConsole.Log(9, "[Ed] EditorAction '" + element.name + "' = " + element.value);
		}
	}
	EditorUpdateRules();
}

function EditorClearBlocks()
{
	while(gBlockSystem.blocks.length > 0)
	{
		gBlockSystem.RemoveBlockAtIndex(0);
	}
	gBlockSystem.Clear();
	gParticleSystem.Clear();
}

function EditorTileFill()
{
	for(var x = 0; x < gTileWidth; x++)
	{
		for(var y = 0; y < gTileHeight; y++)
		{
			if(!gTileSystem.GetTileAt(x, y))
			{
				gTileSystem.AddTile(ETileType.Normal, null, x, y);
			}
		}
	}
}

function EditorClearStats()
{
	gStatsSystem.Clear();
}

function EditorLoad()
{
	var levelName = document.forms["data"].elements["levelName"].value;
	gConsole.Log(3, "[Ed] Loading " + levelName);
	
	var xml = localStorage.getItem(levelName);
	if(xml)
	{
		gLevelSystem.Load(xml);
	}
}

function EditorSave()
{
	var levelName = document.forms["data"].elements["levelName"].value;
	gConsole.Log(3, "[Ed] Saving " + levelName);
	
	var xml = gLevelSystem.Write();
	
	localStorage.setItem(levelName, xml);
}

function BuildLevelXml()
{
	gConsole.Log(1, "[Ed] Saving level xml file");
	
	window.URL = window.webkitURL || window.URL;
	window.BlobBuilder = window.BlobBuilder || window.WebKitBlobBuilder || window.MozBlobBuilder;

	var a = document.links["downloadLevel"];
	
	var MIME_TYPE = 'text/xml';
	var xml = gLevelSystem.Write();
	var blob = new Blob([xml], {type: MIME_TYPE});

	a.href = window.URL.createObjectURL(blob);
	a.download = document.forms["data"].elements["levelName"].value.toLowerCase() + ".xml";
	a.dataset.downloadurl = [MIME_TYPE, a.download, a.href].join(':');
}

function EditorRecordReplay()
{
	gRecordingSystem.Record();
}

function EditorPlaybackReplay()
{
	gRecordingSystem.Playback();
}

function BuildReplayXml()
{
	gConsole.Log(1, "[Ed] Saving Replay xml file");
	
	window.URL = window.webkitURL || window.URL;
	window.BlobBuilder = window.BlobBuilder || window.WebKitBlobBuilder || window.MozBlobBuilder;

	var a = document.links["downloadReplay"];
	
	var xml = gRecordingSystem.Write();
	var bb = new BlobBuilder();
	bb.append(xml);

	var MIME_TYPE = 'text/plain';
	a.href = window.URL.createObjectURL(bb.getBlob(MIME_TYPE));
	a.download = document.forms["data"].elements["levelName"].value + ".xml";
	a.dataset.downloadurl = [MIME_TYPE, a.download, a.href].join(':');
}

function EditorUpdateRules()
{
	/*
	gRulesSystem.bonusRule.Init(gEditorData["BonusStat"], gEditorData["BonusCondition"], gEditorData["BonusTarget"]);
	gRulesSystem.endRule.Init(gEditorData["EndStat"], gEditorData["EndCondition"], gEditorData["EndTarget"]);
	*/
}

function EditorLoadRulesIntoHTML()
{
	/*
	if(gRulesSystem.bonusRule.valid)
	{
		document.forms["data"].elements["BonusStat"].value = gRulesSystem.bonusRule.statType;
		document.forms["data"].elements["BonusCondition"].value = gRulesSystem.bonusRule.condition;
		document.forms["data"].elements["BonusTarget"].value = gRulesSystem.bonusRule.target;
	}
	else
	{
		document.forms["data"].elements["BonusStat"].value = "none";
	}

	if(gRulesSystem.endRule.valid)
	{
		document.forms["data"].elements["EndStat"].value = gRulesSystem.endRule.statType;
		document.forms["data"].elements["EndCondition"].value = gRulesSystem.endRule.condition;
		document.forms["data"].elements["EndTarget"].value = gRulesSystem.endRule.target;
	}
	else
	{
		document.forms["data"].elements["EndStat"].value = "none";
	}
	*/
}

function EditorReloadScript()
{
	if(gLevelSystem.scriptFilename)
	{
		var filename = gLevelSystem.scriptFilename.replace(gLevelSystem.GetLevelPath(), "");
		gLevelSystem.LoadScriptFile(filename);
	}
}


function EditorAction(_x, _y)
{
	var tileX = Math.floor(_x / kTileSize);
	var tileY = Math.floor(_y / kTileSize);
	
	//gConsole.Log(8, "[Ed] EditorAction (" + tileX + ", " + tileY + ") " + gEditorData["type"]);
	
	switch(gEditorData["type"])
	{
		case "normal":
		case "sliding":
		case "static":
			{
				if(gTileSystem.GetTileAt(tileX, tileY))
				{
					if(!gBlockSystem.GetBlockAt(tileX, tileY))
					{
						if(!gPlayerSystem.GetPlayerAt(tileX, tileY))
						{
							var blockType = gEditorData["type"];
							var blockValue = Number(gEditorData["blockValue"]);
							var blockSubType = gEditorData["blockType"];
							var blockRotation = gEditorData["blockRotation"];
							gBlockSystem.AddBlock(blockType, blockSubType, blockRotation, blockValue, tileX, tileY, gEditorBlockSwitch, gEditorBlockConnected, gEditorBlockFading);
							return;
						}
					}
				}
			}
			break;
		case "removeBlocks":
			{
				var block = gBlockSystem.GetBlockAt(tileX, tileY);
				if(block)
				{
					gBlockSystem.RemoveBlock(block);
				}
			}
			break;
		case "removeBlocksTiles":
			{
				var block = gBlockSystem.GetBlockAt(tileX, tileY);
				if(block)
				{
					gBlockSystem.RemoveBlock(block);
				}
				var tile = gTileSystem.GetTileAt(tileX, tileY);
				if(tile)
				{
					gTileSystem.RemoveTile(tile);
				}
			}
			break;
		case "tile":
			{
				var oldTile = gTileSystem.GetTileAt(tileX, tileY);
				gTileSystem.RemoveTile(oldTile);
				gTileSystem.AddTileWithParam(ETileType.Normal, null, tileX, tileY, gEditorTileBlockMove, gEditorTilePlayerMove);
				return;
			}
			break;
	}

	for(var tileType in ETileType)
	{
		if(gEditorData["type"] == ETileType[tileType])
		{
				var oldTile = gTileSystem.GetTileAt(tileX, tileY);
				gTileSystem.RemoveTile(oldTile);
				var subType = gEditorData["tileType"];
				var hintText = gEditorData["hintText"];
				gTileSystem.AddTileWithParam(ETileType[tileType], subType, tileX, tileY, gEditorTileBlockMove, gEditorTilePlayerMove, hintText);
				return;
		}
	}
}

function EditorLightSet()
{
	while(gStyles.lightSources.length < 8)
	{
		gStyles.lightSources.push(new LightSource(0, 0, 0, 0));
	}
	
	var index = parseInt(document.forms["data"].elements["light"].value);
	var lightSource = gStyles.lightSources[index];
	
	EditorLightSetElement("lightx", lightSource.x);
	EditorLightSetElement("lighty", lightSource.y);
	EditorLightSetElement("lightrange", lightSource.range);
	EditorLightSetElement("lightstrength", lightSource.strength);
}

function EditorLightSetElement(elementId, value)
{
	var element = document.getElementById(elementId);
	element.value = value;
}

function EditorLightOnClick()
{
	var index = parseInt(document.forms["data"].elements["light"].value);
	var lightSource = gStyles.lightSources[index];
	
	lightSource.x = EditorLightGetValue("lightx");
	lightSource.y = EditorLightGetValue("lighty");
	lightSource.range = EditorLightGetValue("lightrange");
	lightSource.strength = EditorLightGetValue("lightstrength");
	
	gStyles.InitLights();
}

function EditorLightGetValue(elementId)
{
	var element = document.getElementById(elementId);
	return element.valueAsNumber;
}
