#ifndef CLIENT_HPP_
#define CLIENT_HPP_
#include "poll.hpp"
#include "irc_msg.hpp"
#include "fdtable.hpp"
#include "sock.hpp"


class ClientState; // fwd
class Client {
    private:
        Client(int fd, const char* ip); ///< creates and registers Poll event

        void handle(event_t); ///< handle io (timeout, in, out)
        bool handle_in();
        bool handle_out();

        const int fd;
        time_t last_seen;
        MessageStreamIn msg_stream_in;
        MessageStreamOut msg_stream_out;

        static FdTable<Client*> client_fds;

    public:
        static void createInst(int fd, const ip_str_t); ///< static wrapper for constructor
        ~Client(); ///< disconnects from server/channels (if any), removes event handlers, closes fd

        static void handle(int, event_t); ///< finds Client to fd and calls its handle()
        static void killall(const char*); ///< broadcasts message and calls all ~Client()
        bool send(msg_t*, size_t); ///< send message. might enqueue operation, free's buf afterwards.

        const char* const host; ///< client ip
        const char* const addr; ///< client ip:port
        static const char* const dummy_host; ///< #host to show for non-ops
        ClientState* irc_state;
};


#endif