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

import javafx.animation.Animation;
import javafx.animation.RotateTransition;
import javafx.geometry.Pos;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.*;
import javafx.scene.text.Text;
import javafx.util.Duration;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import uk.mgrove.ac.soton.comp1206.App;
import uk.mgrove.ac.soton.comp1206.ui.GamePane;
import uk.mgrove.ac.soton.comp1206.ui.GameWindow;
import uk.mgrove.ac.soton.comp1206.util.Multimedia;

/**
 * The main menu of the game. Provides a gateway to the rest of the game.
 */
public class MenuScene extends BaseScene {

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

    /**
     * Rotate transition for TetrECS logo
     */
    private RotateTransition titleAnimation;

    /**
     * Create a new menu scene
     * @param gameWindow the Game Window this will be displayed in
     */
    public MenuScene(GameWindow gameWindow) {
        super(gameWindow);
        logger.info("Creating Menu Scene");
    }

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

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

        var menuPane = new StackPane();
        menuPane.setMaxWidth(gameWindow.getWidth());
        menuPane.setMaxHeight(gameWindow.getHeight());
        menuPane.getStyleClass().add("menu-background");
        root.getChildren().add(menuPane);

        var mainPane = new BorderPane();
        menuPane.getChildren().add(mainPane);

        var title = new ImageView(getClass().getResource("/images/TetrECS.png").toExternalForm());
        title.setPreserveRatio(true);
        title.setFitWidth(400);
        titleAnimation = new RotateTransition(Duration.millis(2000), title);
        titleAnimation.setFromAngle(7);
        titleAnimation.setToAngle(-7);
        titleAnimation.setAutoReverse(true);
        titleAnimation.setCycleCount(Animation.INDEFINITE);

        var separator = new VBox();
        separator.setPrefHeight(70);

        var menuItems = new VBox();
        menuItems.setAlignment(Pos.CENTER);
        var singlePlayerButton = new Text("Single Player");
        var multiPlayerButton = new Text("Multiplayer");
        var howToPlayButton = new Text("How to Play");
        var exitButton = new Text("Exit");

        menuItems.getChildren().addAll(title,separator,singlePlayerButton,multiPlayerButton,howToPlayButton,exitButton);
        mainPane.setCenter(menuItems);

        Multimedia.playMusic("music/menu.mp3");

        //Bind the button action to the startGame method in the menu
        singlePlayerButton.setOnMouseClicked(this::startSinglePlayer);
        singlePlayerButton.getStyleClass().add("menuItem");
        multiPlayerButton.setOnMouseClicked(this::startMultiplayer);
        multiPlayerButton.getStyleClass().add("menuItem");
        howToPlayButton.setOnMouseClicked(this::showInstructions);
        howToPlayButton.getStyleClass().add("menuItem");
        exitButton.setOnMouseClicked(this::shutdown);
        exitButton.getStyleClass().add("menuItem");
    }

    /**
     * Initialise the menu
     */
    @Override
    public void initialise() {
        logger.info("Initialising " + this.getClass().getName());

        titleAnimation.play();
    }

    /**
     * Handle when the Start Single Player Game button is pressed
     * @param event event
     */
    private void startSinglePlayer(MouseEvent event) {
        titleAnimation.stop();
        gameWindow.startChallenge();
    }

    /**
     * Handle when the Start Multiplayer Game button is pressed
     * @param event event
     */
    private void startMultiplayer(MouseEvent event) {
        titleAnimation.stop();
        gameWindow.startMultiplayer();
    }

    /**
     * Handle when the How to Play button is pressed
     * @param event event
     */
    private void showInstructions(MouseEvent event) {
        titleAnimation.stop();
        gameWindow.startInstructions();
    }

    /**
     * Handle when the exit button is pressed
     * @param event event
     */
    private void shutdown(MouseEvent event) {
        App.getInstance().shutdown();
    }

}
