using InABox.Clients;
using InABox.Core;
using InABox.IPC;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace InABox.Client.IPC
{
    public static class IPCClientFactory
    {
        private static ConcurrentDictionary<string, IPCClientTransport> Clients = new();

        public static IPCClientTransport GetClient(string pipeName)
        {
            if (!Clients.TryGetValue(pipeName, out var client))
            {
                client = new IPCClientTransport(pipeName);
                Clients[pipeName] = client;
                client.OnPush += Client_OnPush;
            }
            return client;
        }

        private static void Client_OnPush(IPCMessage request)
        {
            if(request.Method == Method.Push)
            {
                if(request.Type is not null && CoreUtils.TryGetEntity(request.Type, out var entity))
                {
                    ClientFactory.PushHandlers.Push(entity, request.GetRequest(entity));
                }
            }
        }
    }
}
