using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Generate : MonoBehaviour
{
	public int Array_Size = 7;
	public GameObject PredioPrefab1;
	public GameObject PredioPrefab2;
	public GameObject PredioPrefab3;
	public GameObject PredioPrefab4;
	public GameObject PredioPrefab5;
	public GameObject PredioPrefab6;
	public GameObject PredioPrefab7;
	public float SpawnRate = 2f;
	public float spawnXposition = 10f;

	private Queue <GameObject> Predios = new Queue<GameObject> ();
	private Vector2 ObjectPoolPosition = new Vector2 (-15f, -20f);
	private float TimeSinceLastSpawned;
	private GameObject currentPredio;
	private GameObject clone;

	private int sorteiaObjeto;

	// Use this for initialization
	void Start()
	{
		for(int i = 0; i < Array_Size; i++)
		{
			InsereAleatorio ();
		}
	}

	void Update()
	{
		TimeSinceLastSpawned += Time.deltaTime;
			if (GameController.instance.GameOver == false && TimeSinceLastSpawned >= SpawnRate) 
		{
			TimeSinceLastSpawned = 0;
			currentPredio = Predios.Dequeue();
			if (currentPredio.transform.position.x < -13) 
			{	
				
				currentPredio.transform.position = new Vector2 (spawnXposition,currentPredio.transform.position.y);
				InsereAleatorio ();
			}
			else
			{	
				clone =	Instantiate (currentPredio, new Vector2(spawnXposition, currentPredio.transform.position.y), Quaternion.identity);
				Destroy (clone,8f);				
				InsereAleatorio ();
			}
				
			
		}
	}

	void InsereAleatorio()
	{
		sorteiaObjeto = Random.Range(1,8);
		switch (sorteiaObjeto) {
		case 1:
			Predios.Enqueue (PredioPrefab1);
			break;
		case 2:
			Predios.Enqueue (PredioPrefab2);
			break;
		case 3:
			Predios.Enqueue (PredioPrefab3);
			break;
		case 4:
			Predios.Enqueue (PredioPrefab4);
			break;
		case 5:
			Predios.Enqueue (PredioPrefab5);
			break;
		case 6:
			Predios.Enqueue (PredioPrefab6);
			break;
		case 7:
			Predios.Enqueue (PredioPrefab7);
			break;
		case 8:
			Predios.Enqueue (PredioPrefab7);
			break;			
		default:
			break;
		}
	}
		
}