#ifndef PQSYNTAXHIGHLIGHTER_H
#define PQSYNTAXHIGHLIGHTER_H

#include <QSyntaxHighlighter>
#include "classes/pqwidget.h"
#include "classes/pqtextcharformat.h"
#include "classes/pqtextedit.h"
#include "classes/pqplaintextedit.h"

enum RuleType {
    Simple = 0,
    Extend
};

typedef struct _PQSHRule {
    RuleType type;
    QTextCharFormat format;
    QString startPattern;
    QRegExp startRx;
    QString endPattern;
    QRegExp endRx;
    int state;
} PQSHRule;


class PQSyntaxHighlighter;
class PQSyntaxHighlighter : public QSyntaxHighlighter {
    Q_OBJECT
    PQ_OBJECT_EX(QSyntaxHighlighter)

    Q_PROPERTY( int currentBlockState READ currentBlockState WRITE setCurrentBlockState )
    Q_PROPERTY( int previousBlockState READ previousBlockState )
    Q_PROPERTY( int currentBlockNumber READ currentBlockNumber )
    Q_PROPERTY( bool rulesEnabled READ rulesEnabled WRITE setRulesEnabled )
    Q_PROPERTY( bool rulesDisabled READ rulesDisabled WRITE setRulesDisabled )

public:
    Q_INVOKABLE explicit PQSyntaxHighlighter(QTextEdit *textEdit);
    Q_INVOKABLE explicit PQSyntaxHighlighter(QPlainTextEdit *textEdit);
    Q_INVOKABLE explicit PQSyntaxHighlighter(PQTextEdit *textEdit);
    Q_INVOKABLE explicit PQSyntaxHighlighter(PQPlainTextEdit *textEdit);
    Q_INVOKABLE explicit PQSyntaxHighlighter(QWidget *textEditObject);
    Q_INVOKABLE void setFormat(int start, int count, PQTextCharFormat *pqformat);
    Q_INVOKABLE void setFormat(int start, int count, QObject *pqformatObject);

    Q_INVOKABLE int currentBlockState();
    Q_INVOKABLE void setCurrentBlockState(int state);
    Q_INVOKABLE int previousBlockState();
    Q_INVOKABLE int currentBlockNumber();

    Q_INVOKABLE void addRule(const QString &pattern, PQTextCharFormat *pqformat);
    Q_INVOKABLE void addRule(const QString &startPattern, const QString &endPattern, int state, PQTextCharFormat *pqformat);

    Q_INVOKABLE bool rulesEnabled();
    Q_INVOKABLE void setRulesEnabled(bool enable);
    Q_INVOKABLE bool rulesDisabled();
    Q_INVOKABLE void setRulesDisabled(bool disable);

    Q_INVOKABLE void rehighlight();


    PQ_ON_SIGNAL_DECL_START {
    } PQ_ON_SIGNAL_DECL_END


Q_SIGNALS:
    void requireHighlightBlock(const QString &text);

private:
    bool m_rulesEnabled = true;
    QList<PQSHRule> m_rules;

protected:
    void highlightBlock(const QString &text);
};

#endif // PQSYNTAXHIGHLIGHTER_H
