using Unity.Burst;
using Unity.Entities;

namespace SalientVoid
{
    [UpdateAfter(typeof(InitializationSystemGroup))]
    public partial struct VictorySystem : ISystem
    {
        private bool _initialSpawnDetected;
        
        [BurstCompile]
        public void OnCreate(ref SystemState state)
        {
            _initialSpawnDetected = false;
            state.RequireForUpdate<CharacterTag>();
        }
        
        public void OnUpdate(ref SystemState state)
        {
            bool foundEnemy = false;
            foreach (RefRO<EnemyTag> _ in SystemAPI.Query<RefRO<EnemyTag>>())
            {
                _initialSpawnDetected = true;
                foundEnemy = true;
                break;
            }
            
            if (_initialSpawnDetected && !foundEnemy)
            {
                GameplayUIController.Instance.ShowVictoryScreen();
                _initialSpawnDetected = false;
            }
        }

        [BurstCompile]
        public void OnDestroy(ref SystemState state) { }
    }
}