package edu.chl.proton.control;

import edu.chl.proton.model.IDocumentHandler;
import edu.chl.proton.model.IFileHandler;
import edu.chl.proton.model.WorkspaceFactory;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.Node;
import javafx.scene.web.HTMLEditor;

import java.io.IOException;


/**
 * @author Anton Levholm
 * Created by antonlevholm on 2017-05-01.
 */
public class MarkdownTabController {
    private static IFileHandler file;
    private static IDocumentHandler document;

    @FXML
    HTMLEditor htmlEditor;

    public void initialize() {
        WorkspaceFactory factory = new WorkspaceFactory();
        file = factory.getWorkspace();
        document = factory.getWorkspace();
        hideHTMLEditorToolbars(htmlEditor);
    }

    // Found at http://stackoverflow.com/questions/10075841/how-to-hide-the-controls-of-htmleditor
    public static void hideHTMLEditorToolbars(final HTMLEditor editor)
    {
        editor.setVisible(false);
        Platform.runLater(() -> {
            Node[] nodes = editor.lookupAll(".tool-bar").toArray(new Node[0]);
            for(Node node : nodes)
            {
                node.setVisible(false);
                node.setManaged(false);
            }
            editor.setVisible(true);
        });
    }

    public static void setTextFlow() {
        //List<Text> text = document.getText();
    }

    @FXML
    public void onClickLinkButton(ActionEvent event) throws IOException {

    }

    @FXML
    public void onClickHeadingButton(ActionEvent event) throws IOException {

    }

    @FXML
    public void onClickBoldButton(ActionEvent event) throws IOException {

    }

    @FXML
    public void onClickItalicButton(ActionEvent event) throws IOException {

    }

    @FXML
    public void onClickQuoteButton(ActionEvent event) throws IOException {

    }

    @FXML
    public void onClickImageButton(ActionEvent event) throws IOException {

    }

    @FXML
    public void onClickCodeButton(ActionEvent event) throws IOException {

    }

    @FXML
    public void onClickOrderedListButton(ActionEvent event) throws IOException {

    }

    @FXML
    public void onClickUnorderedListButton(ActionEvent event) throws IOException {

    }

    @FXML
    public void onClickHorizontalLineButton(ActionEvent event) throws IOException {

    }

}

@@ -229,16 +229,50 @@
 nEvent;%0A
+import javafx.event.EventHandler;%0A
 import j
@@ -314,16 +314,87 @@
 e.Node;%0A
+import javafx.scene.input.KeyCode;%0Aimport javafx.scene.input.KeyEvent;%0A
 import j
@@ -450,16 +450,80 @@
 eption;%0A
+import java.util.regex.Matcher;%0Aimport java.util.regex.Pattern;%0A
 %0A%0A/**%0A *
@@ -966,16 +966,1675 @@
 Editor);
+%0A%0A        htmlEditor.setOnKeyReleased(new EventHandler%3CKeyEvent%3E()%0A        %7B%0A            @Override%0A            public void handle(KeyEvent event)%0A            %7B%0A                if (isValidEvent(event))%0A                %7B%0A                    String text = htmlEditor.getHtmlText();%0A                    System.out.println(text);%0A                    text = stripHTMLTags(text);%0A                    int index = text.length()-1;%0A                    System.out.println(%22Index: %22 + index);%0A                    System.out.println(text.charAt(index));%0A                    //document.insertPart(htmlEditor.getHtmlText());%0A                %7D%0A            %7D%0A%0A            private boolean isValidEvent(KeyEvent event)%0A            %7B%0A                return !isSelectAllEvent(event)%0A                        && ((isPasteEvent(event)) %7C%7C isCharacterKeyReleased(event));%0A            %7D%0A%0A            private boolean isSelectAllEvent(KeyEvent event)%0A            %7B%0A                return event.isShortcutDown() && event.getCode() == KeyCode.A;%0A            %7D%0A%0A            private boolean isPasteEvent(KeyEvent event)%0A            %7B%0A                return event.isShortcutDown() && event.getCode() == KeyCode.V;%0A            %7D%0A%0A            private boolean isCharacterKeyReleased(KeyEvent event)%0A            %7B%0A                // Make custom changes here..%0A                switch (event.getCode())%0A                %7B%0A                    case ALT:%0A                    case COMMAND:%0A                    case CONTROL:%0A                    case SHIFT:%0A                        return false;%0A                    default:%0A                        return true;%0A                %7D%0A            %7D%0A        %7D);
 %0A    %7D%0A%0A
@@ -3164,86 +3164,386 @@
    p
-ublic static void setTextFlow() %7B%0A        //List%3CText%3E text = document.getText
+rivate String stripHTMLTags(String htmlText) %7B%0A        Pattern pattern = Pattern.compile(%22%3C%5B%5E%3E%5D*%3E%22);%0A        Matcher matcher = pattern.matcher(htmlText);%0A        final StringBuffer sb = new StringBuffer(htmlText.length());%0A        while(matcher.find()) %7B%0A            matcher.appendReplacement(sb, %22 %22);%0A        %7D%0A        matcher.appendTail(sb);%0A        return sb.toString().trim
 ();%0A
@@ -3541,32 +3541,33 @@
 .trim();%0A    %7D%0A%0A
+%0A
     @FXML%0A    pu

print text from HTMLEditor on KeyEvent