using System.Collections.Generic;

namespace Core.Domain.Status
{
    public class StatusManager
    {
        private readonly List<IStatusEffect> _activeEffects = new();
        
        public PlayerCapabilities CurrentCapabilities { get; private set; }
        
        public StatusManager()
        {
            Recalculate();
        }

        public void AddEffect(IStatusEffect effect)
        {
            _activeEffects.Add(effect);
            effect.OnApply();
            Recalculate();
        }

        public void Tick(float deltaTime)
        {
            for (var i = _activeEffects.Count - 1; i >= 0; i--)
            {
                var effect = _activeEffects[i];
                effect.Tick(deltaTime);

                if (!effect.IsExpired) continue;
                
                effect.OnRemove();
                _activeEffects.RemoveAt(i);
                Recalculate();
            }
        }

        private void Recalculate()
        {
            var caps = PlayerCapabilities.Default;
            
            foreach (var effect in _activeEffects)
            {
                effect.ModifyCapabilities(ref caps);
            }
            
            CurrentCapabilities = caps;
        }
    }
}