#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 {


typedef std::function<void(JsonDocUPtr)> CallbackType;
struct MapStruct{
    typedef std::map<HashType, CallbackType> Map;
    Map map;
    std::mutex mutex;
};

class TopicSubscriber
{
    typedef std::vector<std::string> ClientList;
public:

    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;
    MapStruct           _callbackMapStruct;
    ClientList          _clientList;
    bool                _running;
};

void subscriberCallback(const HashType &hash,
                        MapStruct &mapWrapper,
                        std::shared_ptr<WsClient::Connection> /*connection*/,
                        std::shared_ptr<WsClient::InMessage> in_message);
} // namespace ComPrivate
} // namespace ROSBridge
