#pragma once

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

class GitFileSystemModel : public QFileSystemModel {
    Q_OBJECT
    Q_PROPERTY(QVariant rootIndex READ getRootIndex CONSTANT)
public:
    enum ExtraRoles {
        GitStatusRole = Qt::UserRole + 100,
        IsFileRole = Qt::UserRole + 101,
        IsDirectoryRole = Qt::UserRole + 102
    };
    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;

protected:
    bool event(QEvent *event) override;

private:
    void onDirectoryLoaded(const QString &path);

private:
    QMap<QString, QString> _gitStatuses;
    QTimer _refreshTimer;
    void updateGitStatus(const QString &repoPath);
    QString findGitRepo(const QString &path) const;
};
