package org.genericsystem.distributed.ui.utils;

import io.vertx.core.json.JsonObject;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;

import javafx.collections.ModifiableObservableListBase;
import javafx.collections.ObservableList;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.control.ScrollPane;
import javafx.scene.layout.Pane;

import org.genericsystem.distributed.ui.Element;
import org.genericsystem.distributed.ui.HtmlNode;

public class Utils {
	static public <PARENTNODE> Function<PARENTNODE, ObservableList<?>> getClassChildren(Element<PARENTNODE> parent) {
		Function<Pane, ObservableList<?>> paneChildren = Pane::getChildren;
		Function<Group, ObservableList<?>> groupChildren = Group::getChildren;
		Function<HtmlNode, List<?>> nodeJsChildren = parentNodeJs -> new ModifiableObservableListBase() {
			private List<HtmlNode> childrenNode = new ArrayList<>();

			@Override
			public Object get(int index) {
				return childrenNode.get(index);
			}

			@Override
			public int size() {
				return childrenNode.size();
			}

			@Override
			protected void doAdd(int index, Object element) {
				HtmlNode htmlNode = ((HtmlNode) element);
				JsonObject jsonObj = new JsonObject().put("msg_type", "A");
				htmlNode.fillJsonAdd(parentNodeJs, jsonObj);
				parent.sendMessage(jsonObj);
				childrenNode.add(htmlNode);
			}

			@Override
			protected Object doSet(int index, Object element) {
				return childrenNode.set(index, ((HtmlNode) element));
			}

			@Override
			protected Object doRemove(int index) {
				JsonObject jsonObj = new JsonObject().put("msg_type", "R");
				childrenNode.get(index).fillJsonRemove(jsonObj);
				parent.sendMessage(jsonObj);
				return childrenNode.remove(index);
			}
		};

		Function<ScrollPane, ObservableList<?>> scrollChildren = scrollPane -> new ModifiableObservableListBase<Node>() {

			@Override
			public Node get(int index) {
				assert size() == 1 && index == 0;
				return scrollPane.getContent();
			}

			@Override
			public int size() {
				return scrollPane.getContent() == null ? 0 : 1;
			}

			@Override
			protected void doAdd(int index, Node element) {
				if (size() != 0)
					throw new IllegalStateException("Only one element is supported in a GSScrollPane !");
				scrollPane.setContent(element);
			}

			@Override
			protected Node doSet(int index, Node element) {
				Node result = doRemove(index);
				doAdd(index, element);
				return result;
			}

			@Override
			protected Node doRemove(int index) {
				if (size() == 0)
					throw new IllegalStateException();
				Node result = scrollPane.getContent();
				scrollPane.setContent(null);
				return result;
			}
		};

		// if (parent != null) {
		if (Pane.class.isAssignableFrom(parent.nodeClass))
			return (Function) paneChildren;
		if (Group.class.isAssignableFrom(parent.nodeClass))
			return (Function) groupChildren;
		if (ScrollPane.class.isAssignableFrom(parent.nodeClass))
			return (Function) scrollChildren;
		if (HtmlNode.class.isAssignableFrom(parent.nodeClass))
			return (Function) nodeJsChildren;
		// }s
		throw new IllegalStateException("Not a supported JavaFX container : ");

	}
}

@@ -80,16 +80,46 @@
 Object;%0A
+import java.util.AbstractList;
 %0Aimport 
@@ -198,17 +198,16 @@
 nction;%0A
-%0A
 import j
@@ -422,17 +422,16 @@
 t.Pane;%0A
-%0A
 import o
@@ -793,32 +793,33 @@
 p::getChildren;%0A
+%0A
 %09%09Function%3CHtmlN
@@ -828,17 +828,24 @@
 e, List%3C
-?
+HtmlNode
 %3E%3E nodeJ
@@ -880,36 +880,30 @@
 new 
-ModifiableObservableListBase
+AbstractList%3CHtmlNode%3E
 () %7B
@@ -987,22 +987,24 @@
 %09public 
-Object
+HtmlNode
  get(int
@@ -1146,33 +1146,28 @@
 ide%0A%09%09%09p
-rotected
+ublic
  void 
-doA
+a
 dd(int i
@@ -1164,38 +1164,40 @@
  add(int index, 
-Object
+HtmlNode
  element) %7B%0A%09%09%09%09
@@ -1215,36 +1215,24 @@
 mlNode =
- ((HtmlNode)
  element
 );%0A%09%09%09%09J
@@ -1219,25 +1219,24 @@
 de = element
-)
 ;%0A%09%09%09%09JsonOb
@@ -1430,27 +1430,24 @@
 %09%09%09p
-rotected Object doS
+ublic HtmlNode s
 et(i
@@ -1456,22 +1456,24 @@
  index, 
-Object
+HtmlNode
  element
@@ -1516,19 +1516,8 @@
 x, (
-(HtmlNode) 
 elem
@@ -1550,27 +1550,24 @@
 %09%09%09p
-rotected Object doR
+ublic HtmlNode r
 emov

Change AbstractCollection