using UnityEngine;
using System.Collections;

public class ThinkieGenerator : MonoBehaviour {

	public Thinkie[] thinkies;

	private float countdown;
	private float frequency;

	private float radius;
	private float startTime;

	void Start () {
		countdown = 0.0f;
		frequency = Random.Range (Constants.GENERATOR_MIN_FREQUENCY, Constants.GENERATOR_MAX_FREQUENCY);
		radius = Constants.GENERATOR_RADIUS;
		startTime = Time.time;
	}

	void FixedUpdate () {
		if (countdown < frequency) {
			countdown += Time.deltaTime;
		} else {
			Spawn();
			countdown = 0.0f;
			frequency = Random.Range (Constants.GENERATOR_MIN_FREQUENCY, Constants.GENERATOR_MAX_FREQUENCY);
		}
		radius = Mathf.Lerp (Constants.GENERATOR_RADIUS, Constants.GENERATOR_RADIUS_COLLAPSED, (Time.time - startTime) / Constants.GAME_TIME_LIMIT);
	}

	private void Spawn(){
		float posX = Random.Range (-1 * radius, radius);
		float posY = Mathf.Sqrt (radius*radius - posX*posX);
		bool isPositiveY = Random.Range (0,2) == 1 ? true : false;
		if (!isPositiveY)
			posY *= -1;
		int index = Random.Range (0, thinkies.Length);
		Instantiate (thinkies[index], new Vector3(posX, posY, transform.position.z), Quaternion.identity);
	}
}
