package de.bmoth.app;

/*
 *Original Code from https://github.com/TomasMikula/RichTextFX/blob/master/richtextfx-demos/src/main/java/org/fxmisc/richtext/demo/JavaKeywords.java
 */

import org.fxmisc.richtext.model.StyleSpans;
import org.fxmisc.richtext.model.StyleSpansBuilder;

import java.util.Collection;
import java.util.Collections;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Highlighter {
    private Highlighter() {
    }

    private static final String[] START = new String[]{
        "MACHINE"
    };

    private static final String[] KEYWORDS = new String[]{
        "VARIABLES", "INVARIANT", "INITIALISATION", "OPERATIONS", "CONSTANTS", "PROPERTIES"
    };
    private static final String[] KEYWORDS2 = new String[]{
        "END", "SELECT", "THEN", "BEGIN", "WHERE", "ANY"
    };

    private static final String START_PATTERN = "\\b(" + String.join("|", START) + ")\\b";
    private static final String KEYWORD_PATTERN = "\\b(" + String.join("|", KEYWORDS) + ")\\b";
    private static final String KEYWORD2_PATTERN = "\\b(" + String.join("|", KEYWORDS2) + ")\\b";
    private static final String PAREN_PATTERN = "\\(|\\)";
    private static final String BRACE_PATTERN = "\\{|\\}";
    private static final String BRACKET_PATTERN = "\\[|\\]";
    private static final String SEMICOLON_PATTERN = "\\;";
    private static final String STRING_PATTERN = "\"([^\"\\\\]|\\\\.)*\"";
    private static final String COMMENT_PATTERN = "//[^\n]*" + "|" + "/\\*(.|\\R)*?\\*/";

    private static final Pattern PATTERN = Pattern.compile(
        "(?<KEYWORD>" + KEYWORD_PATTERN + ")"
            + "|(?<START>" + START_PATTERN + ")"
            + "|(?<KEYWORD2>" + KEYWORD2_PATTERN + ")"
            + "|(?<PAREN>" + PAREN_PATTERN + ")"
            + "|(?<BRACE>" + BRACE_PATTERN + ")"
            + "|(?<BRACKET>" + BRACKET_PATTERN + ")"
            + "|(?<SEMICOLON>" + SEMICOLON_PATTERN + ")"
            + "|(?<STRING>" + STRING_PATTERN + ")"
            + "|(?<COMMENT>" + COMMENT_PATTERN + ")"
    );

    protected static StyleSpans<Collection<String>> computeHighlighting(String text) {
        Matcher matcher = PATTERN.matcher(text);
        int lastKwEnd = 0;
        StyleSpansBuilder<Collection<String>> spansBuilder
            = new StyleSpansBuilder<>();
        while (matcher.find()) {
            String styleClass; /* never happens */
            if (matcher.group("START") != null) styleClass = "start";
            else if (matcher.group("KEYWORD") != null) styleClass = "keyword";
            else if (matcher.group("KEYWORD2") != null) styleClass = "keyword2";
            else if (matcher.group("PAREN") != null) styleClass = "paren";
            else if (matcher.group("BRACE") != null) styleClass = "brace";
            else if (matcher.group("BRACKET") != null) styleClass = "bracket";
            else if (matcher.group("SEMICOLON") != null) styleClass = "semicolon";
            else if (matcher.group("STRING") != null) styleClass = "string";
            else if (matcher.group("COMMENT") != null) styleClass = "comment";
            else styleClass = null;
            assert styleClass != null;
            spansBuilder.add(Collections.emptyList(), matcher.start() - lastKwEnd);
            spansBuilder.add(Collections.singleton(styleClass), matcher.end() - matcher.start());
            lastKwEnd = matcher.end();
        }
        spansBuilder.add(Collections.emptyList(), text.length() - lastKwEnd);
        return spansBuilder.create();
    }
}

@@ -2124,24 +2124,150 @@
 ing text) %7B%0A
+        String%5B%5D groups = new String%5B%5D%7B%22KEYWORD%22, %22KEYWORD2%22, %22PAREN%22, %22BRACE%22, %22BRACKET%22, %22SEMICOLON%22, %22STRING%22, %22COMMENT%22%7D;%0A
         Matc
@@ -2492,16 +2492,23 @@
 yleClass
+ = null
 ; /* nev
@@ -2537,226 +2537,42 @@
     
-if (matcher.group(%22START%22) != null) styleClass = %22start%22;%0A            else if (matcher.group(%22KEYWORD%22) != null) styleClass = %22keyword%22;%0A            else if (matcher.group(%22KEYWORD2%22) != null) styleClass = %22keyword2%22;%0A
+for (String group : groups) %7B%0A    
     
@@ -2571,37 +2571,32 @@
                 
-else 
 if (matcher.grou
@@ -2601,476 +2601,138 @@
 oup(
-%22PAREN%22) != null) styleClass = %22paren%22;%0A            else if (matcher.group(%22BRACE%22) != null) styleClass = %22brace%22;%0A            else if (matcher.group(%22BRACKET%22) != null) styleClass = %22bracket%22;%0A            else if (matcher.group(%22SEMICOLON%22) != null) styleClass = %22semicolon%22;%0A            else if (matcher.group(%22STRING%22) != null) styleClass = %22string%22;%0A            else if (matcher.group(%22COMMENT%22) != null) styleClass = %22comment%22;%0A            else styleClass = null;
+group) != null) %7B%0A                    styleClass = group.toLowerCase();%0A                    break;%0A                %7D%0A            %7D
 %0A   

refactor complicated ifs