#pragma once

#include <QFileSystemModel>
#include <QObject>
#include <QMap>
#include <QSet>
#include <QString>
#include <QTimer>

class GitFileSystemModel : public QFileSystemModel {
    Q_OBJECT
    Q_PROPERTY(QVariant rootIndex READ getRootIndex NOTIFY rootIndexChanged)
public:
    enum ExtraRoles {
        GitStatusRole = Qt::UserRole + 100,
        IsFileRole = Qt::UserRole + 101,
        IsDirectoryRole = Qt::UserRole + 102,
        IsGitIgnoredRole = Qt::UserRole + 103
    };
    Q_ENUM(ExtraRoles)

    explicit GitFileSystemModel(QObject *parent = nullptr);

    QHash<int, QByteArray> roleNames() const override;
    QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;

    Q_INVOKABLE void refreshGitStatus();

    QVariant getRootIndex() const;

    QString findGitRepo(const QString &path) const;

signals:
    void rootIndexChanged();

protected:
    bool event(QEvent *event) override;

private:
    void onDirectoryLoaded(const QString &path);

private:
    QMap<QString, QString> _gitStatuses;
    QSet<QString> _gitIgnored;
    QTimer _refreshTimer;
    void updateGitStatus(const QString &repoPath);
    void updateGitIgnore(const QString &repoPath);
};
