#pragma once

#include "ros_bridge/include/com_private.h"
#include "ros_bridge/include/RosBridgeClient.h"

#include <thread>
#include <atomic>
#include <mutex>
#include <condition_variable>

namespace ros_bridge {
namespace com_private {

struct ThreadData;

class TopicPublisher
{
    typedef std::unique_ptr<std::thread> ThreadPtr;
    using CondVar = std::condition_variable;
public:

    TopicPublisher() = delete;
    TopicPublisher(RosbridgeWsClient &rbc);

    ~TopicPublisher();

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

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

    void publish(JsonDocUPtr docPtr, const char *topic);
    bool advertise(const char *topic, const char *type);

private:
    using TopicMap = std::unordered_map<std::string, std::string>;
    JsonQueue           _queue;
    TopicMap            _topicMap;
    std::mutex          _mutex;
    std::shared_ptr<std::atomic_bool>   _stopped;
    RosbridgeWsClient  &_rbc;
    CondVar             _cv;
    ThreadPtr           _pThread;
};


} // namespace CommunicatorDetail
} // namespace ROSBridge
