#pragma once

#include <blitz/protocol/PacketDispatcher.h>
#include <blitz/protocol/Packets.h>
#include <godot_cpp/classes/node.hpp>

namespace blitz {

class NetworkInterface : public godot::Node, public protocol::PacketDispatcher {
	GDCLASS(NetworkInterface, godot::Node)
  protected:
	static void _bind_methods();

  public:
	NetworkInterface();
	~NetworkInterface();

	void BroadcastPacket(const protocol::Packet& a_Packet);
	void SendPacket(PeerID a_Peer, const protocol::Packet& a_Packet);

	godot::Error JoinGame(const godot::String& a_Address, uint16_t a_Port);
	godot::Error CreateGame(uint16_t a_Port, bool a_Dedicated = false);

	void ShutdownNetwork();

	void _ready() override;

  private:
	void RecievePacketDataReliable(godot::PackedByteArray a_PacketData);
	void RecievePacketDataUnreliable(godot::PackedByteArray a_PacketData);
	void RecievePacketDataUnreliableOrdered(godot::PackedByteArray a_PacketData);

	void OnPlayerConnected(PeerID a_PeerId);
	void OnPlayerDisconnected(PeerID a_PeerId);
	void OnConnectOk();
	void OnConnectFail();
	void OnServerDisconnected();
};

}  // namespace blitz