import com.sun.javafx.perf.PerformanceTracker;
import java.util.ArrayList;
import java.util.List;
import javafx.animation.AnimationTimer;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.stage.Stage;
import javafx.util.Duration;


/**
 *
 * @author ekaterina.pavlova@oracle.com
 */
public class LabelPerf extends Application {

    enum TreeViewType {WIDE, DEPTH};

    static double SCENE_WEIGHT = 300.0;
    static double SCENE_HEIGHT = 600.0;

    ArrayList<Label> labels1 = new ArrayList();
    ArrayList<Label> labels2 = new ArrayList();
    
    Stage stage;
    PerformanceTracker perfTracker;
    Group rootGroup;
    int iters=1;
    @Override public void start(Stage primaryStage) {
        stage = new Stage();

        rootGroup = new Group();
        Scene scene = new Scene(rootGroup, SCENE_WEIGHT, SCENE_HEIGHT);

        stage.setScene(scene);
        stage.sizeToScene();
        stage.show();

        perfTracker = PerformanceTracker.getSceneTracker(scene);

        Timeline autoTL = new Timeline();
        KeyFrame endWarmupKF = new KeyFrame(new Duration(5000), new EventHandler<ActionEvent>() {
            @Override public void handle(ActionEvent e) {
                String fpsText = String.format("WARMUP DONE: fps %.2f", perfTracker.getInstantFPS());
                System.out.println(fpsText);
                perfTracker.resetAverageFPS();                
            }
        });
        KeyFrame endTestKF = new KeyFrame(new Duration(60000), new EventHandler<ActionEvent>() {
            @Override public void handle(ActionEvent e) {
                String fpsText = String.format("TEST DONE: fps %.2f", perfTracker.getInstantFPS());
                System.out.println(fpsText);
                Platform.exit();
            }
        });
        autoTL.getKeyFrames().add(endWarmupKF);
        autoTL.getKeyFrames().add(endTestKF);

        Timeline fpsTimeline = new Timeline();
        fpsTimeline.setCycleCount(Timeline.INDEFINITE);
        KeyFrame fpsKeyFrame = new KeyFrame(new Duration(1000), new EventHandler<ActionEvent>() {
            @Override public void handle(ActionEvent e) {
                String fpsText = String.format("FPS: %.2f", perfTracker.getInstantFPS());
                System.out.println(fpsText);
            }
        });
        fpsTimeline.getKeyFrames().add(fpsKeyFrame);
        

        for (int i=0; i<40; i++) {
            Label l1 = new Label("Label 1 : " + i);
            Label l2 = new Label("Label 2 : " + i);
                  
            l1.setLayoutY(i*15);
            l2.setLayoutY(i*15);
            
            labels1.add(l1);
            labels2.add(l2);            
        }
        
        rootGroup.getChildren().addAll(labels1);
        
        AnimationTimer PerfRunner = new AnimationTimer() {
            boolean stop = false;

            @Override
            public void handle(long l) {

                stage.setTitle("Iters " + iters);
                rootGroup.getChildren().clear();
                if (iters % 2 == 0) {
                    rootGroup.getChildren().addAll(labels1);
                } else {
                    rootGroup.getChildren().addAll(labels2);
                }    
                iters++;
            }
        };

        autoTL.play();
        fpsTimeline.play();
        PerfRunner.start();
    }


    public static void main(String[] args) {
        for (int index = 0; index < args.length; index++) {
            String arg = args[index];            
        }
        
        Application.launch(LabelPerf.class, args);
    }

}