package de.prob2.ui.visualisation.magiclayout;

import java.util.Arrays;
import java.util.List;

import de.prob2.ui.visualisation.magiclayout.graph.Graph;
import de.prob2.ui.visualisation.magiclayout.graph.Vertex;
import javafx.scene.Node;

public class MagicGraphFX implements MagicGraphI {

	@Override
	public List<MagicShape> getPossibleShapes() {
		MagicShape shapes[] = new MagicShape[] { MagicShape.RECTANGLE, MagicShape.CIRCLE, MagicShape.ELLIPSE };
		return Arrays.asList(shapes);
	}

	@Override
	public Node generateMagicGraph(List<MagicNodes> nodegroups, List<MagicEdges> edgegroups) {
		Graph graph = new Graph();
		nodegroups.forEach(nodegroup -> {
			List<String> nodes = Arrays.asList(nodegroup.getExpression().replaceAll("[{]|[}]", "").split(","));
			Vertex.Style style = new Vertex.Style(toVertexType(nodegroup.getShape()), nodegroup.getNodeColor(),
					nodegroup.getLineColor(), nodegroup.getLineWidth(), nodegroup.getLineType());
			nodes.forEach(node -> graph.addVertex(new Vertex(node, style)));
		});
		edgegroups.forEach(edgegroup -> {
			List<String> edges = Arrays.asList(edgegroup.getExpression().replaceAll("[{]|[}]|[(]|[)]", "").split(","));
			edges.forEach(edge -> graph.addEdge(edge.split("↦")[0], edge.split("↦")[1], edgegroup.getName()));
		});
		return graph;
	}

	private Vertex.Type toVertexType(MagicShape shape) {
		switch (shape) {
		case CIRCLE:
			return Vertex.Type.CIRCLE;
		case ELLIPSE:
			return Vertex.Type.ELLIPSE;
		case RECTANGLE:
			return Vertex.Type.RECTANGLE;
		default:
			return Vertex.Type.RECTANGLE;
		}
	}

}

@@ -1045,32 +1045,98 @@
 (edgegroup -%3E %7B%0A
+%09%09%09if (!%22NOT-INITIALISED: %22.equals(edgegroup.getExpression())) %7B%0A%09
 %09%09%09List%3CString%3E 
@@ -1141,32 +1141,39 @@
 %3E edges = Arrays
+%0A%09%09%09%09%09%09
 .asList(edgegrou
@@ -1237,16 +1237,17 @@
 (%22,%22));%0A
+%09
 %09%09%09edges
@@ -1336,24 +1336,29 @@
 etName()));%0A
+%09%09%09%7D%0A
 %09%09%7D);%0A%09%09retu

fix NPE when constant/variable is not initialized