// SPDX-FileCopyrightText: 2025 Prayag Jain <prayagjain2@gmail.com>
//
// SPDX-License-Identifier: GPL-3.0-or-later

#pragma once

#include <QRect>
#include <QTimer>

class Canvas;
class ApplicationContext;
class CacheGrid;
class ItemCache;

class RenderingContext : public QObject
{
    Q_OBJECT
public:
    explicit RenderingContext(ApplicationContext *context);
    ~RenderingContext() override;

    [[nodiscard]] Canvas *canvas() const;
    CacheGrid &cacheGrid() const;
    ItemCache &itemCache() const;

    void markForRender();
    void markForUpdate();
    void markForUpdate(const QRect &region);

    [[nodiscard]] qreal zoomFactor() const;
    void setZoomFactor(qreal newValue);
    void updateZoomFactor(qreal newValue, QPoint center = {-1, -1});
    void zoomIn();
    void zoomOut();
    [[nodiscard]] bool canZoomIn() const;
    [[nodiscard]] bool canZoomOut() const;

    [[nodiscard]] int fps() const;

    void setCanvasBackground(const QColor &color);
    void reset();

Q_SIGNALS:
    void zoomFactorChanged(qreal newZoomFactor);
    void canvasRendered();
    void canvasUpdated();

private:
    void canvasResized();
    Canvas *m_canvas{nullptr};

    QTimer m_frameTimer;

    bool m_needsReRender{false};
    bool m_needsUpdate{false};
    QRect m_updateRegion{};

    qreal m_zoomFactor{1};

    std::unique_ptr<CacheGrid> m_cacheGrid{nullptr};
    std::unique_ptr<ItemCache> m_itemCache{nullptr};
    ApplicationContext *const m_applicationContext;
};
