using UnityEngine;
using System.Collections;

public class WavesSection3 : MonoBehaviour {
	public GameObject gameManager;
	//public int TimeBetweenTentacleAttacks = 8;
	public float NumberOfWavesInEachWave = 8;

	public float TimeBetweenSetOfWaves = 3f;

	public float BossIsVulnerableTime = 2f;

	public float TimeBetweenEachWave = 1f;

	int currentWavesThrown = 0;

	public GameObject TentacleAttack;

	public Transform[] PossibleTentacleSpawn;

	public GameObject SeahorseEnemy;
	public GameObject BashFishEnemy;

	public Transform[] PossibleEnemySpawn;

	void Start () {
		InvokeWave ();

	}


	void Update () {

	}

	void InvokeWave()
	{
		Invoke ("AttackPattern", TimeBetweenEachWave);
	}
	void AttackPattern()
	{
		if (currentWavesThrown < NumberOfWavesInEachWave) 
		{
			StartCoroutine (WaitASecond());	
		}
		if (currentWavesThrown >= NumberOfWavesInEachWave) 
		{
			StartCoroutine (SpawnNewWave ());
		}

	}

	IEnumerator WaitASecond()
	{

		yield return new WaitForSeconds (TimeBetweenEachWave);

		int choice = Random.Range (1, 4);

		if (choice == 1) 
		{
			gameManager.GetComponent<GameManagerScript> ().SpawnWaveInLane1 ();
		}
		else if (choice == 2) 
		{
			gameManager.GetComponent<GameManagerScript> ().SpawnWaveInLane2 ();
		}
		else if (choice == 3) 
		{
			gameManager.GetComponent<GameManagerScript> ().SpawnWaveInLane3 ();
		}
		//Debug.Log (choice);
		if (currentWavesThrown % 2 == 0) 
		{
			SpawnTentacle();
		}
		if (currentWavesThrown % 3 == 0) 
		{
			SpawnEnemies();
		}

		currentWavesThrown = currentWavesThrown + 1;
		Invoke ("AttackPattern", 1f);

	}

	IEnumerator SpawnNewWave()
	{
		yield return new WaitForSeconds (TimeBetweenSetOfWaves);
		currentWavesThrown = 0;
		Invoke ("AttackPattern", TimeBetweenEachWave);
	}

	public void SpawnTentacle()
	{
	//	Vector3 TentacleLocation = PossibleTentacleSpawn [Random.Range (0, 4)].transform.position;
		//Instantiate (TentacleAttack, TentacleLocation, Quaternion.identity);

	}

	public void SpawnEnemies()
	{
		Vector3 SeahorseLocation = PossibleEnemySpawn [Random.Range (0, 4)].transform.position;
		Instantiate (SeahorseEnemy, SeahorseLocation, Quaternion.identity);

		Vector3 BashFishLocation = PossibleEnemySpawn [Random.Range (0, 4)].transform.position;
		Instantiate (BashFishEnemy, BashFishLocation, Quaternion.identity);
	}

}
