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

public class SpawnController : MonoBehaviour
{
    public GameObject Enemy;
    public float spawnDelayInSeconds = 1f;
    private GameObject leftSpawner;
    private GameObject rightSpawner;
    private int daysPassed = 0;
    // This function is called just one time by Unity the moment the component loads
    private void Start()
    {
        EventManager.SubscribeSunrise(StartNextWave);
        leftSpawner = GameObject.Find("LeftSpawn");
        rightSpawner = GameObject.Find("RightSpawn");
    }

        // Update is called once per frame
        void Update()
    {
        
    }

    void StartNextWave()
    {
        daysPassed++;
        //// THIS IS A HACK - there could be a damage coroutine running and we need to reset the global player can take damage option in case an object was destroyed right after damaging the player
        //Assets.GlobalVars.CanPlayerTakeDamage = true;
        StartCoroutine(SpawnEnemies());
    }

    IEnumerator SpawnEnemies()
    {
        GameObject spawnParent;
        if (daysPassed % 2 == 0)
        {
            spawnParent = rightSpawner;
        }
        else
        {
            spawnParent = leftSpawner; 
        }

        for (int i = 0; i < 4; i++)
        {
            GameObject childObject = Instantiate(Enemy, spawnParent.transform.position, spawnParent.transform.rotation);
            childObject.transform.parent = gameObject.transform;

            yield return new WaitForSeconds(spawnDelayInSeconds);
        }
        yield return null;
    }
}
