import java.util.Objects;
import java.util.Optional;


import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonBar.ButtonData;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Dialog;
import javafx.scene.control.Label;
import javafx.scene.control.Separator;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import javafx.scene.web.HTMLEditor;
import javafx.stage.Stage;

public class HtmlEditorDialogTestApp extends Application {

	public static void main(String[] args) {
		Application.launch(args);
	}

	@Override
	public void start(Stage stage) throws Exception {

		initializeButtonBoxAndButtons()
		.initializeLabelBox()
		.initializeSeparator()
		.initializeHtmlEditor()
		.initializeRoot();
		display(stage);
	}

	private Button button1, button2;
	private HBox buttonBox, labelBox;
	private Label labelTag;
	private Text sampleText;
	private Separator separator;
	private HTMLEditor appHtmlEditor, dialogEditor;
	private Dialog<String> dialog;
	private VBox root;

	private EventHandler<ActionEvent> actionHandler = e -> {

		// Create and initialize dialogEditor
		if (Objects.isNull(dialogEditor))
			dialogEditor = new HTMLEditor();
		if (e.getSource() == button1)
			dialogEditor.setHtmlText(appHtmlEditor.getHtmlText());
		else if (e.getSource() == button2)
			dialogEditor.setHtmlText(sampleText.getText());
		else
			throw new RuntimeException("how did this happen?!!");


		// Create and initialize dialog
		if (Objects.isNull(dialog))
			dialog = new Dialog<>();
		dialog.setHeaderText("HTMLEditor Dialog");
		dialog.setResultConverter((buttonType) -> {
			return buttonType.getButtonData() == ButtonData.OK_DONE
					? dialogEditor.getHtmlText()
							: null;
		});


		// Initialize dialog pane
		dialog
		.getDialogPane()
		.setContent(dialogEditor);
		dialog
		.getDialogPane()
		.getButtonTypes()
		.addAll(ButtonType.OK, ButtonType.CANCEL);


		// Display dialog and handle return
		Optional<String> result = dialog.showAndWait();
		if (result.isPresent()) {
			String resultText = result.get().length() > 0
					? result.get()
							: "empty String (\"\")";
					System.out.println("result text: " + resultText);
					appHtmlEditor.setHtmlText(resultText);
		}
		else
			System.out.println("nothing new from dialog editor!");


		// Cleanup
		dialog = null;
		dialogEditor = null;
	};



	private HtmlEditorDialogTestApp initializeButtonBoxAndButtons() {
		// Create and initialize buttonBox
		buttonBox = new HBox();
		buttonBox.setSpacing(10);
		buttonBox.setAlignment(Pos.CENTER_LEFT);

		// Create and initialize textFromEditorButton
		button1 = new Button("Button#1: Open dialog with editor text");
		button1.setOnAction(actionHandler);

		// Create and initialize textFromLabelButton
		button2 = new Button("Button#2: Open dialog with label text");
		button2.setOnAction(actionHandler);

		// Add buttons to buttonBox
		buttonBox
		.getChildren()
		.addAll(button1, button2);

		return this;
	}

	private HtmlEditorDialogTestApp initializeLabelBox() {
		// Create and initialize labelBox, labelTag and sampleText
		labelBox = new HBox();
		labelBox.setSpacing(5);
		labelTag = new Label("Sample text ->      ");
		sampleText = new Text("Explicit text to initialize HTMLEditor in dialog");
		labelBox
		.getChildren()
		.addAll(labelTag, sampleText);

		return this;
	}

	private HtmlEditorDialogTestApp initializeSeparator() {
		separator = new Separator();
		VBox.setMargin(separator, new Insets(10, 0, 10, 0));

		return this;
	}

	private HtmlEditorDialogTestApp initializeHtmlEditor() {
		// Create & initialize htmlEditor
		appHtmlEditor = new HTMLEditor();
		appHtmlEditor.setPrefHeight(350);
		appHtmlEditor.setPrefWidth(600);
		appHtmlEditor.setHtmlText("Original text for htmlEditor");

		VBox.setVgrow(appHtmlEditor, Priority.SOMETIMES);

		return this;
	}

	private HtmlEditorDialogTestApp initializeRoot() {

		// Create and initialize root
		root = new VBox();
		root.setPadding(new Insets(10, 10, 10, 10));
		root.setSpacing(5);
		root.setAlignment(Pos.CENTER_LEFT);
		root.setStyle(
				"-fx-border-style: solid inside;" +
						"-fx-border-width: 2;" +
						"-fx-border-insets: 5;" +
						"-fx-border-radius: 5;" +
				"-fx-border-color: cornflowerblue;");

		// Add children to root
		root
		.getChildren()
		.addAll(
				buttonBox,
				labelBox,
				separator,
				appHtmlEditor);

		return this;
	}

	private void display(Stage stage) {

		// Create scene and display
		Scene scene = new Scene(root);
		stage.setScene(scene);
		stage.setWidth(600);
		stage.setHeight(350);
		stage.setTitle("HTMLEditor Dialog Application");
		stage.show();
	}
}
