#include "keywordshighlighter.h"
#include <QDebug>

using namespace Parse;
using namespace Enhancement;

KeywordsHighlighter::KeywordsHighlighter(QObject *parent)
    : QSyntaxHighlighter(parent), parse_core(nullptr)
{

}

void KeywordsHighlighter::reset(Parse::Result::DocCore *base)
{
    this->parse_core = base;
}

void KeywordsHighlighter::highlightBlock(const QString &text)
{
    if(parse_core == nullptr)
        return;

    QTextCharFormat unknowns;
    unknowns.setForeground(QBrush(Qt::gray));

    QTextCharFormat generate;
    generate.setForeground(QBrush(Qt::blue));

    QTextCharFormat error;
    error.setForeground(QBrush(Qt::red));

    auto block = currentBlock();
    auto words = parse_core->getWords(block.blockNumber());
    for(auto &w : words){
        QList<ErrorMessage> xerrors;

        if(w->host() == parse_core->unknowns()){
            unknowns.setUnderlineStyle(QTextCharFormat::UnderlineStyle::WaveUnderline);
            unknowns.setUnderlineColor(QColor(Qt::red));

            setFormat(w->column()-1, w->toString().length(), unknowns);
        }
        else if(!w->host()->check(xerrors)){
            setFormat(w->column()-1, w->toString().length(), error);
        }
        else{
            setFormat(w->column()-1, w->toString().length(), generate);
        }
    }
}
