#ifndef CHATMODEL_H
#define CHATMODEL_H

#include <QAbstractListModel>
#include <QDateTime>
#include <QStringList>

#include <rtcom-eventlogger/eventlogger.h>
#include <glib.h>
#include <glib/gstdio.h>

#include "models/ChatMessage.h"


class ChatModel : public QAbstractListModel
{
    Q_OBJECT
public:
    enum ChatModelRoles {
        NameRole = Qt::UserRole + 1,
        DateRole,
        HourRole,
        MessageRole,
        isHeadRole,
        isLastRole,
        OutgoingRole,
        GroupUIDRole,
        LocalUIDRole,
        RemoteUIDRole,
        RemoteNameRole,
        IconNameRole,
        EventIDRole
    };

    explicit ChatModel(QObject *parent = nullptr);
    void prependMessage(ChatMessage *message);
    void appendMessage(ChatMessage *message);

    int rowCount(const QModelIndex &parent = QModelIndex()) const;
    QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;

    QList<ChatMessage*> chats;

    // endless scroll exhausted
    Q_PROPERTY(bool exhausted READ is_exhausted NOTIFY exhaustedChanged);
    Q_PROPERTY(int limit READ limit NOTIFY limitChanged);
    Q_PROPERTY(int offset READ offset NOTIFY offsetChanged);
    int limit() const { return m_limit; }
    int offset() const { return m_offset; }
    bool is_exhausted() {
      return m_exhausted;
    }

    Q_INVOKABLE unsigned int getPage();
    unsigned int getMessages(const QString &remote_uid);
    unsigned int getMessages(const QString &remote_uid, int limit, int offset);
    void getOverviewMessages(int limit = 20, int offset = 0);

    void clear();

signals:
    void exhaustedChanged();
    void limitChanged();
    void offsetChanged();

protected:
    QHash<int, QByteArray> roleNames() const;

private:
    QString m_remote_uid;
    int m_page = 0;
    int m_limit = 4;
    int m_offset = 0;
    bool m_exhausted = false;
    gint m_rtcom_sms_service_id = 3;  // rtcom_el_get_service_id(el, "RTCOM_EL_SERVICE_SMS");
};

#endif
