#pragma once

#include <QSyntaxHighlighter>
#include <QTextCharFormat>
#include <QRegularExpression>
#include <QVector>

// ---------------------------------------------------------------------------
// SyntaxHighlighter – Regelbasierte Basis-Klasse.
// Unterklassen befüllen m_rules und können highlightBlock() überschreiben
// um mehrzeilige Konstrukte (Block-Kommentare, heredocs, …) zu behandeln.
// ---------------------------------------------------------------------------
class SyntaxHighlighter : public QSyntaxHighlighter
{
    Q_OBJECT

public:
    explicit SyntaxHighlighter(QTextDocument *parent = nullptr);

protected:
    struct HighlightRule
    {
        QRegularExpression pattern;
        QTextCharFormat    format;
    };

    void highlightBlock(const QString &text) override;

    // Unterklassen befüllen dies im Konstruktor
    QVector<HighlightRule> m_rules;

    // Mehrzeilige Block-Kommentare (/* ... */)
    QRegularExpression m_commentStartExpression;
    QRegularExpression m_commentEndExpression;
    QTextCharFormat    m_multiLineCommentFormat;
    bool               m_hasMultiLineComment = false;
};

// ---------------------------------------------------------------------------
// CppHighlighter – C und C++
// ---------------------------------------------------------------------------
class CppHighlighter : public SyntaxHighlighter
{
    Q_OBJECT
public:
    explicit CppHighlighter(QTextDocument *parent = nullptr);
protected:
    void highlightBlock(const QString &text) override;
};

// ---------------------------------------------------------------------------
// CssHighlighter – CSS
// ---------------------------------------------------------------------------
class CssHighlighter : public SyntaxHighlighter
{
    Q_OBJECT
public:
    explicit CssHighlighter(QTextDocument *parent = nullptr);
protected:
    void highlightBlock(const QString &text) override;
};

// ---------------------------------------------------------------------------
// HtmlHighlighter – HTML (mit eingebettetem CSS in <style> und JS in <script>)
// ---------------------------------------------------------------------------
class HtmlHighlighter : public SyntaxHighlighter
{
    Q_OBJECT
public:
    explicit HtmlHighlighter(QTextDocument *parent = nullptr);
protected:
    void highlightBlock(const QString &text) override;
};

// ---------------------------------------------------------------------------
// PhpHighlighter – PHP (HTML + eingebettetes PHP zwischen <?php ... ?>)
// ---------------------------------------------------------------------------
class PhpHighlighter : public SyntaxHighlighter
{
    Q_OBJECT
public:
    explicit PhpHighlighter(QTextDocument *parent = nullptr);
protected:
    void highlightBlock(const QString &text) override;

private:
    // Hilfsmethode: wendet PHP-Regeln auf einen Teilbereich an
    void highlightPhpRange(const QString &text, int start, int length);

    QVector<HighlightRule> m_phpRules;
    QTextCharFormat        m_phpStringFormat;
    QTextCharFormat        m_phpCommentFormat;
    QTextCharFormat        m_phpTagFormat;
};
