package org.almibe.codearea;

import java.util.ArrayList;
import java.util.List;

import javafx.beans.property.BooleanProperty;
import javafx.beans.property.ReadOnlyBooleanProperty;
import javafx.beans.property.ReadOnlyBooleanWrapper;
import javafx.beans.property.StringProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.concurrent.Worker;
import javafx.scene.Parent;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import netscape.javascript.JSObject;

public class AceCodeArea implements CodeArea {
    private final WebView webView;
    private final WebEngine webEngine;
    private final ReadOnlyBooleanWrapper isInitializedProperty = new ReadOnlyBooleanWrapper(false);
    
    public AceCodeArea() {
        webView = new WebView();        
        webEngine = webView.getEngine();
    }

    @Override
    public void init() {
        //final String html = AceCodeArea.class.getResource("html/editor.html").toExternalForm();
        //final String html = AceCodeArea.class.getResource("html/codemirror-4.8/mode/groovy/index.html").toExternalForm();
        final String html = AceCodeArea.class.getResource("html/codemirror-4.8/demo/vim.html").toExternalForm();
        webEngine.load(html);                
        webEngine.getLoadWorker().stateProperty().addListener(new ChangeListener<Worker.State>() {
            @Override
            public void changed(ObservableValue<? extends Worker.State> observable, Worker.State oldValue, Worker.State newValue) {
                if(newValue == Worker.State.SUCCEEDED) {
                   isInitializedProperty.setValue(true);
                }
            }
        });
    }

    @Override
    public ReadOnlyBooleanProperty isInitializedProperty() {
        return isInitializedProperty.getReadOnlyProperty();
    }

    @Override
    public Parent getWidget() {
        return this.webView;
    }
    
    public JSObject fetchEditor() {
        Object editor = webEngine.executeScript("editor;");
        if(editor instanceof JSObject) {
            return (JSObject) editor;
        }
        throw new IllegalStateException("CodeArea not loaded.");
    }
    
    public JSObject fetchSession() {
        Object temp = webEngine.executeScript("editor.session;");
        if(temp instanceof JSObject) {
            return (JSObject) temp;
        }
        throw new IllegalStateException("CodeArea not loaded.");
    }
    
    public void setValue(String value) {
        fetchEditor().call("setValue", value);
    }
    
    public String getValue() {
        return (String) fetchEditor().call("getValue"); //TODO add check
    }
    
    public void setMode(String mode) {
        fetchSession().call("setMode", mode);
    }
    
    public String getMode() {
        return (String) fetchSession().eval("this.getMode().$id;");
    }

    @Override
    public StringProperty contentProperty() {
        throw new UnsupportedOperationException("contentProperty is not implemented");
    }

    @Override
    public BooleanProperty readOnlyProperty() {
        throw new UnsupportedOperationException("readOnly is not implemented");
    }

    @Override
    public StringProperty modeProperty() {
        throw new UnsupportedOperationException("modeProperty is not implemented");
    }

    @Override
    public StringProperty themeProperty() {
        throw new UnsupportedOperationException("themeProperty is not implemented");
    }
}
@@ -27,60 +27,8 @@
 a;%0A%0A
-import java.util.ArrayList;%0Aimport java.util.List;%0A%0A
 impo
@@ -225,93 +225,8 @@
 ty;%0A
-import javafx.beans.value.ChangeListener;%0Aimport javafx.beans.value.ObservableValue;%0A
 impo
@@ -1216,186 +1216,30 @@
 ner(
-new ChangeListener%3CWorker.State%3E() %7B%0A            @Override%0A            public void changed(ObservableValue%3C? extends Worker.State%3E observable, Worker.State oldValue, Worker.State
+(observable, oldValue,
  new
@@ -1241,30 +1241,30 @@
 , newValue) 
-%7B%0A    
+-%3E  %7B%0A
             
@@ -1319,20 +1319,16 @@
         
-    
 isInitia
@@ -1361,26 +1361,8 @@
 e);%0A
-                %7D%0A
     

clean up