package net.hexid.hexbot.bot.gui;

import java.util.ArrayList;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuBuilder;
import javafx.scene.image.Image;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import net.hexid.hexbot.bot.Bots;

public class BotGUI extends javafx.application.Application {
	protected AnchorPane root;
	protected BotTabPane tabPane;

	public static void init(String[] args) {
		launch(args);
	}

	@Override public void start(Stage stage) {
		root = new AnchorPane();
		tabPane = createTabPane();
		root.getChildren().addAll(tabPane, createMenuBar());

		// if there are arguments
		if(getParameters().getRaw().size() > 0) {
			// iterate over each argument (names of bots)
			for(String arg : getParameters().getRaw()) {
				// add a new tab if the argument is the name of a bot
				if(Bots.hasBot(arg)) addTab(arg);
			}
		}
		if(tabPane.getTabs().size() == 0) { // if no arguments were passed to the gui
			System.out.println("args: 'gui' [BotName...]");
			System.out.println("Available bots: " + Bots.getAvailableBots());
		}

		stage.getIcons().add(new Image(BotGUI.class.getResourceAsStream("/HexBot.png")));
		stage.setTitle("HexBot by Hexid");
		stage.setScene(new javafx.scene.Scene(root, 550, 300));
		stage.show();
	}

	protected void addTab(String botName) {
		try {
			// create a new tab based on the bot's guiClassPath
			tabPane.addBotTab((BotTab)Class.forName(Bots.getBotGuiClassPath(botName)).newInstance());
		} catch(Exception e) {
			e.printStackTrace();
		}
	}

	protected MenuBar createMenuBar() {
		MenuBar menuBar = new MenuBar();
		ArrayList<Menu> menus = new ArrayList<>();

		// iterate over botNames
		for(final String botName : Bots.botNames()) {
			// create a menu item with the bot's long name
			menus.add(MenuBuilder.create().text(Bots.getBotLongName(botName))
					.onShown(new EventHandler<Event>() {
						@Override public void handle(Event e) {
							((Menu)e.getSource()).hide();
							addTab(botName); // create a new tab
						}
					// set the items to be an empty menuItem so that
					// when clicked it will disappear immediately
					}).items(new javafx.scene.control.MenuItem()).build());
		}

		if(menus.size() == 0) // if no bot tabs were created
			menus.add(MenuBuilder.create().text("No bots found").build());
		menuBar.getMenus().addAll(menus);
		
		AnchorPane.setTopAnchor(menuBar, 0.0d);
		AnchorPane.setLeftAnchor(menuBar, 0.0d);
		AnchorPane.setRightAnchor(menuBar, 0.0d);
		return menuBar;
	}

	protected BotTabPane createTabPane() {
		tabPane = new BotTabPane();
		AnchorPane.setTopAnchor(tabPane, 24.0d); // fit to the top menu bar
		AnchorPane.setBottomAnchor(tabPane, 0.0d);
		AnchorPane.setLeftAnchor(tabPane, 0.0d);
		AnchorPane.setRightAnchor(tabPane, 0.0d);
		return tabPane;
	}
}
@@ -43,19 +43,33 @@
 java
-.util.Array
+fx.collections.Observable
 List
@@ -239,24 +239,62 @@
 enuBuilder;%0A
+import javafx.scene.control.MenuItem;%0A
 import javaf
@@ -1480,20 +1480,17 @@
 %0A%09%09try %7B
-%0A%09%09%09
+ 
 // creat
@@ -1757,21 +1757,26 @@
 ar();%0A%09%09
-Array
+Observable
 List%3CMen
@@ -1790,54 +1790,92 @@
 s = 
-new ArrayList%3C%3E
+menuBar.getMenus
 ();%0A
-%0A
 %09%09
-// iterate over botNames
+MenuBuilder%3C? extends MenuBuilder%3E mb = MenuBuilder.create();%0A
 %0A%09%09f
@@ -1914,24 +1914,49 @@
 otNames()) %7B
+ // iterate over botNames
 %0A%09%09%09// creat
@@ -2007,36 +2007,18 @@
 nus.add(
-MenuBuilder.create()
+mb
 .text(Bo
@@ -2231,147 +2231,21 @@
 %09%09%09%09
-// set the items to be an empty menuItem so that%0A%09%09%09%09%09// when clicked it will disappear immediately%0A%09%09%09%09%09%7D).items(new javafx.scene.control.
+%7D).items(new 
 Menu
@@ -2335,36 +2335,18 @@
 nus.add(
-MenuBuilder.create()
+mb
 .text(%22N
@@ -2374,46 +2374,8 @@
 ));%0A
-%09%09menuBar.getMenus().addAll(menus);%0A%09%09
 %0A%09%09A

Refactor to use ObservableList for direct access to the menu