#ifndef FOLDERLISTMODEL_H
#define FOLDERLISTMODEL_H

#include <QAbstractItemModel>
#include <QHash>
#include <QByteArray>
#include <QVector>
#include "services/accountservice.h"
#include "db/dao/folderdao.h"
#include "core/models/account.h"
#include "core/models/folder.h"

class FolderTreeItem
{
public:
    enum Type { AccountNode, FolderNode };

    explicit FolderTreeItem(Type type, QVariant data, FolderTreeItem *parent = nullptr);
    ~FolderTreeItem();

    void appendChild(FolderTreeItem *child);
    void clearChildren();
    FolderTreeItem *child(int row);
    int childCount() const;
    int row() const;
    FolderTreeItem *parentItem();
    Type type() const { return m_type; }
    QVariant data() const { return m_data; }

private:
    Type m_type;
    QVariant m_data;
    QVector<FolderTreeItem*> m_childItems;
    FolderTreeItem *m_parentItem;
};

class FolderListModel : public QAbstractItemModel
{
    Q_OBJECT
public:
    explicit FolderListModel(AccountService *accountService, QObject *parent = nullptr);
    ~FolderListModel() override;

    enum Roles {
        ItemTypeRole = Qt::UserRole + 1,
        AccountIdRole,
        FolderIdRole,
        NameRole,
        UnreadCountRole
    };

    QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override;
    QModelIndex parent(const QModelIndex &index) const override;
    int rowCount(const QModelIndex &parent = QModelIndex()) const override;
    int columnCount(const QModelIndex &parent = QModelIndex()) const override;
    QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
    Qt::ItemFlags flags(const QModelIndex &index) const override;
    QHash<int, QByteArray> roleNames() const override;

    void refresh();

private:
    void setupModelData();
    void clearModel();

    FolderTreeItem *m_rootItem;
    AccountService *m_accountService;
};

#endif // FOLDERLISTMODEL_H