#pragma once

/**
 * \file ServerConnexion.h
 * \brief File containing the blitz::server::ServerConnexion class
 */

#include "blitz/network/Connexion.h"

namespace blitz {

namespace game {

class Player;

}  // namespace game


namespace server {

class Server;

/**
 * \struct KeepAlive
 * \brief Structure containing the keep alive informations
 * \var KeepAlive::KeepAliveID The keep alive ID
 * \var KeepAlive::SendTime The time when the keep alive was sent
 * \var KeepAlive::RecievedResponse If the keep alive has recieved a response
 */
struct KeepAlive {
	std::uint64_t KeepAliveID = 0;
	std::uint64_t SendTime;
	bool RecievedResponse = false;
};

/**
 * \class ServerConnexion
 * \brief Class used to manage the server connexion
 */
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:
	/**
	 * \brief Constructor
	 * \param server The server
	 * \param socket The socket
	 * \param id The ID of the connexion
	 */
	ServerConnexion(Server* server, network::TCPSocket& socket, std::uint8_t id);
	/**
	 * \brief Move constructor
	 */
	ServerConnexion(ServerConnexion&& move);
	virtual ~ServerConnexion();

	/**
	 * \brief Start the connexion
	 */
	void Init();

	/**
	 * \brief Get the ID of the connexion
	 * \return The ID of the connexion
	 */
	std::uint8_t GetID() const {
		return m_ID;
	}

	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;
	virtual void HandlePacket(const protocol::PlayerPositionAndRotationPacket* packet) override;
	virtual void HandlePacket(const protocol::PlayerShootPacket* packet) override;

	virtual bool UpdateSocket() override;

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

}  // namespace server
}  // namespace blitz
