package com.kodcu.component;

import javafx.collections.ObservableList;
import javafx.scene.control.*;
import javafx.scene.layout.VBox;

import java.nio.file.Path;
import java.util.List;
import java.util.Optional;

import static javafx.scene.control.Alert.AlertType;

/**
 * Created by usta on 06.03.2015.
 */
public final class AlertHelper {

    public static final ButtonType LOAD_FILE_SYSTEM_CHANGES = new ButtonType("Load File System Changes");
    public static final ButtonType KEEP_MEMORY_CHANGES = new ButtonType("Keep Memory Changes");

    public static final ButtonType OPEN_IN_APP = new ButtonType("Open anyway");
    public static final ButtonType OPEN_EXTERNAL = new ButtonType("Open external");

    public static Optional<ButtonType> deleteAlert(List<Path> pathsLabel) {
        Alert deleteAlert = new Alert(Alert.AlertType.WARNING, null, ButtonType.YES, ButtonType.CANCEL);
        deleteAlert.setHeaderText("Do you want to delete selected path(s)?");
        DialogPane dialogPane = deleteAlert.getDialogPane();

        ListView listView = new ListView();
        listView.getStyleClass().clear();
        ObservableList items = listView.getItems();
        items.addAll(pathsLabel);
        listView.setEditable(false);
        dialogPane.setContent(listView);

        listView.setPrefHeight(Optional.ofNullable(pathsLabel)
                .map(List::size)
                .map(e -> e * 40)
                .filter(e -> e <= 300 && e >= 40)
                .orElse(300));

        return deleteAlert.showAndWait();
    }

    public static Optional<ButtonType> showAlert(String alertMessage) {
        AlertDialog deleteAlert = new AlertDialog(AlertType.WARNING, null, ButtonType.YES, ButtonType.CANCEL);
        deleteAlert.setHeaderText(alertMessage);
        return deleteAlert.showAndWait();
    }

    public static void okayAlert(String alertMessage) {
        AlertDialog deleteAlert = new AlertDialog(AlertType.WARNING, null, ButtonType.OK);
        deleteAlert.setHeaderText(alertMessage);
        deleteAlert.show();
    }

    public static Optional<ButtonType> nullDirectoryAlert() {
        AlertDialog deleteAlert = new AlertDialog(AlertType.WARNING, null, ButtonType.OK);
        deleteAlert.setHeaderText("Please select directorie(s)");
        return deleteAlert.showAndWait();
    }

    public static Optional<ButtonType> notImplementedDialog() {
        AlertDialog alert = new AlertDialog(AlertType.WARNING, null, ButtonType.OK);
        alert.setHeaderText("This feature is not available for Markdown.");
        return alert.showAndWait();
    }

    public static Optional<ButtonType> saveAlert() {
        AlertDialog saveAlert = new AlertDialog();
        saveAlert.setHeaderText("This document is not saved. Do you want to close it?");
        return saveAlert.showAndWait();
    }

    public static Optional<ButtonType> conflictAlert(Path path) {
        Alert alert = new Alert(Alert.AlertType.WARNING);
        alert.setTitle("File Cache Conflict");
        alert.setHeaderText(String.format("Changes have been made to '%s' in memory and on disk", path));
        alert.getButtonTypes().clear();
        alert.getButtonTypes().addAll(LOAD_FILE_SYSTEM_CHANGES, KEEP_MEMORY_CHANGES, ButtonType.CANCEL);
        return alert.showAndWait();
    }

    public static Optional<ButtonType> sizeHangAlert(Path path, int hangFileSizeLimit) {
        Alert alert = new Alert(Alert.AlertType.WARNING);
        alert.setTitle(String.format("File size > %dMB", hangFileSizeLimit));
        alert.setHeaderText(String.format("It may cause application being unresponsive", path));
        alert.getButtonTypes().clear();
        alert.getButtonTypes().addAll(OPEN_IN_APP, OPEN_EXTERNAL, ButtonType.CANCEL);
        return alert.showAndWait();
    }

    public static Optional<ButtonType> nosizeAlert(Path path, int hangFileSizeLimit) {
        Alert alert = new Alert(Alert.AlertType.WARNING);
        alert.setTitle("No file size");
        alert.setHeaderText(String.format("It may cause application being unresponsive if it's real size > %dMB", path, hangFileSizeLimit));
        alert.getButtonTypes().clear();
        alert.getButtonTypes().addAll(OPEN_IN_APP, OPEN_EXTERNAL, ButtonType.CANCEL);
        return alert.showAndWait();
    }

    public static void showDuplicateWarning(List<String> duplicatePaths, Path lib) {
        Alert alert = new Alert(Alert.AlertType.WARNING);

        DialogPane dialogPane = alert.getDialogPane();

        ListView listView = new ListView();
        listView.getStyleClass().clear();
        ObservableList items = listView.getItems();
        items.addAll(duplicatePaths);
        listView.setEditable(false);

        dialogPane.setContent(listView);

        alert.setTitle("Duplicate JARs found");
        alert.setHeaderText(String.format("Duplicate JARs found, it may cause unexpected behaviours.\n\n" +
                "Please remove the older versions from these pair(s) manually. \n" +
                "JAR files are located at %s directory.", lib));
        alert.getButtonTypes().clear();
        alert.getButtonTypes().addAll(ButtonType.OK);
        alert.showAndWait();
    }
}

@@ -23,16 +23,52 @@
 onent;%0A%0A
+import javafx.application.Platform;%0A
 import j
@@ -1305,32 +1305,68 @@
 ent(listView);%0A%0A
+        Platform.runLater(()-%3E%7B%0A    
         listView
@@ -1424,24 +1424,28 @@
             
+    
 .map(List::s
@@ -1449,16 +1449,20 @@
 ::size)%0A
+    
         
@@ -1483,24 +1483,28 @@
  -%3E e * 40)%0A
+    
             
@@ -1557,16 +1557,20 @@
         
+    
 .orElse(
@@ -1575,16 +1575,60 @@
 e(300));
+%0A            listView.refresh();%0A        %7D);
 %0A%0A      

Update delete helper