package com.syntacticsugar.vooga.authoring.library;

import java.util.ArrayList;

import com.syntacticsugar.vooga.authoring.level.LevelTabManager;
import com.syntacticsugar.vooga.gameplayer.objects.GameObjectType;
import com.syntacticsugar.vooga.util.gui.factory.GUIFactory;
import com.syntacticsugar.vooga.xml.data.IData;
import com.syntacticsugar.vooga.xml.data.ObjectData;
import com.syntacticsugar.vooga.xml.data.TowerData;

import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.control.TabPane.TabClosingPolicy;
import javafx.scene.layout.GridPane;

public class ObjectLibraryManager {

	private GridPane myView;
	private Button addSpawn;
	private Button addTower;
	private TabPane myLibraryTabs;
	private ArrayList<DataLibrary> myLibraries;

	public ObjectLibraryManager(LevelTabManager levels) {
		myView = new GridPane();
		myLibraryTabs = new TabPane();
		myLibraryTabs.setTabClosingPolicy(TabClosingPolicy.UNAVAILABLE);
		myLibraries = new ArrayList<>();
		populateLibraryTabs();
		addSpawn = GUIFactory.buildButton("Add To Wave", e -> processSpawner(levels), 150.0, null);
		addTower = GUIFactory.buildButton("Add To Towers", e -> processTower(levels), 150.0, null);
		myView.add(addSpawn, 0, 0, 1, 1);
		myView.add(addTower, 1, 0, 1, 1);
		myView.add(myLibraryTabs, 0, 1, 2, 1);

	}

	private GameObjectType[] getObjectTypes() {
		return GameObjectType.values();
	}

	private void populateLibraryTabs() {

		for (GameObjectType type : getObjectTypes()) {
			DataLibrary lib = new DataLibrary(type);
			Tab tab = new Tab(type.toString());
			tab.setContent(lib.getContent().getView());
			myLibraryTabs.getTabs().add(tab);
			myLibraries.add(lib);
		}
	}

	public Node getView() {
		return myView;
	}

	public void refresh() {
		for (DataLibrary lib : myLibraries) {
			lib.refresh();
		}
	}

	public ArrayList<DataLibrary> getLibraries() {
		return myLibraries;
	}

	private IData getCurrentData() {
		int num = myLibraryTabs.getSelectionModel().getSelectedIndex();
		IData data = myLibraries.get(num).getCurrentData();
		return data;
	}

	private void processSpawner(LevelTabManager levels) {
		if (getCurrentData().getType().equals(GameObjectType.ENEMY)) {
			ObjectData data = (ObjectData) getCurrentData();
			levels.addCurrentSpawner(data);
		}
	}

	private void processTower(LevelTabManager levels) {
		if (getCurrentData().getType().equals(GameObjectType.TOWER)) {
			TowerData data = (TowerData) getCurrentData();
			levels.addCurrentTower(data);
		}
	}

}

@@ -422,16 +422,44 @@
 rData;%0A%0A
+import javafx.geometry.Pos;%0A
 import j
@@ -476,16 +476,16 @@
 e.Node;%0A
-
 import j
@@ -672,16 +672,49 @@
 ridPane;
+%0Aimport javafx.scene.layout.VBox;
 %0A%0Apublic
@@ -744,16 +744,38 @@
 ager %7B%0A%0A
+%09private VBox myView;%0A
 %09private
@@ -784,22 +784,23 @@
 ridPane 
-myView
+buttons
 ;%0A%09priva
@@ -980,22 +980,23 @@
 ls) %7B%0A%09%09
-myView
+buttons
  = new G
@@ -998,32 +998,55 @@
 new GridPane();%0A
+%09%09myView = new VBox();%0A
 %09%09myLibraryTabs 
@@ -1261,37 +1261,36 @@
 pawner(levels), 
-150.0
+null
 , null);%0A%09%09addTo
@@ -1366,24 +1366,185 @@
 s), 
-150.0, null
+null, null);%0A%09%09addSpawn.setPrefWidth(120);%0A%09%09addTower.setPrefWidth(120);%0A%09%09buttons.add(addSpawn, 0, 0, 1, 1);%0A%09%09buttons.add(addTower, 1, 0, 1, 1);%0A%09%09// myView.setHgap(50
 );%0A%09%09
+// 
 myVi
@@ -1567,32 +1567,35 @@
  0, 0, 1, 1);%0A%09%09
+// 
 myView.add(addTo
@@ -1610,24 +1610,27 @@
 0, 1, 1);%0A%09%09
+// 
 myView.add(m
@@ -1655,16 +1655,173 @@
 , 2, 1);
+%0A%09%09myView.setAlignment(Pos.TOP_CENTER);%0A%09%09buttons.setAlignment(Pos.TOP_CENTER);%0A%09%09buttons.setHgap(77);%0A%09%09myView.getChildren().addAll(buttons, myLibraryTabs);
 %0A%0A%09%7D%0A%0A%09p

fix view