using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class SpawnManager : MonoBehaviour {

    public Text waveText;
    public Text enemyRemainingText;

    public GameObject[] enemyRoster;
    public GameObject[] bossRoster;
    public int monsterCount, waveCount = 0;
    public int remainingAlive = 0;
    public float bossWaveNum = 5;
    public Transform spawnPoint;

    public float waitToWave;

    private bool bossWave;

    public float spawnTime = 0, spawnInterval;

    public AudioSource _AudioSource;

    public AudioClip _AudioClip1;
    public AudioClip _AudioClip2;

    // Use this for initialization
    void Start ()
    {
        _AudioSource.clip = _AudioClip1;

        _AudioSource.Play();
    }
	
	// Update is called once per frame
	void Update () {

        if (remainingAlive == 0)
        {

            if (waitToWave <= spawnTime)
            {
                waveCount++;
                monsterCount++; // add more frenemies

                if (waveCount != bossWaveNum)
                {
                    if (_AudioSource.clip == _AudioClip2)
                    {

                        _AudioSource.clip = _AudioClip1;

                        _AudioSource.Play();

                    }

                    _AudioSource.clip = _AudioClip1;                 
                    spawnTime = 0;
                    StartCoroutine(SpawnWave());
                }

                else
                {
                    if (_AudioSource.clip == _AudioClip1)
                    {

                        _AudioSource.clip = _AudioClip2;

                        _AudioSource.Play();

                    }
                    SpawnBoss();
                    bossWaveNum += 5;
                }

            }

            else
            {
                spawnTime += Time.deltaTime;
            }
        }	
	}

    IEnumerator SpawnWave()
    {
        for (int i = 0; i < monsterCount; i++)
        {
            
            int enemyRosterIndex = Random.Range(0, enemyRoster.Length);

            if (waveCount == 1)
            {
                enemyRosterIndex = 0;
            }
            if (waveCount == 2)
            {
                enemyRosterIndex = 3;
            }
            if (waveCount == 3)
            {
                enemyRosterIndex = 2;
            }
            if (waveCount == 4)
            {
                enemyRosterIndex = 1;
            }



            Instantiate(enemyRoster[enemyRosterIndex], new Vector2(Random.Range(-8, 8), spawnPoint.position.y), Quaternion.identity);
            remainingAlive++;
            yield return new WaitForSeconds(spawnInterval);
        }
    }

    public void DeductRemainingAlive(int count)
    {   
        remainingAlive -= count;
        
    }

    public void SpawnBoss()
    {
        int bossRosterIndex = Random.Range(0, bossRoster.Length);
        Instantiate(bossRoster[bossRosterIndex], new Vector2(Random.Range(-8, 8), spawnPoint.position.y), Quaternion.identity);
        remainingAlive++;
    }
}
