#include "text_lighter.h"
#include <QtWidgets>

Lighter_class::Lighter_class(QTextDocument *parent)
    : QSyntaxHighlighter(parent),
      f_isRightExt(false)
{}

Lighter_class::Lighter_class(QTextDocument *parent, const QString& ext)
    : QSyntaxHighlighter(parent),
      f_currentExt(ext),
      f_isRightExt(false)
{
    f_multiLineCommentFormat.setForeground(Qt::red);

    f_commentStartExpression = QRegularExpression(QStringLiteral("/\\*"));
    f_commentEndExpression = QRegularExpression(QStringLiteral("\\*/"));

}


void Lighter_class::highlightBlock(const QString &text)
{
    qDebug() << text;
    for (const HighlightingRule &rule : qAsConst(f_highlightingRules)) {
        QRegularExpressionMatchIterator matchIterator = rule.f_pattern.globalMatch(text);
        while (matchIterator.hasNext()) {
            QRegularExpressionMatch match = matchIterator.next();
            setFormat(match.capturedStart(), match.capturedLength(), rule.f_format);
        }
    }
    setCurrentBlockState(0);

    int startIndex = 0;
       if (previousBlockState() != 1)
           startIndex = text.indexOf(f_commentStartExpression);

       while (startIndex >= 0) {
               QRegularExpressionMatch match = f_commentEndExpression.match(text, startIndex);
               int endIndex = match.capturedStart();
               int commentLength = 0;
               if (endIndex == -1) {
                   setCurrentBlockState(1);
                   commentLength = text.length() - startIndex;
               } else {
                   commentLength = endIndex - startIndex
                                   + match.capturedLength();
               }
               setFormat(startIndex, commentLength, f_multiLineCommentFormat);
               startIndex = text.indexOf(f_commentStartExpression, startIndex + commentLength);
           }
}

bool Lighter_class::startElement(const QString&, const QString &, const QString& tagName, const QXmlAttributes& attrs)
{
    qDebug() << tagName << attrs.count();
    if (tagName == "syntax")
    {
        qDebug() << "tagName == syntax";
        for(int i = 0; i < attrs.count(); i++)
        {
            if(attrs.localName(i) == "ext_list")
            {
                QStringList extList = attrs.value(i).split(" ");
                if(extList.contains(f_currentExt))
                    f_isRightExt = true;
                else
                    f_isRightExt = false;

                qDebug() << "ext_list" << attrs.value(i);
            }
        }
    }
    else if (tagName == "pattern")
    {
        if(f_isRightExt)
        {
            qDebug() << "pattern";
            f_pattern = QRegularExpression(attrs.value(0));
            qDebug() << "read regExp = " << f_pattern;
        }
    }
    else if (tagName == "format")
    {
        if(f_isRightExt)
        {
            qDebug() << "format";
            f_color = attrs.value(0);
            f_fontWeight = attrs.value(1).toInt();
            qDebug() << "read fontWeight = " << f_fontWeight;
        }
    }

    return true;
}

bool Lighter_class::characters(const QString& strText)
{
    f_currentText = strText;
    return true;
}

bool Lighter_class::endElement(const QString &, const QString&, const QString &tagName)
{
    qDebug() << "In end element with tagName = " + tagName;
    if (tagName == "rule")
    {
        if(f_isRightExt)
        {
            HighlightingRule rule;

            rule.f_pattern = f_pattern;
            QTextCharFormat currFormat;
            currFormat.setForeground(QColor(f_color));
            currFormat.setFontWeight(f_fontWeight);
            rule.f_format = currFormat;
            f_highlightingRules.append(rule);
        }

    }
    qDebug() << "MOST USEFUL QDEBUG";
}

bool Lighter_class::fatalError(const QXmlParseException& exception)
{
    qDebug() << "Line:" << exception.lineNumber()
             << ", Column:" << exception.columnNumber()
             << ", Message:" << exception.message();
    return false;
}










