package tests;

import java.awt.Frame;
import java.awt.Rectangle;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.control.TextArea;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.paint.Color;
import javafx.stage.Screen;

import javax.swing.SwingUtilities;

import com.jme3.app.SimpleApplication;
import com.jme3.math.ColorRGBA;
import com.jme3x.jfx.JmeFxContainer;
import com.sun.javafx.PlatformUtil;

public class TestJavaFX extends SimpleApplication {

	private static Group root;
	private static ComboBox<String> comboBox;

	public static void main(String[] args) {
		new TestJavaFX().start();
	}

	@Override
	public void simpleInitApp() {
		this.setPauseOnLostFocus(false);
		this.flyCam.setDragToRotate(true);
		final JmeFxContainer jmefx = JmeFxContainer.install(this, getGuiNode(), true, null);
		Platform.runLater(new Runnable() {

			@Override
			public void run() {
				jmefx.setScene(createScene(), root);
			}
		});

		this.guiNode.attachChild(jmefx.getJmeNode());

		this.viewPort.setBackgroundColor(ColorRGBA.Red);
	}

	public static Scene createScene() {

		root = new Group();

		Scene scene = new Scene(root, 600, 600, true);
		scene.setFill(new Color(0, 0, 0, 0));

		final TreeItem<String> treeRoot = new TreeItem<String>("Root node");
		treeRoot.getChildren().addAll(Arrays.asList(new TreeItem<String>("Child Node 1"), new TreeItem<String>("Child Node 2"), new TreeItem<String>("Child Node 3")));
		treeRoot.getChildren().get(2).getChildren().addAll(Arrays.asList(new TreeItem<String>("Child Node 4"), new TreeItem<String>("Child Node 5")));

		final TreeView treeView = new TreeView();
		treeView.setShowRoot(true);

		treeView.setRoot(treeRoot);

		treeRoot.setExpanded(true);
		treeView.setLayoutY(100);

		Button test1 = new Button("Test1");
		test1.setLayoutX(500);
		test1.setLayoutY(500);
		test1.setOnAction(event -> {

		});

		CheckBox test2 = new CheckBox("Test2");
		test2.setLayoutX(700);
		test2.setLayoutY(700);

		MenuBar bar = new MenuBar();

		Menu testMenu = new Menu("Test");
		bar.getMenus().add(testMenu);
		MenuItem i1 = new MenuItem("Entry1");
		MenuItem i2 = new MenuItem("Entry2");
		Menu sub = new Menu("Submenu");
		sub.getItems().addAll(new MenuItem("Sub entry 1"), new MenuItem("Sub Entry 2"));
		testMenu.getItems().addAll(i1, sub, i2);

		TextArea ta = new TextArea();
		ta.setOpacity(0.4);
		ta.setLayoutX(400);
		ta.setLayoutY(300);

		ChoiceBox cb = new ChoiceBox();

		cb.setItems(FXCollections.observableArrayList("Alfa", "Beta"));
		cb.setLayoutX(300);
		cb.setLayoutY(200);

		comboBox = new ComboBox<String>();
		comboBox.setLayoutX(350);
		comboBox.setLayoutY(250);

		root.getChildren().addAll(treeView, test1, bar, test2, ta, cb, comboBox);

		ololo();

		return scene;
	}


	public static final void ololo() {

		List<String> values = new ArrayList<String>();

		SwingUtilities.invokeLater(() -> {

			final Frame frame = new Frame();
			frame.setVisible(true);

			// wait AWT init native minimum size
				try {
					Thread.sleep(500);
				} catch(Exception e) {
					e.printStackTrace();
				}

				final Rectangle bounds = frame.getBounds();
				values.add(String.valueOf(bounds));
				values.add(String.valueOf(frame.getInsets()));
				values.add(String.valueOf(frame));
				values.add(String.valueOf(frame.getPeer().getMinimumSize()));
				values.add(String.valueOf(frame.getPeer().getPreferredSize()));

				frame.setVisible(false);
				frame.dispose();

				Platform.runLater(() -> {

					final Screen primary = Screen.getPrimary();

					comboBox.getItems().addAll(values);
					comboBox.getItems().add(String.valueOf(primary.getBounds()));
					comboBox.getItems().add(String.valueOf(primary.getVisualBounds()));
				});
			});
	}
}
