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

import javafx.geometry.Pos;
import javafx.scene.image.ImageView;
import javafx.scene.input.KeyCode;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import javafx.scene.text.TextAlignment;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import uk.mgrove.ac.soton.comp1206.component.PieceBoard;
import uk.mgrove.ac.soton.comp1206.game.GamePiece;
import uk.mgrove.ac.soton.comp1206.ui.GamePane;
import uk.mgrove.ac.soton.comp1206.ui.GameWindow;

/**
 * Scene to show game instructions
 */
public class InstructionsScene extends BaseScene {

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

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

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

    // exit instructions when escape key pressed
    scene.setOnKeyPressed((event) -> {
      if (event.getCode() == KeyCode.ESCAPE) {
        gameWindow.startMenu();
      }
    });
  }

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

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

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

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

    //Awful title
    var title = new Text("How to Play");
    title.getStyleClass().add("title");
    mainPane.setTop(title);
    BorderPane.setAlignment(title, Pos.TOP_CENTER);

    var instructionsText = new Text("TetrECS is a fast-paced gravity-free block placement game, where you must survive by clearing rows through careful placement of the upcoming blocks before the time runs out. Lose all 3 lives and you're destroyed!");
    instructionsText.getStyleClass().add("instructions");
    instructionsText.setWrappingWidth(700);
    instructionsText.setTextAlignment(TextAlignment.CENTER);

    var instructions = new ImageView(getClass().getResource("/images/Instructions.png").toExternalForm());
    instructions.setPreserveRatio(true);
    instructions.setFitHeight(gameWindow.getHeight()*0.5);

    var instructionsContainer = new VBox(instructionsText, instructions);
    instructionsContainer.setAlignment(Pos.TOP_CENTER);
    mainPane.setCenter(instructionsContainer);

    var pieceGrid = new GridPane();
    pieceGrid.setVgap(8);
    pieceGrid.setHgap(8);
    for (var i = 0; i < GamePiece.PIECES; i++) {
      var pieceBoard = new PieceBoard(48, 48);
      pieceBoard.displayPiece(GamePiece.createPiece(i));
      pieceGrid.add(pieceBoard, i % 5, Math.floorDiv(i,5));
    }

    var allPieces = new VBox();
    var piecesHeader = new Text("Game Pieces");
    piecesHeader.getStyleClass().add("title");
    allPieces.getChildren().addAll(piecesHeader,pieceGrid);
    allPieces.setAlignment(Pos.BOTTOM_CENTER);
    pieceGrid.setAlignment(Pos.BOTTOM_CENTER);

    mainPane.setBottom(allPieces);
    BorderPane.setAlignment(allPieces, Pos.BOTTOM_CENTER);
  }

}