#pragma strict

var levelTiles : TextAsset;
var levelTileStr : String;
var gridStartX : float = 0.0f;
var gridStartZ : float = 0.0f;

var gridSizeX : float = 1.0f;
var gridSizeZ : float = 1.0f;

var instances : GameObject[];

var levels : String[];
var thisLevel : int = 0;

function Start() {
	LoadNewLevel(levels[thisLevel]);
	GenLevel();
}

function ResetLevel() {
	thisLevel = 0;
}

function NextLevel() {
	if (thisLevel < levels.Length-1) { 
		thisLevel = thisLevel + 1;
		LoadNewLevel(levels[thisLevel]);
		GenLevel();
	} else {
		Debug.Log("the end");
	}
}

function LoadNewLevel(levelName : String) {
	levelTiles = Resources.Load(levelName) as TextAsset;
}

//var Mumbles : AudioSource = new AudioSource();
var tune : int = 1;

function GenLevel() {
	var t : TextMesh = GameObject.Find("level").GetComponent(TextMesh);
	t.text = "LEVEL " + (thisLevel+1).ToString();
	
	audio.clip = Resources.Load("tune"+tune.ToString());
	audio.PlayDelayed(2.0f);
	tune=tune+1;
	if (tune == 4) {
		tune = 1;
	}

	levelTileStr = levelTiles.text;
	levelTileStr = levelTileStr.Replace("\r", "");
	levelTileStr = levelTileStr.Replace("\n", "");
	var tile : GameObject;
	
	gridStartX = transform.position.x;
	gridStartZ = transform.position.z;
	
	var x : float = transform.position.x;
	var z : float = transform.position.z;
		
	for (var i=0; i < levelTileStr.Length; i++) {
		if (levelTileStr.Substring(i,1) == "^" || levelTileStr.Substring(i,1) == "-") {
			if (i > 12 && i < 110 && levelTileStr.Substring(i,1) == "^") {
				tile = Resources.Load("Arrows");
				instances[i] = Instantiate (tile, Vector3(x, transform.position.y, z), Quaternion.Euler(-90, 0, 0));
				instances[i].GetComponent("ArrowData").SendMessage("SetInitialValues", i);
			}			
			z = z - gridSizeZ;
			x = gridStartX;
		}

		var tex : String = "tile"; 
			
		if (levelTileStr.Substring(i,1) == "#") {
			tile = Resources.Load("tile2");
			tex = "tile";
		} else if (levelTileStr.Substring(i,1) == ".") {
			tile = Resources.Load("tilebase");
			tex = "tile";
		} else if (levelTileStr.Substring(i,1) == "s") {
			tile = Resources.Load("tilebase");
			tex = "start";
		} else if (levelTileStr.Substring(i,1) == "e") {
			tile = Resources.Load("tilebase");
			tex = "end";
		} else if (levelTileStr.Substring(i,1) == "w") {
			tile = Resources.Load("tilebase");
			tex = "woosh";
		} else if (levelTileStr.Substring(i,1) == " ") {	
			tile = Resources.Load("tilehole");
			tex = "tile";
		} else if (levelTileStr.Substring(i,1) == "<") {	
			tile = Resources.Load("tilebase");
			tex = "left";
		} else if (levelTileStr.Substring(i,1) == ">") {	
			tile = Resources.Load("tilebase");
			tex = "right";
		} else {
			continue;		
		}
		
		//Cuts.renderer.materials[1].SetTexture("_MainTex2", Resources.Load(images[seq].ToString()));
	
		instances[i] = Instantiate (tile, Vector3(x, transform.position.y, z), Quaternion.Euler(-90, 0, 0));
		instances[i].name = "tile"+i;
		if (tex != "tile") {
			instances[i].renderer.materials[0].SetTexture("_MainTex", Resources.Load(tex));
		}
		if (tex == "left" || tex == "right") {
			instances[i].renderer.material.SetColor ("_Color", Color.green);
		}
		if (tex == "start") {
			instances[i].renderer.materials[0].SetTexture("_MainTex", Resources.Load("tile"));
			instances[i].renderer.material.SetColor ("_Color", Color.cyan);
		}
		if (tex == "woosh") {
			instances[i].renderer.materials[0].SetTexture("_MainTex", Resources.Load("woosh"));
			instances[i].renderer.material.SetColor ("_Color", Color.yellow);
			instances[i].GetComponentInChildren(ActionTrigger).SetAction("LeapForward");
		}
		if (tex == "end") {
			instances[i].renderer.materials[0].SetTexture("_MainTex", Resources.Load("tile"));
			instances[i].renderer.material.SetColor ("_Color", Color.cyan);
			instances[i].GetComponentInChildren(ActionTrigger).SetAction("EndReached");
		}
		if (tex == "tilebase") {
			instances[i].GetComponent(ActionTrigger).SendMessage("SetAction", "WalkForward");
		}
		if (tex == "left") {
			instances[i].GetComponentInChildren(ActionTrigger).SetAction("HopLeft");
		}
		if (tex == "right") {
			instances[i].GetComponentInChildren(ActionTrigger).SetAction("HopRight");
		}
		x = x + gridSizeX;
	}
}

function UpdateBlocks (arrowObj : GameObject) {
	//Debug.Log("asdass");
	//Destroy(arrowObj);
	//Debug.Log(arrowObj.GetComponent("ArrowData"));
}


