using System;
using Assets.Source.Core.Impacting;

namespace Assets.Source.Core.Effecting
{
    public abstract class Effect
    {
        private int _stacks = 1;

        public event Action OnExpire;
        public event Action OnStackAdd;
        public event Action OnStackSubtract;

        public int Stacks
        {
            get { return _stacks; }
            set
            {
                var delta = value - _stacks;
                if (delta < 0)
                    for (int i = 0; i < -delta; i++)
                        OnStackSubtract?.Invoke();
                if (delta > 0)
                    for (int i = 0; i < delta; i++)
                        OnStackAdd?.Invoke();

                _stacks = value;
                if (_stacks > 0)
                    return;
                OnExpire?.Invoke();
            }
        }

        public IImpactable Owner { get; set; }

        public void Stack(int stacks)
        {
            Stacks += stacks;
        }

        public void Subtract(int stacks)
        {
            Stacks -= stacks;
        }
    }
}
