using System.Collections.Generic;

namespace GatherAndDefend.Events
{
    /// <summary>
    /// makes it possible to add custom and centralized event observables<br/>
    /// </summary>
    public class EventAggregator : Singleton<EventAggregator>
    {
        private List<EventBase> events = new List<EventBase>();
        /// <summary>
        /// returns an event observable of given type. Creates it if non-existent
        /// </summary>
        /// <typeparam name="T">the type of the event</typeparam>
        /// <returns></returns>
        public T GetEvent<T>() where T : EventBase, new()
        {
            var evt = events.Find(x => x is T);
            if (evt == null)
            {
                evt = new T();
                events.Add(evt);
            }
            return evt as T;

        }
    }
}