#pragma once

#include "ros_bridge/include/ComPrivateInclude.h"
#include "ros_bridge/include/RosBridgeClient.h"
#include "ros_bridge/include/TypeFactory.h"
#include "ros_bridge/include/CasePacker.h"

namespace ROSBridge {
namespace ComPrivate {

class TopicSubscriber
{
    typedef std::vector<std::string> ClientList;
public:    
    typedef std::function<void(JsonDocUPtr)> CallbackType;
    typedef std::map<HashType, CallbackType> CallbackMap;

    TopicSubscriber() = delete;
    TopicSubscriber(CasePacker *casePacker, RosbridgeWsClient *rbc);
    ~TopicSubscriber();

    //! @brief Starts the subscriber.
    void start();

    //! @brief Resets the subscriber.
    void reset();

    //! @return Returns false if a subscription to this topic allready exists.
    //!
    //! @note Only one callback can be registered.
    bool subscribe(const char* topic, const CallbackType &callback);

private:
    CasePacker         *_casePacker;
    RosbridgeWsClient  *_rbc;
    CallbackMap         _callbackMap;
    ClientList          _clientList;
    bool                _running;
};

void subscriberCallback(const HashType &hash,
                        const TopicSubscriber::CallbackMap &map,
                        std::shared_ptr<WsClient::Connection> /*connection*/,
                        std::shared_ptr<WsClient::InMessage> in_message);

void test(std::shared_ptr<WsClient::Connection> /*connection*/,
                        std::shared_ptr<WsClient::InMessage> in_message);
} // namespace ComPrivate
} // namespace ROSBridge
