package be.iminds.ilabt.jfed.rspec.convert;

import be.iminds.ilabt.jfed.rspec.model.javafx_impl.FXRspecFactory;
import javafx.embed.swing.JFXPanel;
import org.slf4j.LoggerFactory;
import org.testng.annotations.BeforeMethod;

import javax.swing.*;
import java.util.concurrent.CountDownLatch;

/**
 * RspecConverterTestFX
 */
public class RspecConverterTestFX extends RspecConverterTest {
    private static final org.slf4j.Logger LOG = LoggerFactory.getLogger(RspecConverterTestFX.class);

    protected final FXRspecFactory fxRspecFactory;

    public RspecConverterTestFX() {
        super(FXRspecFactory.getInstance());
        this.fxRspecFactory = (FXRspecFactory) rspecFactory;
    }

    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    private static boolean isJavaFxSetup = false;

    private static void setupJavaFx() throws InterruptedException {
        long timeMillis = System.currentTimeMillis();

        final CountDownLatch latch = new CountDownLatch(1);

        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                // initializes JavaFX environment
                new JFXPanel();

                latch.countDown();
            }
        });

        System.out.println("javafx initialising...");
        latch.await();
        System.out.println("javafx is initialised in " + (System.currentTimeMillis() - timeMillis) + "ms");
    }

    @BeforeMethod()
    public void setupSuite() throws InterruptedException {
        if (!isJavaFxSetup) {
            setupJavaFx();
            isJavaFxSetup = true;
        }
    }
}
