package edu.bloomu.ch4a;

import javafx.animation.ParallelTransition;
import javafx.animation.ScaleTransition;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.util.Duration;

/**
* Animates colorful text with a scale transition.
*
* @author Drue Coles
*/
public class Emotions extends Application {

    @Override
    public void start(Stage stage) {
        StackPane root = new StackPane();
        final int width = 500;
        final int height = 500;
        Scene scene = new Scene(root, width, height, Color.BLACK);

        Text smileText = colorfulText("\u263a", Color.YELLOW);
        Text frownText = colorfulText("\u2639", Color.LIME);
        smileText.setTranslateY(-50);
        frownText.setTranslateY(-50);
        
        double stretchFactor = 3;
        setScaleTransition(smileText, -stretchFactor, stretchFactor, 1500);
        setScaleTransition(frownText, stretchFactor, stretchFactor, 800);
        root.getChildren().addAll(smileText, frownText);
        stage.setTitle("Emotions");
        stage.setScene(scene);
        stage.show();
        stage.setAlwaysOnTop(true);
    }
    
     /**
     * Returns a styled text node constructed with a given string.
     */
    private static Text colorfulText(String str, Color color) {
        Text text = new Text(str);
        text.setFill(color);
        String style = "-fx-font-size: 200px; -fx-font-weight: bold";
        text.setStyle(style);
        text.setStroke(color.darker().darker());
        text.setStrokeWidth(3);
        return text;
    }

    /**
     * Applies a scale transition to a node.
     *
     * @param node the node to scale
     * @param xStretch the horizontal stretch factor
     * @param yStretch the vertical stretch factor
     * @param n duration of each cycle in milliseconds
     */
    private static void setScaleTransition(Node node, double xStretch,
                                           double yStretch, int n) {
        ScaleTransition scaleTransition = new ScaleTransition(Duration.millis(n));
        scaleTransition.setToX(xStretch);
        scaleTransition.setToY(yStretch);

        ParallelTransition pTrans = new ParallelTransition(node, scaleTransition);
        pTrans.setCycleCount(Timeline.INDEFINITE);
        pTrans.setAutoReverse(true);
        pTrans.play();
    }

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