#pragma once

/**
 * \file TCPListener.h
 * \brief File containing the blitz::network::TCPListener class
 */

#include "TCPSocket.h"

namespace blitz {
namespace network {

/**
 * \class TCPListener
 * \brief Cross platform abstraction of a TCP socket server
 */
class TCPListener {
  private:
	SocketHandle m_Handle;
	std::uint16_t m_Port;
	int m_MaxConnections;

  public:
	/**
	 * \brief Default constructor
	 */
	TCPListener();
	/**
	 * \brief Default destructor
	 */
	~TCPListener();

	/**
	 * \brief Starts listening for guests to connect
	 * \param port The port to listen to
	 * \param maxConnexions The maximum amount of connexion that can happen at the same time. \n
	 * Every other guests will be kicked if this amount is reached.
	 * \return Wether this action was succesfull
	 */
	bool Listen(std::uint16_t port, int maxConnexions);

	/**
	 * \brief Tries to accept an incoming request to connect
	 * \param newSocket the empty socket to put the result to
	 * \return true if a new connexion was accepted
	 */
	bool Accept(TCPSocket& newSocket);

	/**
	 * \brief Destroys the socket
	 */
	void Destroy();

	/**
	 * \brief Closes transmissions
	 * \return true if this action was succesfull
	 */
	bool Close();

	/**
	 * \brief Allows to set the socket in non blocking/blocking mode
	 * \param block If set to true, every call to Read will wait until the socket receives something
	 * \return true if the operation was successful
	 */
	bool SetBlocking(bool blocking);

	/**
	 * \brief Getter of the m_Port member
	 * \return The port which the socket listen to
	 */
	std::uint16_t GetListeningPort() const {
		return m_Port;
	}

	/**
	 * \brief Getter of the m_MaxConnections member
	 * \return The maximum amount of connexions that can happen at the same time.
	 */
	int GetMaximumConnections() const {
		return m_MaxConnections;
	}

	/**
	 * \brief Disables TCPListener copy
	 */
	REMOVE_COPY(TCPListener);
};

}  // namespace network
}  // namespace blitz