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

import javafx.application.Platform;
import javafx.scene.control.Separator;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import uk.mgrove.ac.soton.comp1206.component.*;
import uk.mgrove.ac.soton.comp1206.game.MultiplayerGame;
import uk.mgrove.ac.soton.comp1206.ui.GameWindow;

/**
 * Class for multiplayer game scene
 */
public class MultiplayerScene extends ChallengeScene {

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

    /**
     * Chat container
     */
    private Chat chat;

    /**
     * Live leaderboard
     */
    private Leaderboard leaderboard;

    /**
     * Create a new multiplayer challenge scene
     *
     * @param gameWindow the Game Window
     */
    public MultiplayerScene(GameWindow gameWindow) {
        super(gameWindow);
    }

    /**
     * Set up the game object and model
     */
    @Override
    public void setupGame() {
        logger.info("Starting a new challenge");

        //Start new game
        game = new MultiplayerGame(5, 5, gameWindow.getCommunicator());
        game.setOnGameFail(() -> Platform.runLater(gameWindow::startMenu));

        gameWindow.getCommunicator().send("SCORES");
    }

    /**
     * Build the Multiplayer Challenge window
     */
    @Override
    public void build() {
        super.build();
        var leftMenu = new VBox();
        leftMenu.setSpacing(8);
        leftMenu.setMaxWidth(200);
        leftMenu.setPrefWidth(200);
        chat = new Chat(gameWindow.getCommunicator(), true);
        chat.setChatFocusTraversable(false);
        leaderboard = new Leaderboard("Leaderboard");
        var chatTitle = new Text("Chat");
        chatTitle.getStyleClass().add("heading");
        leftMenu.getChildren().addAll(leaderboard, new Separator(), chatTitle, chat);
        mainPane.setLeft(leftMenu);
        leaderboard.bindLeaderboardScores(game.leaderboardScoresProperty());
        mainPane.requestFocus();
    }

}
