package de.prob2.ui.visualisation;

import java.io.FileNotFoundException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.inject.Inject;
import com.google.inject.Singleton;

import de.prob2.ui.internal.StageManager;
import de.prob2.ui.prob2fx.CurrentTrace;
import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.ScrollPane;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;

@Singleton
public class VisualisationView extends AnchorPane {
	private static final Logger LOGGER = LoggerFactory.getLogger(VisualisationView.class);

	@FXML
	private StackPane probLogoStackPane;
	@FXML
	private ScrollPane visualisationScrollPane;
	@FXML
	private StateVisualisationView currentStateVisualisation;
	@FXML
	private StateVisualisationView previousStateVisualisation;
	@FXML
	private VBox previousStateVBox;

	private final CurrentTrace currentTrace;
	private final StageManager stageManager;

	@Inject
	public VisualisationView(final CurrentTrace currentTrace, final StageManager stageManager) {
		this.currentTrace = currentTrace;
		this.stageManager = stageManager;
		stageManager.loadFXML(this, "visualisation_view.fxml");
	}

	@FXML
	public void initialize() {
		visualisationScrollPane.visibleProperty().bind(probLogoStackPane.visibleProperty().not());
		probLogoStackPane.visibleProperty().bind(currentStateVisualisation.visualisationPossibleProperty().not());
		previousStateVBox.managedProperty().bind(previousStateVisualisation.visualisationPossibleProperty());
		previousStateVBox.visibleProperty().bind(previousStateVBox.managedProperty());

		currentTrace.currentStateProperty().addListener((observable, from, to) -> {
			try {
				currentStateVisualisation.visualiseState(to);
				previousStateVisualisation.visualiseState(from);
			} catch (FileNotFoundException e) {
				LOGGER.warn("Failed to open images for visualisation", e);
				Alert alert = stageManager.makeAlert(Alert.AlertType.WARNING, e.getMessage());
				alert.setHeaderText("Visualisation not possible");
				alert.showAndWait();
			}
		});
	}
}

@@ -1736,16 +1736,38 @@
 o) -%3E %7B%0A
+%09%09%09if (to != null) %7B%0A%09
 %09%09%09try %7B
@@ -1767,16 +1767,17 @@
 %09%09try %7B%0A
+%09
 %09%09%09%09curr
@@ -1822,16 +1822,17 @@
 o);%0A%09%09%09%09
+%09
 previous
@@ -1872,16 +1872,17 @@
 (from);%0A
+%09
 %09%09%09%7D cat
@@ -1916,16 +1916,17 @@
 ) %7B%0A%09%09%09%09
+%09
 LOGGER.w
@@ -1976,16 +1976,17 @@
 n%22, e);%0A
+%09
 %09%09%09%09Aler
@@ -2060,24 +2060,25 @@
 age());%0A%09%09%09%09
+%09
 alert.setHea
@@ -2116,24 +2116,25 @@
 ible%22);%0A%09%09%09%09
+%09
 alert.showAn
@@ -2142,16 +2142,22 @@
 Wait();%0A
+%09%09%09%09%7D%0A
 %09%09%09%7D%0A%09%09%7D

resolve problem that visualisation view sometimes tries to find images of previous machine (PROB2UI-132)