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

namespace GGJ2021
{
    public class EnemyManager : MonoBehaviour
    {
        enum EnemiesState
        {
            Chill,
            Chasing
        }

        private EnemiesState _enemiesState;
        private EnemyController[] _enemies;

        private void Start()
        {
            _enemies = FindObjectsOfType<EnemyController>();
            _enemiesState = EnemiesState.Chill;
        }

        private void Update()
        {
            if (_enemiesState != EnemiesState.Chasing && CheckIfAnyChasing())
            {
                // TODO: Play chasing music
                _enemiesState = EnemiesState.Chasing;
            }
            else if (_enemiesState != EnemiesState.Chill && CheckIfAllChill())
            {
                // TODO: Play default music
                _enemiesState = EnemiesState.Chill;
            }
        }

        private bool CheckIfAnyChasing()
        {
            for (int i = 0; i < _enemies.Length; i++)
                if (_enemies[i].State == EnemyController.EnemyState.Chasing)
                    return true;
            return false;
        }

        private bool CheckIfAllChill()
        {
            for (int i = 0; i < _enemies.Length; i++)
                if (_enemies[i].State == EnemyController.EnemyState.Patrolling || _enemies[i].State == EnemyController.EnemyState.Waiting)
                    continue;
                else
                    return false;
            return true;
        }
    }
}
