#include "blitz/protocol/PacketFactory.h"
#include "blitz/protocol/Packets.h"
#include <functional>
#include <map>

namespace blitz {
namespace protocol {
namespace PacketFactory {

using PacketCreator = std::function<PacketPtr()>;

static std::map<PacketType, PacketCreator> packets = {
	{PacketType::PlayerLogin, []() -> PacketPtr { return std::make_unique<PlayerLoginPacket>(); }},
	{PacketType::KeepAlive, []() -> PacketPtr { return std::make_unique<KeepAlivePacket>(); }},
	{PacketType::PlayerList, []() -> PacketPtr { return std::make_unique<PlayerListPacket>(); }},
	{PacketType::PlayerJoin, []() -> PacketPtr { return std::make_unique<PlayerJoinPacket>(); }},
	{PacketType::PlayerLeave, []() -> PacketPtr { return std::make_unique<PlayerLeavePacket>(); }},
	{PacketType::ConnectionInfo, []() -> PacketPtr { return std::make_unique<ConnexionInfoPacket>(); }},
	{PacketType::Disconnect, []() -> PacketPtr { return std::make_unique<DisconnectPacket>(); }},
	{PacketType::ServerTps, []() -> PacketPtr { return std::make_unique<ServerTpsPacket>(); }},
	{PacketType::Chat, []() -> PacketPtr { return std::make_unique<ChatPacket>(); }},
};

PacketPtr CreatePacket(PacketType type, DataBuffer& buffer) {
	PacketPtr packet = packets[type]();
	packet->Deserialize(buffer);
	return packet;
}

}  // namespace PacketFactory
}  // namespace protocol
}  // namespace blitz
