// Copyright (C) 2024 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#ifndef QQUICKGENERATOR_P_H
#define QQUICKGENERATOR_P_H

//
//  W A R N I N G
//  -------------
//
// This file is not part of the Qt API.  It exists purely as an
// implementation detail.  This header file may change from version to
// version without notice, or even be removed.
//
// We mean it.
//

#include <private/qquickvectorimageglobal_p.h>
#include <QtCore/qstring.h>
#include <QtCore/qloggingcategory.h>
#include <QtCore/qrect.h>

QT_BEGIN_NAMESPACE

QT_DECLARE_EXPORTED_QT_LOGGING_CATEGORY(lcQuickVectorImage, Q_QUICKVECTORIMAGEGENERATOR_EXPORT)

class QPainterPath;
class QGradient;
class QQuickShapePath;
class QQuadPath;
class QQuickItem;
class QQuickShape;
class QRectF;

namespace QQuickVectorImageGenerator {
struct NodeInfo;
struct ImageNodeInfo;
struct PathNodeInfo;
struct TextNodeInfo;
struct UseNodeInfo;
struct StructureNodeInfo;
struct AnimateColorNodeInfo;
struct MaskNodeInfo;
struct FilterNodeInfo;
struct MarkerNodeInfo;
struct PatternNodeInfo;
}

using namespace QQuickVectorImageGenerator;

class Q_QUICKVECTORIMAGEGENERATOR_EXPORT QQuickGenerator
{
public:
    QQuickGenerator(const QString fileName, QQuickVectorImageGenerator::GeneratorFlags flags);
    virtual ~QQuickGenerator();

    void setGeneratorFlags(QQuickVectorImageGenerator::GeneratorFlags flags);
    QQuickVectorImageGenerator::GeneratorFlags generatorFlags();

    bool generate();
    QQuickVectorImageGenerator::ErrorState errorState() const { return m_errorState; }

    virtual QString generateNodeBase(const NodeInfo &info, const QString &idSuffix = QString{}) = 0;
    virtual bool generateDefsNode(const StructureNodeInfo &info) = 0;
    virtual void generateDefsInstantiationNode(const StructureNodeInfo &info) = 0;
    virtual void generateImageNode(const ImageNodeInfo &info) = 0;
    virtual void generatePath(const PathNodeInfo &info, const QRectF &overrideBoundingRect = QRectF{}) = 0;
    virtual void generateNode(const NodeInfo &info) = 0;
    virtual void generateTextNode(const TextNodeInfo &info) = 0;
    virtual void generateUseNode(const UseNodeInfo &info) = 0;
    virtual void generateFilterNode(const FilterNodeInfo &info) = 0;
    virtual bool generateStructureNode(const StructureNodeInfo &info) = 0;
    virtual bool generateRootNode(const StructureNodeInfo &info) = 0;
    virtual bool generateMaskNode(const MaskNodeInfo &info) = 0;
    virtual bool generateMarkerNode(const MarkerNodeInfo &info) = 0;
    virtual bool generatePatternNode(const PatternNodeInfo &info) = 0;
    virtual void outputShapePath(const PathNodeInfo &info, const QPainterPath *path, const QQuadPath *quadPath, QQuickVectorImageGenerator::PathSelector pathSelector, const QRectF &boundingRect) = 0;
    void optimizePaths(const PathNodeInfo &info, const QRectF &overrideBoundingRect);
    bool isNodeVisible(const NodeInfo &info);

    QString fileName() const
    {
        return m_fileName;
    }

protected:
    void checkSanityLimit_helper(quint64 limit, QLatin1StringView limitObject);
    bool checkSanityLimit(quint64 value, quint64 limit, QLatin1StringView limitObject)
    {
        if (Q_LIKELY(!errorState())) {
            if (value <= limit || m_flags.testFlag(QQuickVectorImageGenerator::AssumeTrustedSource))
                return true;
            checkSanityLimit_helper(limit, limitObject);
        }
        return false;
    }

    QQuickVectorImageGenerator::GeneratorFlags m_flags;
    QQuickVectorImageGenerator::ErrorState m_errorState = QQuickVectorImageGenerator::NoError;


private:
    QString m_fileName;
};

QT_END_NAMESPACE

#endif // QQUICKGENERATOR_P_H
