#pragma once

#include "blitz/network/Connexion.h"
#include "blitz/network/TCPSocket.h"
#include "blitz/protocol/PacketDispatcher.h"
#include "blitz/protocol/PacketHandler.h"

namespace blitz {

namespace game {

class Player;

}  // namespace game


namespace server {

class Server;

struct KeepAlive {
	std::uint64_t keepAliveID = 0;
	std::uint64_t sendTime;
	bool recievedResponse = false;
};


class ServerConnexion : public network::Connexion {
  private:
	Server* m_Server;
	std::uint8_t m_ID;
	KeepAlive m_KeepAlive;
	game::Player* m_Player;
	std::string m_ChatColor;

  public:
	ServerConnexion(Server* server, network::TCPSocket& socket, std::uint8_t id);
	ServerConnexion(ServerConnexion&& move);
	virtual ~ServerConnexion();

	void Start();

	virtual void HandlePacket(const protocol::PlayerLoginPacket* packet) override;
	virtual void HandlePacket(const protocol::KeepAlivePacket* packet) override;
	virtual void HandlePacket(const protocol::DisconnectPacket* packet) override;
	virtual void HandlePacket(const protocol::ChatPacket* packet) override;

	std::uint8_t GetID() const {
		return m_ID;
	}

	virtual bool UpdateSocket();

	REMOVE_COPY(ServerConnexion);

  private:
	void RegisterHandlers();
	void CheckKeepAlive();
	void SendKeepAlive();
	void InitConnection();
	void InitPlayerChatColor();
};

}  // namespace server
}  // namespace blitz
