#pragma once
#include <drogon/nosql/RedisSubscriber.h>
#include "RedisConnection.h"
#include "SubscribeContext.h"
#include <mutex>
#include <unordered_map>
#include <memory>
#include <list>
namespace drogon::nosql
{
class RedisSubscriberImpl
    : public RedisSubscriber,
      public std::enable_shared_from_this<RedisSubscriberImpl>
{
  public:
    ~RedisSubscriberImpl() override;
    void subscribe(const std::string &channel,
                   RedisMessageCallback &&messageCallback) noexcept override;
    void psubscribe(const std::string &pattern,
                    RedisMessageCallback &&messageCallback) noexcept override;
    void unsubscribe(const std::string &channel) noexcept override;
    void punsubscribe(const std::string &pattern) noexcept override;
    void setConnection(const RedisConnectionPtr &conn);
    void clearConnection();
    void subscribeNext();
    void subscribeAll();
  private:
    RedisConnectionPtr conn_;
    std::unordered_map<std::string, std::shared_ptr<SubscribeContext>>
        subContexts_;
    std::unordered_map<std::string, std::shared_ptr<SubscribeContext>>
        psubContexts_;
    std::list<std::shared_ptr<std::function<void(const RedisConnectionPtr &)>>>
        tasks_;
    std::mutex mutex_;
};
}  