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

public class EnemySpawner : MonoBehaviour
{
    public static EnemySpawner Instance => instance;
    private static EnemySpawner instance;

    [SerializeField]
    private GameObject m_enemyPrefab;

    [SerializeField]
    private Transform m_enemyParent;

    [SerializeField]
    private List<Helper> m_spawnLocations;

    private bool m_isSpawning = false;
    private List<GameObject> m_enemies;

    public void SetSpawnerEnabled(bool isEnabled)
    {
        m_isSpawning = isEnabled;
    }

    public void CleanUpEnemies()
    {
        StartCoroutine(CleanUp());
    }

    private IEnumerator CleanUp()
    {
        m_isSpawning = false;
        
        for (int i = 0; i < m_enemies.Count; i++)
        {
            Destroy(m_enemies[i]);
        }
        m_enemies.Clear();

        foreach (var h in m_spawnLocations)
        {
            h.SpawnTimer = 5f;
            h.MinSpawnTime = 15f;
            h.MaxSpawnTime = 30f;
        }
        m_isSpawning = true;
        yield return null;
    }

    [System.Serializable]
    private class Helper
    {
        public Transform SpawnLocation;
        public float SpawnTimer;
        public float MinSpawnTime;
        public float MaxSpawnTime;
    }

    private void Awake()
    {
        instance = this;
        m_spawnLocations = new List<Helper>();  
        m_enemies = new List<GameObject>();
        for (int i = 0; i < transform.childCount; i++)
        {
            var _child = transform.GetChild(i);
            m_spawnLocations.Add(new Helper()
            {
                SpawnLocation = _child,
                SpawnTimer = 2f,
                MinSpawnTime = 15f,
                MaxSpawnTime = 30f,
            });
        }
    }

    private void Update()
    {
        if (m_isSpawning == false) return;

        for (int i = 0; i < m_spawnLocations.Count; i++)
        {
            SpawnLogic(m_spawnLocations[i]);
        }
    }

    private void SpawnLogic(Helper spawner)
    {
        spawner.SpawnTimer -= Time.deltaTime;
        if (spawner.SpawnTimer < 0)
        {
            SpawnEnemy(spawner.SpawnLocation);
            spawner.SpawnTimer = Random.Range(spawner.MinSpawnTime, spawner.MaxSpawnTime);
            spawner.MinSpawnTime *= 0.8f;
            spawner.MaxSpawnTime *= 0.8f;

            if (spawner.MinSpawnTime < 2f) spawner.MinSpawnTime = 2f;
            if (spawner.MaxSpawnTime < 4f) spawner.MaxSpawnTime = 4f;
        }
    }

    private void SpawnEnemy(Transform location)
    {
        var _enemy = Instantiate(m_enemyPrefab, m_enemyParent);

        var _dir = GameManager.Instance.Player.transform.position - location.position;
        _dir.y = 0.5f;
        var _lookRot = Quaternion.LookRotation(_dir, Vector3.up);

        _enemy.transform.SetPositionAndRotation(location.position, _lookRot);
        _enemy.gameObject.SetActive(true);
        m_enemies.Add(_enemy.gameObject);
    }
}
