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) -> {
			if (to != null) {
				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();
				}
			}
		});
	}
}

@@ -1740,30 +1740,8 @@
 %3E %7B%0A
-%09%09%09if (to != null) %7B%0A%09
 %09%09%09t
@@ -1745,17 +1745,16 @@
 %09%09try %7B%0A
-%09
 %09%09%09%09curr
@@ -1795,16 +1795,38 @@
 te(to);%0A
+%09%09%09%09if (to != null) %7B%0A
 %09%09%09%09%09pre
@@ -1876,16 +1876,21 @@
 );%0A%09%09%09%09%7D
+%0A%09%09%09%7D
  catch (
@@ -1912,25 +1912,24 @@
 eption e) %7B%0A
-%09
 %09%09%09%09LOGGER.w
@@ -1979,17 +1979,16 @@
 n%22, e);%0A
-%09
 %09%09%09%09Aler
@@ -2062,25 +2062,24 @@
 age());%0A%09%09%09%09
-%09
 alert.setHea
@@ -2121,17 +2121,16 @@
 %22);%0A%09%09%09%09
-%09
 alert.sh
@@ -2145,23 +2145,18 @@
 t();%0A%09%09%09
-%09
 %7D%0A
-%09%09%09%7D
 %0A%09%09%7D);%0A%09

fix problem that visualisation from previous machine was still visible after opening a new machine