#pragma once

#include <SDL_net.h>
#include "Constants.h"

class Server {
    public:
    Server(int port);
    ~Server();

    int FindFreeClientIndex() const;
    int FindExistingClientIndex( const IPaddress &ip ) const;
    bool IsClientConnected( int clientIndex ) const;
    const IPaddress & GetClientAddress( int clientIndex ) const;

    void update(unsigned int delta);

    void gameTick();

    private:
    UDPsocket udpSocket;

    UDPpacket *connectionDenied;
    UDPpacket *connectionAccepted;
    UDPpacket *receivedPacket;

    int m_maxClients;
    int m_numConnectedClients;
    bool m_clientConnected[constants::MAX_CLIENTS];
    IPaddress m_clients[constants::MAX_CLIENTS];

    bool sendPacket(UDPpacket &packet);

    bool sendConnectionDenied(IPaddress dest);
    bool sendConnectionAccepted(IPaddress dest, int index);
};