using UnityEngine;
using System.Collections;

public class Game : MonoBehaviour {
	
	Level level;
	Hero hero;
	Vector2 startTouchPosition;
	
	void Start () {
		//Utils.Create<Inventory>(transform);
		level = Utils.Create<Level>(transform);
		hero  = Utils.Create<Hero>(level.transform);
		GameInterface gameInterface = Utils.Create<GameInterface>(transform);
		gameInterface.level = level;
		gameInterface.hero  = hero;
		level.AddUnit(2, 2, hero);
		for (int i = 0; i < 30; i ++){
			level.AddUnit(Random.Range(2, 40), Random.Range(2, 40), Utils.Create<Unit>(level.transform));
		}
		for (int i = 0; i < 30; i ++){
			level.AddUnit(Random.Range(2, 40), Random.Range(2, 40), Utils.Create<Goblin>(level.transform));
		}
		for (int i = 0; i < 30; i ++){
			int x = Random.Range(2, 40);
			int y = Random.Range(2, 40);
			if (level.isFloor(x, y))
				level.AddItems(x, y, new Item[]{
					new LifeElixir()
				});
			x = Random.Range(2, 40);
			y = Random.Range(2, 40);
			if (level.isFloor(x, y))
				level.AddItems(x, y, new Item[]{
					new Key(Random.Range(0, 6))
				});
		}
	}
	
	void Update () {
		//TODO
//		float speedFactor = 10f;
//		float delta = Time.deltaTime;
		/*if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow)   ) {
			transform.position -= Vector3.up    * speedFactor * delta;
		}
		if (Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.DownArrow) ) {
			transform.position -= Vector3.down  * speedFactor * delta;
		}
		if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow) ) {
			transform.position -= Vector3.left  * speedFactor * delta;
		}
		if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow)) {
			transform.position -= Vector3.right * speedFactor * delta;
		}	*/	
		
		
		if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow)) {
			hero.Move(Direction.up);
			level.MakeTurn();
		}
		if (Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.DownArrow) ) {
			hero.Move(Direction.down);
			level.MakeTurn();
		}
		if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow) ) {
			hero.Move(Direction.left);
			level.MakeTurn();
		}
		if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow)) {
			hero.Move(Direction.right);
			level.MakeTurn();
		}		
		//if (Input.GetKey(KeyCode.Space)){
		//	level.FindPath(new Coordinate(1, 1), new Coordinate(5, 5));
		//}
		//Control.activeTouches[0].position
		//Debug.Log(level.shift);
		//TODO
		/*if (Control.activeTouchCount > 0){
			if (Control.activeTouches[0].state == TouchState.Touch) {
				transform.position += (Vector3)(Control.activeTouches[0].position - Control.activeTouches[0].previosPosition);
			}
		}*/
		
		//TODO
		level.needShift = new Vector3(hero.transform.localPosition.x, hero.transform.localPosition.y);
		if (Input.GetKey(KeyCode.Escape)) {
			Application.Quit();
		}
	}
}
