using System;
using System.Collections.Generic;
using Assets.Source.Misc;
using Assets.Source.Patterns.EventBus;
using Assets.Source.Patterns.EventBus.Events;
using UnityEngine;

namespace Assets.Source.Utils
{
    public static class EBus
    {
        private static readonly EventBus _eventBus = new EventBus();

        public static SubscriptionToken Subscribe<TEventBase>(Action<TEventBase> action, IReleaseTracker tracker = null) where TEventBase : EventBase
        {
            return Application.isPlaying ? _eventBus.Subscribe(action, tracker) : null;
        }

        public static void Unsubscribe(SubscriptionToken token)
        {
            if (!Application.isPlaying)
                return;
            _eventBus.Unsubscribe(token);
        }

        public static void Unsubscribe(ICollection<SubscriptionToken> collection)
        {
            if (!Application.isPlaying)
                return;
            foreach (var token in collection)
                _eventBus.Unsubscribe(token);
        }

        public static void Publish<TEventBase>(TEventBase eventItem) where TEventBase : EventBase
        {
            if (!Application.isPlaying)
                return;
            _eventBus.Publish(eventItem);
        }
    }
}
