using UnityEngine;
using System.Collections;

public class spawnerBehaviour : MonoBehaviour {
//	private int foo { get; set;}

	private int remaining;
	public int Remaining {
		get{
			return remaining;
		}
	}
	private GameObject enemy;
	public GameObject Enemy {
		get{
			return enemy;
		}
	}
//	private GameObject spawnEnemy(GameObject _enemy) {
//		//decide la texture
//	}
	private System.Random rnd;
	private float cooldown;
//	private float timeCal
	// Use this for initialization
	void Start () {
//		GameObject cam = GameObject.Find ("Main Camera");
		rnd = new System.Random(System.Convert.ToInt32(Time.time));
//		Color[] colors =  {new Color(233, 84, 27), new Color(72, 133, 27), new Color(72, 133, 164)};
//		Color bgcolor = colors [Random.Range (0, 3)];
//		Camera.main.backgroundColor = Color.Lerp (bgcolor, bgcolor, 1f);
		remaining = 17; // + System.Convert.ToInt32(0.3 * Referee.Level);
		if(Random.Range (0, 2) == 0) {
			enemy = GameObject.Find ("Spark");
		}
		else {
			enemy = GameObject.Find ("Meteor");
		}
		cooldown = Time.time;
		Camera.main.aspect = 16.0f / 9.0f;
		Debug.Log(Camera.main.aspect);
	}
	
	// Update is called once per frame
	void Update () {
		if (Time.time >= cooldown) {
						//	new GameObject
//		Quaternion rot = new Quaternion(0f, 0f, 0f, 0f);
//		Vector3 v1 = new Vector3 ((float)((rnd.NextDouble() - 0.5) * 24.8), 5f /*+ offsetY*/, 0f);
//		Vector3 v2 = new Vector3 ((float)((rnd.NextDouble() - 0.5) * 24.8), -5f, 0f);
			Vector3 zeroVector = Camera.main.ScreenToWorldPoint (new Vector3 (0, 0, 0));
//			Vector3 v1 = new Vector3 ((float)((rnd.NextDouble () - 0.5) * 2 * zeroVector.x), 5f /*+ offsetY*/, 0f);
//			Vector3 v2 = new Vector3 ((float)((rnd.NextDouble () - 0.5) * 2 * zeroVector.x), -5f, 0f);
			Vector3 v1 = new Vector3 ((float)((Random.Range(0, 10000) / 10000.0f - 0.5) * 2 * zeroVector.x), 5f /*+ offsetY*/, 0f);
			Vector3 v2 = new Vector3 ((float)((Random.Range(0, 10000) / 10000.0f - 0.5) * 2 * zeroVector.x), -5f, 0f);
			//		rot.z = Vector3.Angle(Vector3.down, v2 - v1);
			short versor = 1;
//			versor = 1;
			if(v1.x > v2.x) {
				versor = -1;
			}
			Quaternion rot = Quaternion.Euler (new Vector3 (0, 0, Vector3.Angle(Vector3.down, v2 - v1) * versor));
		
//			Debug.Log (rot.z);
			Instantiate (enemy, v1 , rot);
			cooldown = Time.time + 1.5f * 1 / RefereeBehaviour.LevelSpeed;
			remaining--;
			if(remaining < 0) {
				RefereeBehaviour.IsLevelOver = true;
				Debug.Log(remaining);
			}
		}
	}


}
