package com.kodcu.component;

import javafx.application.Platform;
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);

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

        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();
    }
}

@@ -1098,32 +1098,67 @@
 new ListView();%0A
+        listView.setMinHeight(40);%0A
         listView
@@ -1175,32 +1175,32 @@
 lass().clear();%0A
-
         Observab
@@ -1352,44 +1352,8 @@
 );%0A%0A
-        Platform.runLater(()-%3E%7B%0A    
     
@@ -1423,28 +1423,24 @@
             
-    
 .map(List::s
@@ -1456,28 +1456,24 @@
             
-   
- 
 .map(e -%3E e 
@@ -1474,28 +1474,24 @@
  -%3E e * 40)%0A
-    
             
@@ -1544,20 +1544,16 @@
         
-   
- 
 .orElse(
@@ -1555,28 +1555,24 @@
 Else(300));%0A
-    
         list
@@ -1590,20 +1590,8 @@
 h();
-%0A        %7D);
 %0A%0A  

Delete alert fix