/*
 * Copyright (c) 2022, 2023, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

import javafx.animation.Animation;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import javafx.util.Duration;

public class SceneChangeShouldNotFocusStageTest extends Application {

    private static final String os = System.getProperty("os.name");
    private static final boolean isMac = os.startsWith("Mac");
    private static final boolean isWindows = os.startsWith("Windows");

    @Override
    public void start(Stage primaryStage) {
        if (!isMac && !isWindows) {
            System.err.println("Test can only be run on Mac and Windows 11");
        }

        Scene scene1 = new Scene(new Pane(new TextField("This is scene1")), 200, 200);
        Scene scene2 = new Scene(new Pane(new TextField("This is scene2")), 200, 200);

        int firstFrameTime = (isMac ? 200 : 100);
        int secondFrameTime = (isMac ? 400 : 200);

        Timeline tl = new Timeline();
        tl.setCycleCount(Animation.INDEFINITE);
        tl.getKeyFrames().addAll(new KeyFrame(Duration.millis(firstFrameTime), e -> primaryStage.setScene(scene1)),
                new KeyFrame(Duration.millis(secondFrameTime), e -> primaryStage.setScene(scene2)));

        primaryStage.setOnShown(e -> {
            tl.play();
        });

        // The key to reproducing this seems to be having an animation
        // when the window is first shown.
        if (isMac) {
            primaryStage.setIconified(true);
        }
        primaryStage.show();
    }
}
