/*
 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
 * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 *
 */
import com.sun.glass.ui.Robot;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.lang.reflect.InvocationTargetException;
import javafx.application.Application;
import javafx.embed.swing.SwingNode;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;
import javax.swing.*;

/**
 * Test checks that component can get mouse, key and focus events.
 *
 * It is automatically generated test.
 *
 * @author andrei-eremeev
 */
public class SwingNodeJButtonTest extends Application {

    private JComponent component;
    private JButton second;
    private Point location;
    private Dimension size;

    private boolean isOk = true;

    @Override
    public void start(final Stage stage) throws Exception {
        Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
            @Override
            public void uncaughtException(Thread t, Throwable e) {
                System.err.println("Uncaught exception on thread : " + t);
                e.printStackTrace();
                System.exit(1);
            }
        });
        final Robot robot = createRobot();
        SwingNode swingNode = new SwingNode();
        init(swingNode);
        StackPane pane = new StackPane();
        pane.getChildren().add(swingNode);
        Scene scene = new Scene(pane, 300, 300);
        stage.setScene(scene);
        stage.setX(150);
        stage.setY(150);
        robot.mouseMove(0, 0);
        stage.setOnShown(new EventHandler<WindowEvent>() {
            @Override
            public void handle(WindowEvent t) {
                new Thread(() -> {
                    try {
                        SwingUtilities.invokeAndWait(() -> second.requestFocus());
                        SwingUtilities.invokeAndWait(() -> {
                            location = component.getLocationOnScreen();
                            size = component.getSize();
                        });
                        invokeAndWait(() -> {
                            robot.mouseMove(location.x + size.width / 2, location.y + size.height / 2);
                            delay(500);
                            click(robot, MouseEvent.BUTTON1);
                        });
                        invokeAndWait(() -> robot.mouseMove(location.x + size.width / 2, location.y - 1));
                        invokeAndWait(() -> {
                            robot.mouseMove(location.x + size.width / 2, location.y + size.height / 2);
                            delay(500);
                            click(robot, MouseEvent.BUTTON1);
                        });
                        invokeAndWait(() -> {
                            robot.keyPress(KeyEvent.VK_CONTROL);
                            delay(100);
                            robot.keyPress(KeyEvent.VK_TAB);
                            delay(250);
                            robot.keyRelease(KeyEvent.VK_TAB);
                            delay(100);
                            robot.keyRelease(KeyEvent.VK_CONTROL);
                        });
                    } catch (InterruptedException | InvocationTargetException e) {
                        e.printStackTrace();
                        System.exit(1);
                    }
                }).start();
            }
        });
        stage.show();
    }


    private void click(Robot robot, int key) {
        robot.mousePress(key);
        delay(100);
        robot.mouseRelease(key);
    }

    private void init(final SwingNode node) throws InterruptedException {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JPanel panel = new JPanel();
                panel.setLayout(null);
                component = new JButton("Component");
                component.setLocation(25, 25);
                component.setSize(100, 100);
                second = new JButton("OK");
                second.setLocation(25, 150);
                second.setSize(100, 100);
                panel.add(second);
                panel.add(component);
                node.setContent(panel);
            }
        });
    }

    private void invokeAndWait(Runnable run) {
        com.sun.glass.ui.Application.invokeAndWait(run);
    }

    private Robot createRobot() {
        return com.sun.glass.ui.Application.GetApplication().createRobot();
    }

    public void delay(int delay) {
        try {
            Thread.sleep(delay);
        } catch (Exception e) {
        }
    }

    public static void main(String[] args) {
        launch(args);
    }
}
