using System;
using System.Collections.Generic;
using System.Text;

namespace InABox.Core
{
    /// <summary>
    /// Static class through which the server registers <see cref="IPusher"/>s and <see cref="IPollHandler"/>s, as well as actually pushing messages.
    /// </summary>
    public class PushManager
    {
        private static List<IPollHandler> Handlers = new List<IPollHandler>();

        private static List<IPusher> Pushers { get; set; } = new List<IPusher>();

        private PushManager() { }

        public static void AddPusher(IPusher pusher) =>
            Pushers.Add(pusher);

        public static void Push<TPush>(TPush push) where TPush : BaseObject => 
            Pushers.ForEach(x => x.Push(push));

        public static void Push<TPush>(Guid session, TPush push) where TPush : BaseObject =>
            Pushers.ForEach(x => x.Push(session, push));

        public static void PushUser<TPush>(Guid userID, TPush push) where TPush : BaseObject =>
            Pushers.ForEach(x => x.PushUser(userID, push));

        public static void Push<TPush>(Platform platform, TPush push) where TPush : BaseObject => 
            Pushers.ForEach(x => x.Push(platform, push));

        public static void Poll(Guid session)
        {
            foreach (var handler in Handlers)
            {
                foreach (var push in handler.Poll(session))
                {
                    Pushers.ForEach(x => x.Push(session, handler.Type, push));
                }
            }
        }

        public static void AddPollHandler<TPush>(PollHandler<TPush> handler)
            where TPush : BaseObject
        {
            Handlers.Add(handler);
        }
        public static void AddPollHandler<TPush>(PollHandler<TPush>.PollEvent poll)
            where TPush : BaseObject
        {
            Handlers.Add(new PollHandler<TPush>(poll));
        }
    }
}
