//package de.schuette.jfx.stage_opacity_bug;

import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.input.KeyCode;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.util.Duration;

/**
 * A simple app that demostrates a failure for JDK-8149490, 
 * 
 * from package de.schuette.jfx.stage_opacity_bug;
 */

public class FadeApp extends Application {

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

    private Label label;

    @Override
    public void start(Stage stage) {
        if (stage == null) {
            throw new IllegalArgumentException("No stage was set.");
        }

        this.label = new Label("HELLO WORLD");

        Scene scene = new Scene(label, 300, 300);

        scene.setOnKeyPressed(e -> {
            if (e.getCode() == KeyCode.ESCAPE) {
                stage.close();
            }
        });

        stage.setScene(scene);
        stage.setOpacity(1);
        stage.initStyle(StageStyle.TRANSPARENT);
        stage.setTitle("Opacity change does result in repaint when stage style is transparent.");
        stage.setAlwaysOnTop(true);
        stage.show();

        Timeline t = new Timeline(new KeyFrame(Duration.millis(0),
                new KeyValue(stage.opacityProperty(), 1)), new KeyFrame(
                        Duration.millis(500), new KeyValue(stage.opacityProperty(), 0)));
        t.setAutoReverse(true);
        t.setCycleCount(Timeline.INDEFINITE);

        t.playFromStart();

        t = new Timeline(new KeyFrame(Duration.millis(400), e -> {
//label.textProperty().set(String.valueOf(Math.random()));
        }));
        t.setCycleCount(Timeline.INDEFINITE);
        t.playFromStart();
    }

}
