package de.prob2.ui.formula;

import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;

import com.google.inject.Inject;
import de.prob.animator.command.ExpandFormulaCommand;
import de.prob.animator.command.InsertFormulaForVisualizationCommand;
import de.prob.animator.domainobjects.ExpandedFormula;
import de.prob.animator.domainobjects.IEvalElement;
import de.prob.statespace.AnimationSelector;
import javafx.scene.Scene;
import javafx.scene.control.ScrollPane;
import javafx.stage.Stage;

public class FormulaGenerator {
	private IEvalElement formula;
	private FormulaGraph graph;
	private final Set<String> collapsedNodes = new CopyOnWriteArraySet<>();
	
	private final AnimationSelector animationSelector;
	
	@Inject
	public FormulaGenerator(final AnimationSelector animationSelector) {
		this.animationSelector = animationSelector;
	}
	
	public void setFormula(final IEvalElement formula) {
		try {
			InsertFormulaForVisualizationCommand cmd1 = new InsertFormulaForVisualizationCommand(formula);
			animationSelector.getCurrentTrace().getStateSpace().execute(cmd1);
			
			ExpandFormulaCommand cmd2 = new ExpandFormulaCommand(cmd1.getFormulaId(), animationSelector.getCurrentTrace().getCurrentState());
			animationSelector.getCurrentTrace().getStateSpace().execute(cmd2);
			ExpandedFormula data = cmd2.getResult();
			data.collapseNodes(new HashSet<>(collapsedNodes));
			
			//graph = new FormulaGraph(new FormulaNode(25, 350, data));
			graph = new FormulaGraph(new FormulaNode(data));
			draw();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void collapseNode(final String id) {
		collapsedNodes.add(id);
	}

	public void expandNode(final String id) {
		collapsedNodes.remove(id);
	}
	
	private void draw() {
		ScrollPane root = new ScrollPane(graph);
		Stage stage = new Stage();
		stage.setTitle("Mathematical Expression");
		Scene scene = new Scene(root, 1024, 768);
		stage.setScene(scene);
		stage.show();
	}
}

@@ -493,24 +493,63 @@
 ScrollPane;%0A
+import javafx.scene.input.MouseButton;%0A
 import javaf
@@ -563,16 +563,16 @@
 .Stage;%0A
-
 %0Apublic 
@@ -1458,71 +1458,8 @@
 %09%09%09%0A
-%09%09%09//graph = new FormulaGraph(new FormulaNode(25, 350, data));%0A
 %09%09%09g
@@ -1506,16 +1506,37 @@
 data));%0A
+%09%09%09graph.autosize();%0A
 %09%09%09draw(
@@ -1889,16 +1889,16 @@
 sion%22);%0A
-
 %09%09Scene 
@@ -1933,16 +1933,631 @@
 , 768);%0A
+%09%09root.setOnMouseClicked(e -%3E %7B%0A%09%09%09if(e.getButton() == MouseButton.PRIMARY) %7B%0A%09%09%09%09%0A%09%09%09%09graph.setPrefHeight(graph.getPrefHeight() * 2);%0A%09%09%09%09graph.setPrefWidth(graph.getPrefWidth() * 2);%0A%09%09%09%09graph.setScaleX(graph.getScaleX() * 2);%0A%09%09%09%09graph.setScaleY(graph.getScaleY() * 2);%0A%0A%09%09%09%7D else if(e.getButton() == MouseButton.SECONDARY) %7B%0A%09%09%09%09graph.setPrefHeight(graph.getPrefHeight() * 0.5);%0A%09%09%09%09graph.setPrefWidth(graph.getPrefWidth() * 0.5);%0A%09%09%09%09%0A%09%09%09%09graph.setScaleX(graph.getScaleX() * 0.5);%0A%09%09%09%09graph.setScaleY(graph.getScaleY() * 0.5);%0A%09%09%09%7D%0A%09%09%09root.setContent(graph);%0A%09%09%09//scene.setRoot(root);%0A%09%09%09//stage.show();%0A%09%09%7D);%0A
 %09%09stage.

Prepare for Zooming in FormulaGraph