package com.kodcu.component;

import com.kodcu.controller.ApplicationController;
import com.kodcu.other.Current;
import com.kodcu.other.IOHelper;
import com.kodcu.service.ThreadService;
import javafx.application.Platform;
import javafx.beans.property.ReadOnlyObjectProperty;
import javafx.concurrent.Worker;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import netscape.javascript.JSObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Stream;

/**
 * Created by usta on 09.04.2015.
 *
 * NOTE : getWebEngine i diğaerlerinde de böyle yap
 * *********************************************************
 */
@Component
public class LiveReloadPane extends ViewPanel {

    @Autowired
    public LiveReloadPane(ThreadService threadService, ApplicationController controller, Current current) {
        super(threadService,controller,current);
        this.webView.setContextMenuEnabled(true);
    }

    @Override
    public void browse() {
        controller.getHostServices()
                .showDocument(String.format("http://localhost:%d/livereload/index.reload", controller.getPort()));
    }

    public void initializeDiffReplacer() {
        threadService.runActionLater(()->{
            String diffhtml = IOHelper.readFile(LiveReloadPane.class.getResourceAsStream("/public/diffhtml.js"));
            String extension = IOHelper.readFile(LiveReloadPane.class.getResourceAsStream("/public/diffhtml-extension.js"));
            webEngine().executeScript(diffhtml);
            webEngine().executeScript(extension);
        });
    }

    public void updateDomdom() {
        threadService.runActionLater(()->{
            getWindow().call("updateDomdom", current.currentEditorValue());
        });
    }
}

@@ -922,17 +922,21 @@
 2015.%0A *
+ %3Cp%3E
 %0A
-
  * NOTE 
@@ -1255,16 +1255,17 @@
 Service,
+ 
 controll
@@ -1267,16 +1267,17 @@
 troller,
+ 
 current)
@@ -1447,89 +1447,32 @@
 ent(
-String.format(%22http://localhost:%25d/livereload/index.reload%22, controller.getPort()
+webEngine().getLocation(
 ));%0A
@@ -1542,38 +1542,36 @@
 dService.run
-Action
+Task
 Later(()
 -%3E%7B%0A        
@@ -1550,34 +1550,36 @@
 .runTaskLater(()
+ 
 -%3E
+ 
 %7B%0A            St
@@ -1795,32 +1795,85 @@
 xtension.js%22));%0A
+            threadService.runActionLater(() -%3E %7B%0A    
             webE
@@ -1909,32 +1909,36 @@
 l);%0A            
+  
+  
 webEngine().exec
@@ -1959,16 +1959,32 @@
 nsion);%0A
+            %7D);%0A
         
@@ -2066,18 +2066,20 @@
 Later(()
+ 
 -%3E
+ 
 %7B%0A      

Update LiveReloadPane