package uk.mgrove.ac.soton.comp1206.scene;

import javafx.animation.FadeTransition;
import javafx.application.Platform;
import javafx.geometry.Pos;
import javafx.scene.image.ImageView;
import javafx.scene.layout.StackPane;
import javafx.util.Duration;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import uk.mgrove.ac.soton.comp1206.ui.GamePane;
import uk.mgrove.ac.soton.comp1206.ui.GameWindow;
import uk.mgrove.ac.soton.comp1206.util.Multimedia;

import java.util.Timer;
import java.util.TimerTask;

public class LoadingScene extends BaseScene {

    /**
     * Logger
     */
    private static final Logger logger = LogManager.getLogger(LoadingScene.class);

    /**
     * Fade transition for ECS Games logo
     */
    private FadeTransition logoFade;

    /**
     * Create a new scene, passing in the GameWindow the scene will be displayed in
     *
     * @param gameWindow the game window
     */
    public LoadingScene(GameWindow gameWindow) {
        super(gameWindow);
    }

    /**
     * Build the layout of the scene
     */
    @Override
    public void build() {
        logger.info("Building " + this.getClass().getName());

        root = new GamePane(gameWindow.getWidth(),gameWindow.getHeight());

        var logo = new ImageView(getClass().getResource("/images/ECSGames.png").toExternalForm());
        logo.setPreserveRatio(true);
        logo.setFitWidth(380);
        root.getChildren().add(logo);
        StackPane.setAlignment(logo, Pos.CENTER);

        logoFade = new FadeTransition(Duration.millis(2000), logo);
        logoFade.setFromValue(0);
        logoFade.setToValue(1);

        logo.setOpacity(0);
    }

    /**
     * Initialise this scene. Called after creation
     */
    @Override
    public void initialise() {
        logger.info("Initialising " + this.getClass().getName());

        logoFade.play();
        Multimedia.playAudio("sounds/intro.mp3");

        TimerTask loadMenuTimerTask = new TimerTask() {
            @Override
            public void run() {
                Platform.runLater(gameWindow::startMenu);
            }
        };
        var loadMenuTimer = new Timer("Timer");
        loadMenuTimer.schedule(loadMenuTimerTask, 4000);
    }

}
