package com.kodcu.service.ui;

import com.kodcu.component.MenuItemBuilt;
import com.kodcu.controller.ApplicationController;
import com.kodcu.other.Current;
import com.kodcu.service.*;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.MenuItem;
import javafx.scene.input.Dragboard;
import javafx.scene.input.MouseButton;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.filefilter.TrueFileFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;

/**
 * Created by usta on 25.12.2014.
 */
@Component
public class WebviewService {

    @Autowired
    private ApplicationController controller;

    @Autowired
    private PathResolverService pathResolver;

    @Autowired
    private ThreadService threadService;

    @Autowired
    private ParserService parserService;

    @Autowired
    private MarkdownService markdownService;

    @Autowired
    private Current current;

    @Autowired
    private DocumentService documentService;

    public WebView createWebView() {

        WebView webView = new WebView();
        WebEngine webEngine = webView.getEngine();
        webEngine.load(String.format("http://localhost:%d/editor.html", controller.getPort()));
        webView.setContextMenuEnabled(false);
        ContextMenu menu = new ContextMenu();

        webView.setOnMouseClicked(event -> {

            MenuItem copy = MenuItemBuilt.item("Copy").onclick(event1 -> {
                controller.cutCopy(current.currentEditorSelection());
            });
            MenuItem paste = MenuItemBuilt.item("Paste").onclick(e -> {
                controller.paste(false);
            });
            MenuItem pasteRaw = MenuItemBuilt.item("Paste raw").onclick(e -> {
                current.insertEditorValue(controller.pasteRaw());
            });
            MenuItem convert = MenuItemBuilt.item("Markdown to Asciidoc").onclick(e -> {
                markdownService.convertToAsciidoc(current.currentEditorValue(), content -> {
                    threadService.runActionLater(() -> {
                        documentService.newDoc(content);
                    });
            });
            });

            if (menu.getItems().size() == 0) {

                menu.getItems().addAll(copy, paste, pasteRaw, convert);
            }

            if (menu.isShowing()) {
                menu.hide();
            }
            if (event.getButton() == MouseButton.SECONDARY) {
                convert.setVisible(current.currentTab().isAsciidoc());
                menu.show(webView, event.getScreenX(), event.getScreenY());
            }
        });


        webView.setOnDragDropped(event -> {
            Dragboard dragboard = event.getDragboard();
            boolean success = false;

            if (dragboard.hasFiles()) {

                List<File> dragboardFiles = dragboard.getFiles();

                if (dragboardFiles.size() == 1) {
                    Path path = dragboardFiles.get(0).toPath();
                    if (Files.isDirectory(path)) {

                        Iterator<File> files = FileUtils.iterateFilesAndDirs(path.toFile(), TrueFileFilter.INSTANCE, TrueFileFilter.INSTANCE);

                        StringBuffer buffer = new StringBuffer();
                        buffer.append("[tree,file=\"\"]");
                        buffer.append("\n--\n");
                        buffer.append("#" + path.getFileName().toString());

                        while (files.hasNext()) {
                            File next = files.next();

                            Path relativize = path.relativize(next.toPath());

                            Path path1 = relativize.getName(0);
                            if ("".equals(path1.toString()) || pathResolver.isHidden(path1))
                                continue;

                            String hash = String.join("", Collections.nCopies(relativize.getNameCount() + 1, "#"));

                            buffer.append("\n");
                            buffer.append(hash);
                            buffer.append(relativize.getFileName().toString());

                        }
                        buffer.append("\n--");
                        current.insertEditorValue(buffer.toString());
                        success = true;
                    }
                }

                Optional<String> block = parserService.toImageBlock(dragboardFiles);
                if (block.isPresent()) {
                    current.insertEditorValue(block.get());
                    success = true;
                } else {
                    block = parserService.toIncludeBlock(dragboardFiles);
                    if (block.isPresent()) {
                        current.insertEditorValue(block.get());
                        success = true;
                    }
                }

            }

            if (dragboard.hasHtml() && !success) {
                Optional<String> block = parserService.toWebImageBlock(dragboard.getHtml());
                if (block.isPresent()) {
                    current.insertEditorValue(block.get());
                    success = true;
                }
            }

            if (dragboard.hasString() && !success) {
                current.insertEditorValue(dragboard.getString());
                success = true;
            }

            event.setDropCompleted(success);
            event.consume();
        });


        return webView;
    }
}

@@ -1605,58 +1605,8 @@
 );%0A%0A
-        webView.setOnMouseClicked(event -%3E %7B%0A%0A    
     
@@ -1676,36 +1676,32 @@
 %3E %7B%0A            
-    
 controller.cutCo
@@ -1742,36 +1742,28 @@
 ));%0A        
-   
- 
 %7D);%0A
-    
         Menu
@@ -1810,36 +1810,32 @@
 .onclick(e -%3E %7B%0A
-    
             cont
@@ -1859,36 +1859,28 @@
 e);%0A        
-    %7D);%0A    
+%7D);%0A
         Menu
@@ -1946,36 +1946,32 @@
 %3E %7B%0A            
- 
-   
 current.insertEd
@@ -2004,36 +2004,32 @@
 Raw());%0A        
-    
 %7D);%0A            
@@ -2020,28 +2020,24 @@
 %7D);%0A        
-   
- 
 MenuItem con
@@ -2093,36 +2093,32 @@
 .onclick(e -%3E %7B%0A
-    
             mark
@@ -2182,36 +2182,32 @@
 ), content -%3E %7B%0A
-    
                 
@@ -2263,20 +2263,16 @@
         
-    
 document
@@ -2304,36 +2304,32 @@
                 
-    
 %7D);%0A            
@@ -2340,23 +2340,65 @@
         
-    %7D);
+%7D);%0A%0A        webView.setOnMouseClicked(event -%3E %7B
 %0A%0A      
@@ -2434,25 +2434,24 @@
 e() == 0) %7B%0A
-%0A
             
@@ -2686,27 +2686,27 @@
     
-convert.setVisible(
+boolean asciidoc = 
 curr
@@ -2734,16 +2734,61 @@
 ciidoc()
+;%0A                convert.setVisible(asciidoc
 );%0A     

Set visibility for Markdown to Asciidoc