/*
 * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 */
package demo;

import com.sun.javafx.application.ParametersImpl;
import java.util.Iterator;
import javafx.stage.Stage;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.stage.Window;
//import org.jemmy.env.Timeout;
//import org.jemmy.timing.State;
//import org.jemmy.timing.Waiter;

/**
 * Launcher class which is aware of different run modes we need for our tests
 *
 * @author Sergey Grinev
 */
public class AppLauncher {

    public void launch(final Class cl, String[] args) {
        switch (mode) {
            case DEFAULT:
                defaultLaunch(cl, args);
                break;
                /*
            case REMOTE:
                launchOnRemoteStage(cl, args);
                break;
                */
            default:
                throw new IllegalStateException("Unknown launch mode: " + mode);

        }
    }

    public enum Mode {

        DEFAULT, SWING, SWT, REMOTE
    };
    private Mode mode = Mode.DEFAULT;
    private long testDelay = Long.getLong("test.javafx.testdelay", 1000);
    private long testDelayRemote = Long.getLong("test.javafx.testdelayremote", 4000);

    public long getTestDelay() {
        return mode == Mode.REMOTE ? testDelayRemote : testDelay;
    }

    public Mode getMode() {
        return mode;
    }

    private AppLauncher() {
        /*
        if (Boolean.getBoolean("javafx.swinginteroperability")) {
            mode = Mode.SWING;
        }
        if (Boolean.getBoolean("javafx.swtinteroperability")) {
            mode = Mode.SWT;
        }
        */
    }
    private final static AppLauncher INSTANCE = new AppLauncher();

    public static AppLauncher getInstance() {
        return INSTANCE;
    }
/*
    private static void instantiateOnSwingQueue(final Class<? extends Interoperability> cl, String[] args) {
        try {
            SwingAWTUtils.instantiateOnSwingQueue(cl);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    private static void instantiateOnSWTQueue(final Class<? extends Interoperability> cl, String[] args) {
        try {
            Interoperability obj = cl.newInstance();
            obj.startSWT();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
*/
    /*
    private void launchOnRemoteStage(final Class<? extends Application> cl, final String[] args) {
        Platform.runLater(new Runnable() {

            public void run() {
                try {
                    Application obj = (Application) cl.newInstance();
                    ParametersImpl.registerParameters(obj, new ParametersImpl(args));
                    obj.start(remoteStage);
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        });
    }
*/
    private static void defaultLaunch(final Class<? extends Application> cl, final String[] args) {
        new Thread(new Runnable() {

            public void run() {
                Application.launch(cl, args);
            }
        }, "FXSQE app launch thread").start();

    }

    /**
     * Setup stage to be used by launcher. E.g. stage provided by plugin
     * launcher
     *
     * @param remoteStage
     */
    /*
    public void setRemoteStage(Stage remoteStage) {
        if (remoteStage == null) {
            throw new IllegalArgumentException("Stage can't null");
        }
        if (this.remoteStage != null) {
            throw new IllegalStateException("Current implementation allows only one remote stage per VM");
        }
        mode = Mode.REMOTE;
        this.remoteStage = remoteStage;
    }
    */
    private Stage remoteStage = null;
}
