using System;
using Assets.Source.Patterns.EventBus.Events;
using Assets.Source.Patterns.EventBus.Interfaces;

namespace Assets.Source.Patterns.EventBus
{
    internal class Subscription<TEventBase> : ISubscription where TEventBase : EventBase
    {
        public SubscriptionToken SubscriptionToken { get; private set; }

        public Subscription(Action<TEventBase> action, SubscriptionToken token)
        {
            _action = action; //?? throw new ArgumentNullException(nameof(action));
            SubscriptionToken = token; //?? throw new ArgumentNullException(nameof(token));
        }

        public void Publish(EventBase eventItem)
        {
            if (!(eventItem is TEventBase))
                throw new ArgumentException("Event Item is not the correct type.");

            _action.Invoke(eventItem as TEventBase);
        }

        private readonly Action<TEventBase> _action;
    }
}
