#ifndef SERVER_SESSION_TABLE_HPP_
#define SERVER_SESSION_TABLE_HPP_

#include <vector>
#include <string>

#include <UserDB/UserDB.hpp>
#include <Serialize/Serialize.hpp>
#include <Material/Material.hpp>
#include <ToolTip/ToolTip.hpp>

struct ServerSession
{
    std::string server;
    std::string username;
    UserDB::SessionID sid;

    std::string getLabel() const;
    bool operator==(ServerSession other);
};

template<>
struct SerialCodec<ServerSession>
{
    static void encode(std::vector<uint8_t> &outBytes, const ServerSession &session)
    {
        serializeInto(outBytes, session.server, session.username, session.sid);
    }

    static size_t decode(const uint8_t *data, size_t size, ServerSession &session)
    {
        return deserialize(data, size, session.server, session.username, session.sid);
    }
};

/**
 * @brief Try loading the server session table.
 */
void initServerSessionTable();

/**
 * @brief Set the server session ID.
 * 
 * This function is thread-safe.
 * 
 * @param server The server for which the session ID changed.
 * @param username The username.
 * @param sid The session ID.
 */
void setServerSession(std::string server, std::string username, UserDB::SessionID sid);

/**
 * @brief Get the last know session ID for the specified server and user.
 */
UserDB::SessionID getLastKnownSessionID(std::string server, std::string username);

/**
 * @brief Return a copy of the list of sessions.
 */
std::vector<ServerSession> getServerSessions();

/**
 * @brief Returns a number which changes every time the list changes.
 */
uint64_t getServerSessionsGeneration();

#endif