package de.bmoth;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.control.SeparatorMenuItem;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import org.fxmisc.flowless.VirtualizedScrollPane;
import org.fxmisc.richtext.CodeArea;
import org.fxmisc.richtext.LineNumberFactory;
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;

public class App extends Application {
	@Override
	public void start(Stage primaryStage) {
        MenuBar menuBar = new MenuBar();
        Menu menuFile = new Menu("File");
        Menu menuCheck = new Menu("Checks");

        MenuItem open = new MenuItem("Open");
        open.setOnAction(new EventHandler<ActionEvent>() {
            public void handle(ActionEvent t) {
                openFile();
            }
        });

        MenuItem exit = new MenuItem("Exit");
        exit.setOnAction(new EventHandler<ActionEvent>() {
            public void handle(ActionEvent t) {
                System.exit(0);
            }
        });

        menuFile.getItems().addAll(open,new SeparatorMenuItem(),exit);
        menuBar.getMenus().addAll(menuFile, menuCheck);

        CodeArea codeArea = new CodeArea();
		codeArea.setParagraphGraphicFactory(LineNumberFactory.get(codeArea));

		codeArea.richChanges()
				.filter(ch -> !ch.getInserted().equals(ch.getRemoved())) // XXX
				.subscribe(change -> {
					codeArea.setStyleSpans(0, computeHighlighting(codeArea.getText()));
				});

		Scene scene = new Scene(new VBox(),800,600);

        ((VBox) scene.getRoot()).getChildren().addAll(menuBar,codeArea);

        primaryStage.setScene(scene);
		primaryStage.setTitle("BMoth");
		primaryStage.show();
	}

    private static StyleSpans<Collection<String>> computeHighlighting(String text) {
        StyleSpansBuilder<Collection<String>> spansBuilder
                = new StyleSpansBuilder<>();
        spansBuilder.add(Collections.emptyList(), text.length());
        return spansBuilder.create();
    }

    private static void openFile() {

    }
}

@@ -12,16 +12,257 @@
 bmoth;%0A%0A
+import java.util.Collection;%0Aimport java.util.Collections;%0A%0Aimport org.fxmisc.richtext.CodeArea;%0Aimport org.fxmisc.richtext.LineNumberFactory;%0Aimport org.fxmisc.richtext.model.StyleSpans;%0Aimport org.fxmisc.richtext.model.StyleSpansBuilder;%0A%0A
 import j
@@ -546,46 +546,8 @@
 em;%0A
-import javafx.scene.layout.StackPane;%0A
 impo
@@ -605,330 +605,8 @@
 age;
-%0Aimport org.fxmisc.flowless.VirtualizedScrollPane;%0Aimport org.fxmisc.richtext.CodeArea;%0Aimport org.fxmisc.richtext.LineNumberFactory;%0Aimport org.fxmisc.richtext.model.StyleSpans;%0Aimport org.fxmisc.richtext.model.StyleSpansBuilder;%0A%0Aimport java.util.Collection;%0Aimport java.util.Collections;%0Aimport java.util.regex.Matcher;
 %0A%0Apu
@@ -642,17 +642,20 @@
 ation %7B%0A
-%09
+    
 @Overrid
@@ -656,17 +656,20 @@
 verride%0A
-%09
+    
 public v
@@ -1289,16 +1289,17 @@
 ll(open,
+ 
 new Sepa
@@ -1314,16 +1314,17 @@
 uItem(),
+ 
 exit);%0A 
@@ -1423,18 +1423,24 @@
 Area();%0A
-%09%09
+        
 codeArea
@@ -1502,18 +1502,24 @@
 rea));%0A%0A
-%09%09
+        
 codeArea
@@ -1532,21 +1532,16 @@
 hanges()
-%0A%09%09%09%09
 .filter(
@@ -1596,20 +1596,32 @@
  // XXX%0A
-%09%09%09%09
+                
 .subscri
@@ -1639,13 +1639,28 @@
 %3E %7B%0A
-%09%09%09%09%09
+                    
 code
@@ -1727,19 +1727,37 @@
 ));%0A
-%09%09%09%09%7D);%0A%0A%09%09
+                %7D);%0A%0A        
 Scen
@@ -1791,12 +1791,14 @@
 x(),
+ 
 800,
+ 
 600)
@@ -1862,16 +1862,17 @@
 menuBar,
+ 
 codeArea
@@ -1913,18 +1913,24 @@
 scene);%0A
-%09%09
+        
 primaryS
@@ -1957,10 +1957,16 @@
 %22);%0A
-%09%09
+        
 prim
@@ -1986,9 +1986,12 @@
 ();%0A
-%09
+    
 %7D%0A%0A 
@@ -2132,32 +2132,16 @@
 sBuilder
-%0A               
  = new S

remove unused imports