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

public class EnemyHandler : MonoBehaviour {

    public List<Transform> spawnPoints = new List<Transform>();

    [Tooltip("How often to spawn an enemy (seconds)")]
    public int spawnClocker = 3;

    public List<GameObject> spawnables = new List<GameObject>();

    private bool spawning = true;
    private GameManager gameManager;

    void Start() {
        gameManager = FindObjectOfType<GameManager>();

        InvokeRepeating("SpawnOne", spawnClocker, spawnClocker);
    }

    void Disable() {
        if (spawning) {
            CancelInvoke();
            spawning = false;
        }
    }

    void Enable() {
        if (!spawning) {
            InvokeRepeating("SpawnOne", spawnClocker, spawnClocker);
            spawning = true;
        }
    }

    void Update() {
        if (gameManager.IsGamePaused())
            Disable();
        else
            Enable();
    }

    // Spawn enemy on random spawn point
    void SpawnOne() {
        int index = (int)Random.Range(0, spawnPoints.Count);
        Transform spawnPoint = spawnPoints[index];

        int enemyIndex = (int)Random.Range(0, spawnables.Count);

        Instantiate(spawnables[enemyIndex], spawnPoint.position, spawnPoint.rotation, transform);
    }

    // Spawns enemy on all spawn points
    void SpawnAll() {
        int enemyIndex;
        for (int i = 0; i < spawnPoints.Count; i++) {
            enemyIndex = (int)Random.Range(0, spawnables.Count);
            Instantiate(spawnables[enemyIndex], spawnPoints[i].position, spawnPoints[i].rotation, transform);
        }
    }
}
