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

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

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

/**
 * TestRspecReconstrutableFX
 */
public class TestRspecReconstructableFX extends TestRspecReconstructable {
    private final static Logger LOG = LoggerFactory.getLogger(TestRspecReconstructableFX.class);


    /////////////////////////////////////////////////////////////////////////////////////////////////////////////
    //// get JavaFX initialised before these tests //////////////////////////////////////////////////////////////
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////
    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;
        }
    }

    @BeforeMethod
    private void prepare() throws InterruptedException {
        setupSuite();
    }
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////

    private static RspecFactory fxRspecFactory = FXRspecFactory.getInstance();

    public TestRspecReconstructableFX() {
        super(fxRspecFactory);
    }


    @Factory
    public static Object[] createReconstructableTests() {
        return createReconstructableTestsInternal(fxRspecFactory);
    }


    @Test
    public void testRspecRecorderCommand() throws Throwable {
        TestRspecReconstructable.testXmlStaxReconstructable(TestRspecsLibrary.requestWithCommandsAndBarriersPreStaxFormat, fxRspecFactory);
    }
}
