// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#pragma once

#include <projectexplorer/task.h>

#include <qmljs/qmljsdocument.h>
#include <qmljs/qmljsmodelmanagerinterface.h>

#include <QtTaskTree/QSingleTaskTreeRunner>

#include <QHash>
#include <QList>
#include <QObject>
#include <QTimer>

namespace QmlJSEditor::Internal {

class QmlTaskManager : public QObject
{
    Q_OBJECT
public:
    QmlTaskManager();

    void extensionsInitialized();

    void updateMessages();
    void updateSemanticMessagesNow();
    void documentsRemoved(const Utils::FilePaths &paths);

private:
    void updateMessagesNow(bool updateSemantic = false);

    void insertTask(const ProjectExplorer::Task &task);
    void removeTasksForFile(const Utils::FilePath &fileName);
    void removeAllTasks(bool clearSemantic);

private:
    class FileErrorMessages
    {
    public:
        Utils::FilePath fileName;
        ProjectExplorer::Tasks tasks;
    };
    static void collectMessages(QPromise<FileErrorMessages> &promise,
                                const QmlJS::Snapshot &snapshot,
                                const QList<QmlJS::ModelManagerInterface::ProjectInfo> &projectInfos,
                                const QmlJS::ViewerContext &vContext,
                                bool updateSemantic);

private:
    QHash<Utils::FilePath, ProjectExplorer::Tasks> m_docsWithTasks;
    QTimer m_updateDelay;
    bool m_updatingSemantic = false;
    QSingleTaskTreeRunner m_taskTreeRunner;
};

} // QmlJSEditor::Internal
