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

public class WaveController : BaseSceneManager {

	bool gameOver = false;

	public GameObject[] wavesToStrikeWith;
	public Vector3 startPosition;

	public EncounterMessage messageEncounter;

	public float randomTimeMin = 5;
	public float randomTimeMax = 8;

	float timeToWait;

	float TimeSinceLastWave = 0;
	int currentWave;

	// Use this for initialization
	void Start () {
		for (int i = 0; i < wavesToStrikeWith.Length; i++) {
			wavesToStrikeWith [i].SetActive (false);
		}

		currentWave = 0;
		timeToWait = Random.Range (randomTimeMin, randomTimeMax);
	}
	
	// Update is called once per frame
	void Update () {
		if (encounterBegun) {
			if (!gameOver) {
				if (Time.time - TimeSinceLastWave > timeToWait) {
					//DO OUR STUFF HERE
					GameObject nextWave = wavesToStrikeWith [currentWave];
					nextWave.GetComponent<Wave> ().dealtDamage = false;
					nextWave.transform.position = startPosition;

					nextWave.SetActive (true);
					currentWave++;
					TimeSinceLastWave = Time.time;
					timeToWait = Random.Range (randomTimeMin, randomTimeMax);

					if (currentWave == wavesToStrikeWith.Length) {
						// we've won
						timeToWait = Time.time;
						gameOver = true;
					}
				}
			} else {

				messageEncounter.ShowSceneWithText ("You and your crew have successfully managed to survive through the storm, although you may be worse for wear!");


				if (Time.time - timeToWait > 20)
				{
					SceneManager.LoadScene ("BookScene");
				}
			}
		}
	}
}
