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

[System.Serializable]
public struct EnemySpawnInfo
{
    public enum Path
    {
        Top, //0
        Bot, //1
        Right, //2 
        Left, //3
        Hole, //4
    }
    public GameObject enemyGO;
    public Path spawnPoint;
    public float time;
}

public class Wave : MonoBehaviour
{
    public EnemySpawnInfo[] Enemies;
    public float BreakTime = 1.0f;

    private Transform[] SpawnPoints;
    private float CurrentEnemyTimer;
    private int CurrentEnemy = 0;
    private bool WaveIsActive = false;

    private void Awake()
    {
        SpawnPoints = GetComponentInParent<RoundsManager>().SpawnPoints;

    }
    private void Start()
    {
        CurrentEnemyTimer = Enemies[CurrentEnemy].time;
    }

    private void Update()
    {
        if(WaveIsActive && CurrentEnemyTimer <= 0)
        {
            if (CurrentEnemy < Enemies.Length)
            {
                SpawnNewEnemy(Enemies[CurrentEnemy]);
                CurrentEnemy++;
            }
        }
        CurrentEnemyTimer -= Time.deltaTime;
    }

    internal void ActivateWave()
    {
        Debug.Log("Comienza nueva oleada");
        WaveIsActive = true;
    }

    internal bool WaveHasEnded()
    {
        return CurrentEnemy >= Enemies.Length;
    }

    internal void SpawnNewEnemy(EnemySpawnInfo newEnemy)
    {
        switch(newEnemy.spawnPoint)
        {
            case EnemySpawnInfo.Path.Top:
                Instantiate(newEnemy.enemyGO, SpawnPoints[0].position, Quaternion.identity);
                break;
            case EnemySpawnInfo.Path.Bot:
                Instantiate(newEnemy.enemyGO, SpawnPoints[1].position, Quaternion.identity);
                break;
            case EnemySpawnInfo.Path.Right:
                Instantiate(newEnemy.enemyGO, SpawnPoints[2].position, Quaternion.identity);
                break;
            case EnemySpawnInfo.Path.Left:
                Instantiate(newEnemy.enemyGO, SpawnPoints[3].position, Quaternion.identity);
                break;
            case EnemySpawnInfo.Path.Hole:
                break;
            default:
                break;
        }

        if(CurrentEnemy + 1 < Enemies.Length)
        {
            CurrentEnemyTimer = Enemies[CurrentEnemy + 1].time - Enemies[CurrentEnemy].time;
        }

    }
}
