#include "highlighter_shock.h"

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

    // Format for bg_shock_* keys
    keyFormat.setForeground(QColor("#569CD6"));
    keyFormat.setFontWeight(QFont::Bold);
    rule.pattern = QRegularExpression(QStringLiteral("\\bbg_shock_[\\w]+\\b"));
    rule.format = keyFormat;
    highlightingRules.append(rule);

    // Format for quoted string values
    stringFormat.setForeground(QColor("#CE9178"));
    rule.pattern = QRegularExpression(QStringLiteral("\"[^\"]*\""));
    rule.format = stringFormat;
    highlightingRules.append(rule);

    // Format for numeric values inside quotes (overridden by stringFormat but still useful for raw values)
    numberFormat.setForeground(QColor("#B5CEA8"));
    rule.pattern = QRegularExpression(QStringLiteral("-?\\d+(\\.\\d+)?"));
    rule.format = numberFormat;
    highlightingRules.append(rule);

    // Optional: Format for control groups (e.g., bg_shock_volume_*)
    subgroupFormat.setForeground(QColor("#4EC9B0"));
    rule.pattern = QRegularExpression(QStringLiteral("\\bbg_shock_volume_\\w+\\b"));
    rule.format = subgroupFormat;
    highlightingRules.append(rule);
}

void Highlighter_Shock::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);
}
