#pragma once

#include "include/adapters/qt.h"
#include "include/async.h"
#include "include/hiredis.h"
#include "MWareInterface.h"
#include <QtCore/QObject>
#include <QtCore/QDebug>

class Redis : public QObject
{
    Q_OBJECT
public:
    explicit Redis(QObject *parent = 0);
    ~Redis();

public slots:
    void start();
    void conn();
    bool hset(QString m, QString k, QString v);
    QString hget(QString m, QString k);
    QHash<QString, QString> hgetall(QString k);
    QStringList hkeys(QString k);
    QStringList hvals(QString k);
    bool set(QString k, QString v);
    bool setb(QString k, QByteArray &v);
    QString get(QString k);
    QByteArray getb(QString k);
    bool rpush(QString lData, QString js);                  //向队列尾部加入字符串数据
    bool rpushb(QString lData, QByteArray ba);              //向队列尾部加入二进制数据
    QString lpop(QString lData);
    QString blpop(QString lData, quint32 timeout);          //同步阻塞一定时间返回数据
    bool blpop(QString lData, redisCallbackFn *fn);         //阻塞从队列头部获取最早数据
    bool publish(QString ch, QString js);
    bool publishb(QString ch, QByteArray ba);
    bool expire(QString k,int sec);
    void subscribe(QString ch, EventSubInterface *fn);      // 订阅
    void psubscribe(QString ch, EventSubInterface *fn);     // 订阅：模式匹配

private:
    QString ip = "127.0.0.1";
    quint16 port = 16379;
    QString auth = "<LanPeng&&CeKong>";

    bool redisOk = false;               //redis全局状态，操作出错时置为false, redis线程内会自动重连
    redisContext *rc;                   //同步连接
    redisAsyncContext *rac = nullptr;   //异步连接
    RedisQtAdapter adapter;             //异步事件驱动适配器

public:
    void Setup(tagSetup ts);
};
