using System;
using System.Collections.Generic;
using Crysome.Common.Network;
using Crysome.Common.Network.Packets;

namespace Crysome.Server.Network;

public class PacketChannel
{
	public readonly Dictionary<Type, Action<CrysomeClient, IPacket>> Handlers;

	public PacketChannel()
	{
		Handlers = new Dictionary<Type, Action<CrysomeClient, IPacket>>();
	}

	public void HandlePacket(CrysomeClient sender, IPacket packet)
	{
		if (Handlers.TryGetValue(((object)packet).GetType(), out var value))
		{
			value(sender, packet);
		}
	}

	public void RegisterHandler<TPacket>(Action<CrysomeClient, IPacket> handler)
	{
		Handlers[typeof(TPacket)] = handler;
	}
}
