#include "highlighter_rumble.h"

Highlighter_Rumble::Highlighter_Rumble(QTextDocument *parent)
    : QSyntaxHighlighter(parent) {
    HighlightingRule rule;

    // Format for the "RUMBLEGRAPHFILE" header
    headerFormat.setForeground(QColor("#569CD6"));
    headerFormat.setFontWeight(QFont::Bold);
    rule.pattern = QRegularExpression(QStringLiteral("^RUMBLEGRAPHFILE\\b"));
    rule.format = headerFormat;
    highlightingRules.append(rule);

    // Format for line count (second line, typically a single integer)
    countFormat.setForeground(QColor("#C586C0"));
    rule.pattern = QRegularExpression(QStringLiteral("^\\d+$"));
    rule.format = countFormat;
    highlightingRules.append(rule);

    // Format for floating-point number pairs (e.g. 0.0000 0.4701)
    floatPairFormat.setForeground(QColor("#B5CEA8"));
    rule.pattern = QRegularExpression(QStringLiteral("^\\s*-?\\d+\\.\\d+\\s+-?\\d+\\.\\d+\\s*$"));
    rule.format = floatPairFormat;
    highlightingRules.append(rule);
}

void Highlighter_Rumble::highlightBlock(const QString &text) {
    for (const HighlightingRule &rule : std::as_const(highlightingRules)) {
        QRegularExpressionMatchIterator matchIterator = rule.pattern.globalMatch(text);
        while (matchIterator.hasNext()) {
            QRegularExpressionMatch match = matchIterator.next();
            setFormat(match.capturedStart(), match.capturedLength(), rule.format);
        }
    }

    setCurrentBlockState(0);
}
