/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package swingtest;

import CommonFunctions.debugPrinter;
import applications.CarbonControl;
import applications.Trip;
import com.sun.javafx.tk.swing.SwingScene;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javafx.reflect.FXClassType;
import javafx.reflect.FXContext;
import javafx.reflect.FXFunctionMember;
import javafx.reflect.FXLocal;
import javafx.reflect.FXLocal.ObjectValue;
import javafx.scene.Scene;
import javax.swing.*;
import javax.swing.JComponent;
import mdot_gui.MainScreenJInterface;

/* Creates a window to house the javafx GUI.*/
public class JavaFXToSwingTest extends JFrame {

    public static JTextField tf = new JTextField("JavaFX for SWING");

    public JavaFXToSwingTest(String args[]) {
        this.setUndecorated(true);
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("IntelliDrive");

        //PUT CODE THAT SHOULD BE EXECUTED BEFORE SYSTEM SHUTS DOWN HERE!
        addWindowListener(new WindowAdapter() {

            @Override
            public void windowClosing(WindowEvent e) {

                //Write carbon emissions data to file!!
                if (CarbonControl.CarbonEmissionsStarted == true) {

                    CarbonControl.CarbonEmissionsStarted = false;
                    CarbonControl.getCarbon().start = false;

                    Trip t = CarbonControl.getCarbon().getMyTrip();
                    CarbonControl.tripsDatabase.add(t);

                    //write entire database out to xml file.
                    xmlpackage.CarbonEmissionsXMLWriter.writeToFile(applications.CarbonControl.tripsDatabase);

                }

                //write charges to xml file.
                xmlpackage.ChargesWriter.writeToFile(database.Context.chargedVector);

                debugPrinter.printLine("Charges data written to file.");
                System.exit(0);
            }
        });

        Container container = getContentPane();
        container.setLayout(new BorderLayout());

        //String sceneClass = "pathanimation.MyScene1";//"mdot_gui.MainScreen";//
        String sceneClass = "mdot_gui.MainScreen";//
        FXLocal.Context context = FXLocal.getContext();
        FXClassType instanceType = context.findClass(sceneClass);
	ObjectValue objectInstance = (ObjectValue) instanceType.newInstance();
        MainScreenJInterface mainScreenJavaFXScene = (MainScreenJInterface) objectInstance.asObject();
        // Pass arguments from java to javafx (javafx accepts Object[]
        mainScreenJavaFXScene.transferArgs(args);
        JComponent myScene = sceneInstanceToJComponent((Scene)mainScreenJavaFXScene);
        //Now let's show the JComponent+JavaFXScene in a simple Swing App.

        //JComponent myScene = SceneToJComponent.loadScene(sceneClass);

        JLabel label = new JLabel(" Below is a JavaFX Animation: ");

        myScene.setSize(300, 300);
        JPanel p = new JPanel();
        p.add(myScene, BorderLayout.CENTER);
        container.add(p, BorderLayout.SOUTH);
        pack();
    }
/**
     * This method wraps the Scene with a JComponent. This is what you can add
     * to your Swing application.
     * @param scene the JavaFX object that extends javafx.scene.Scene.
     * @return the javafx.scene.Scene wrapped by a JComponent.
     */
	public static JComponent sceneInstanceToJComponent(Scene scene) {
	    FXClassType sceneType = FXContext.getInstance().findClass("javafx.scene.Scene");
	    ObjectValue obj = FXLocal.getContext().mirrorOf(scene);
	    FXFunctionMember getPeer = sceneType.getFunction("impl_getPeer");
	    FXLocal.ObjectValue peer = (ObjectValue) getPeer.invoke(obj);
	    SwingScene swingScene = (SwingScene)peer.asObject();
	    return (JComponent) swingScene.scenePanel;
	}

    public static void main(final String args[]) {
        java.awt.EventQueue.invokeLater(
                new Runnable() {

                    public void run() {
                        JavaFXToSwingTest test = new JavaFXToSwingTest(args);
                        test.setVisible(true);

                    }
                });
    }
}
