# Class GuiRenderer

java.lang.Object
it.polimi.ingsw.is25am31.view.gui.GuiRenderer

All Implemented Interfaces:
  `UiRenderer`

public class GuiRenderer extends Object implements UiRenderer
Singleton implementation of the UiRenderer interface for JavaFX-based GUI rendering. Serves as the bridge between the game logic and the JavaFX UI, handling all UI updates and user interactions. Ensures thread-safe UI updates using Platform.runLater and manages the various game phase transitions and visual updates.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `void`
  `customCardPrintMyTurn(int turnType)`
  Handles custom card print for the player's turn.
  `void`
  `customCardPrintNotMyTurn(String currentPlayer)`
  Handles custom card print when it's not the player's turn.
  `void`
  `drawShip(Ship ship)`
  Draws a ship on the appropriate view based on the current context.
  `void`
  `effectResolution()`
  Handles effect resolution phase by updating the turn message.
  `void`
  `endAssemblingPhase()`
  Handles the end of the assembling phase.
  `static GuiRenderer`
  `getInstance()`
  Gets the singleton instance of GuiRenderer.
  `void`
  `handleConnectionResponse(ConnectionResponse response)`
  Handles the connection response from the server.
  `void`
  `handleUpdateMessage(UpdateMessage msg)`
  Handles update messages from the server.
  `boolean`
  `hasCrewPlacementFailed()`
  Checks if the last crew placement attempt failed.
  `void`
  `newLeader(boolean isNewLeader)`
  Updates the UI when leadership changes.
  `void`
  `newPlayerJoined(String nickname)`
  Notifies when a new player joins the game.
  `void`
  `notifyDefeat(int loseType)`
  Notifies the player of their defeat and switches to spectator mode.
  `void`
  `onError(String message)`
  Displays an error message in the card manager.
  `void`
  `onInfo(String message)`
  Displays an informational message in the card manager.
  `void`
  `printPromptSymbol()`
  Prints the prompt symbol.
  `void`
  `reportException(Exception e)`
  Reports an exception to the appropriate UI component.
  `void`
  `roundResult(boolean roundWon)`
  Shows the result of a round (won or lost).
  `void`
  `runUi(VirtualView client, VirtualServer server, ClientModel clientModel)`
  Initializes and runs the UI with the provided client, server, and model.
  `void`
  `setErrorLabel(javafx.scene.control.Label errorLabel)`
  Sets the error label for displaying error messages.
  `void`
  `setGuiController(Gui gui)`
  Sets the GUI controller for this renderer.
  `void`
  `setGuiController(Object controller)`
  Sets the GUI controller object.
  `void`
  `setLastCrewPlacementFailed(boolean failed)`
  Sets whether the last crew placement attempt failed.
  `void`
  `setMessageLabel(javafx.scene.control.Label label)`
  Sets the message label for general messages.
  `void`
  `setNickname()`
  Prompts the player to set their nickname.
  `void`
  `setPromptLabel(javafx.scene.control.Label promptLabel)`
  Sets the prompt label for displaying messages.
  `void`
  `setUUID(UUID clientId)`
  Sets the client UUID.
  `void`
  `showCard(Card card)`
  Displays a card in the card manager window.
  `void`
  `showDiceResult(int diceResult)`
  Shows the result of a dice roll.
  `void`
  `showFaceUpComponentList(List<Component> components)`
  Updates the face-up component list display.
  `void`
  `showFinalRanking(Map<Player,Integer> ranking)`
  Shows the final ranking screen with player scores.
  `void`
  `showGameList(List<Integer> gameList)`
  Shows the list of available games in the game selector.
  `void`
  `showPlayersList(List<String> players)`
  Shows the list of players in the game.
  `void`
  `showRocketsPosition(List<Player> players, int level)`
  Shows the rockets' positions on the track.
  `void`
  `showsDrawnComponent(Component component)`
  Displays a drawn component in the selected tile view.
  `void`
  `showSecondsLeft(int count, int secondsLeft)`
  Shows the seconds left for a timer.
  `void`
  `showSecondsLeftForGui(int count, int secondsLeft)`
  Shows the countdown timer in the GUI.
  `void`
  `showSubDeck(List<Card> subDeck)`
  Shows the sub-deck of cards.
  `void`
  `startAssemblingPhase()`
  Starts the assembling phase by loading the stage 2 scene.
  `void`
  `startCrewPlacingPhase(Ship ship, Map<Integer,Integer> positions)`
  Starts the crew placement phase by loading the appropriate scene.
  `void`
  `startFlightPhase(Ship ship, Map<Integer,Integer> positions)`
  Starts the flight phase by closing the crew selection window and updating positions.

  ### Methods inherited from class java.lang.Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Method Details

  - ### getInstance

    public static GuiRenderer getInstance()
    Gets the singleton instance of GuiRenderer. Creates a new instance if one doesn't exist and launches the JavaFX application.
    Returns:
      the singleton instance of GuiRenderer

  - ### setGuiController

    public void setGuiController(Gui gui)
    Sets the GUI controller for this renderer.
    Parameters:
      `gui` - the GUI instance to set

  - ### runUi

    public void runUi(VirtualView client, VirtualServer server, ClientModel clientModel)
    Initializes and runs the UI with the provided client, server, and model.
    Specified by:
      `runUi` in interface `UiRenderer`
    Parameters:
      `client` - the virtual view representing the client interface
      `server` - the virtual server for network communication
      `clientModel` - the client model containing game state

  - ### showsDrawnComponent

    public void showsDrawnComponent(Component component)
    Displays a drawn component in the selected tile view.
    Specified by:
      `showsDrawnComponent` in interface `UiRenderer`
    Parameters:
      `component` - the component to display

  - ### showFaceUpComponentList

    public void showFaceUpComponentList(List<Component> components)
    Updates the face-up component list display.
    Specified by:
      `showFaceUpComponentList` in interface `UiRenderer`
    Parameters:
      `components` - the list of face-up components to display

  - ### drawShip

    public void drawShip(Ship ship) throws Exception
    Draws a ship on the appropriate view based on the current context. Shows opponent's ship in a separate window or updates the player's own ship view.
    Specified by:
      `drawShip` in interface `UiRenderer`
    Parameters:
      `ship` - the ship to draw
    Throws:
      `Exception` - if drawing fails

  - ### showRocketsPosition

    public void showRocketsPosition(List<Player> players, int level)
    Shows the rockets' positions on the track. Currently not implemented for GUI.
    Specified by:
      `showRocketsPosition` in interface `UiRenderer`
    Parameters:
      `players` - the list of players
      `level` - the game level

  - ### showCard

    public void showCard(Card card)
    Displays a card in the card manager window. Creates a new card manager window if one doesn't exist.
    Specified by:
      `showCard` in interface `UiRenderer`
    Parameters:
      `card` - the card to display

  - ### startCrewPlacingPhase

    public void startCrewPlacingPhase(Ship ship, Map<Integer,Integer> positions)
    Starts the crew placement phase by loading the appropriate scene.
    Specified by:
      `startCrewPlacingPhase` in interface `UiRenderer`
    Parameters:
      `ship` - the player's ship
      `positions` - the component positions map

  - ### endAssemblingPhase

    public void endAssemblingPhase()
    Handles the end of the assembling phase. Currently not implemented for GUI.
    Specified by:
      `endAssemblingPhase` in interface `UiRenderer`

  - ### startAssemblingPhase

    public void startAssemblingPhase()
    Starts the assembling phase by loading the stage 2 scene.
    Specified by:
      `startAssemblingPhase` in interface `UiRenderer`

  - ### showFinalRanking

    public void showFinalRanking(Map<Player,Integer> ranking)
    Shows the final ranking screen with player scores. Closes any open card manager or dashboard windows before showing rankings.
    Specified by:
      `showFinalRanking` in interface `UiRenderer`
    Parameters:
      `ranking` - the final ranking map with players and their scores

  - ### notifyDefeat

    public void notifyDefeat(int loseType)
    Notifies the player of their defeat and switches to spectator mode.
    Specified by:
      `notifyDefeat` in interface `UiRenderer`
    Parameters:
      `loseType` - the reason code for defeat

  - ### customCardPrintMyTurn

    public void customCardPrintMyTurn(int turnType)
    Handles custom card print for the player's turn.
    Specified by:
      `customCardPrintMyTurn` in interface `UiRenderer`
    Parameters:
      `turnType` - the type of turn

  - ### customCardPrintNotMyTurn

    public void customCardPrintNotMyTurn(String currentPlayer)
    Handles custom card print when it's not the player's turn.
    Specified by:
      `customCardPrintNotMyTurn` in interface `UiRenderer`
    Parameters:
      `currentPlayer` - the nickname of the current active player

  - ### setPromptLabel

    public void setPromptLabel(javafx.scene.control.Label promptLabel)
    Sets the prompt label for displaying messages.
    Parameters:
      `promptLabel` - the label to set

  - ### setGuiController

    public void setGuiController(Object controller)
    Sets the GUI controller object.
    Parameters:
      `controller` - the controller to set

  - ### setErrorLabel

    public void setErrorLabel(javafx.scene.control.Label errorLabel)
    Sets the error label for displaying error messages.
    Parameters:
      `errorLabel` - the label to set

  - ### setMessageLabel

    public void setMessageLabel(javafx.scene.control.Label label)
    Sets the message label for general messages.
    Parameters:
      `label` - the label to set

  - ### setLastCrewPlacementFailed

    public void setLastCrewPlacementFailed(boolean failed)
    Sets whether the last crew placement attempt failed.
    Parameters:
      `failed` - true if placement failed, false otherwise

  - ### hasCrewPlacementFailed

    public boolean hasCrewPlacementFailed()
    Checks if the last crew placement attempt failed.
    Returns:
      true if the last placement failed, false otherwise

  - ### reportException

    public void reportException(Exception e)
    Reports an exception to the appropriate UI component. Displays error messages in various labels depending on the current context.
    Specified by:
      `reportException` in interface `UiRenderer`
    Parameters:
      `e` - the exception to report

  - ### handleConnectionResponse

    public void handleConnectionResponse(ConnectionResponse response)
    Handles the connection response from the server.
    Specified by:
      `handleConnectionResponse` in interface `UiRenderer`
    Parameters:
      `response` - the connection response to handle

  - ### showPlayersList

    public void showPlayersList(List<String> players)
    Shows the list of players in the game. Currently not implemented for GUI.
    Specified by:
      `showPlayersList` in interface `UiRenderer`
    Parameters:
      `players` - the list of player nicknames

  - ### newPlayerJoined

    public void newPlayerJoined(String nickname)
    Notifies when a new player joins the game. Currently not implemented for GUI.
    Specified by:
      `newPlayerJoined` in interface `UiRenderer`
    Parameters:
      `nickname` - the nickname of the new player

  - ### showDiceResult

    public void showDiceResult(int diceResult)
    Shows the result of a dice roll.
    Specified by:
      `showDiceResult` in interface `UiRenderer`
    Parameters:
      `diceResult` - the dice roll result (1-6)

  - ### roundResult

    public void roundResult(boolean roundWon)
    Shows the result of a round (won or lost).
    Specified by:
      `roundResult` in interface `UiRenderer`
    Parameters:
      `roundWon` - true if the player won the round, false if lost

  - ### newLeader

    public void newLeader(boolean isNewLeader)
    Updates the UI when leadership changes. Loads or updates the appropriate dashboard based on game level.
    Specified by:
      `newLeader` in interface `UiRenderer`
    Parameters:
      `isNewLeader` - true if the player is the new leader, false otherwise

  - ### onInfo

    public void onInfo(String message)
    Displays an informational message in the card manager.
    Specified by:
      `onInfo` in interface `UiRenderer`
    Parameters:
      `message` - the information message to display

  - ### onError

    public void onError(String message)
    Displays an error message in the card manager.
    Specified by:
      `onError` in interface `UiRenderer`
    Parameters:
      `message` - the error message to display

  - ### setNickname

    public void setNickname()
    Prompts the player to set their nickname.
    Specified by:
      `setNickname` in interface `UiRenderer`

  - ### showSecondsLeftForGui

    public void showSecondsLeftForGui(int count, int secondsLeft)
    Shows the countdown timer in the GUI.
    Specified by:
      `showSecondsLeftForGui` in interface `UiRenderer`
    Parameters:
      `count` - the current count
      `secondsLeft` - the seconds remaining

  - ### showSubDeck

    public void showSubDeck(List<Card> subDeck)
    Shows the sub-deck of cards.
    Specified by:
      `showSubDeck` in interface `UiRenderer`
    Parameters:
      `subDeck` - the list of cards in the sub-deck

  - ### handleUpdateMessage

    public void handleUpdateMessage(UpdateMessage msg)
    Handles update messages from the server.
    Specified by:
      `handleUpdateMessage` in interface `UiRenderer`
    Parameters:
      `msg` - the update message to handle

  - ### printPromptSymbol

    public void printPromptSymbol() throws InterruptedException
    Prints the prompt symbol. Not implemented for GUI.
    Specified by:
      `printPromptSymbol` in interface `UiRenderer`
    Throws:
      `InterruptedException` - if interrupted

  - ### setUUID

    public void setUUID(UUID clientId)
    Sets the client UUID. Not implemented for GUI.
    Specified by:
      `setUUID` in interface `UiRenderer`
    Parameters:
      `clientId` - the client UUID

  - ### effectResolution

    public void effectResolution()
    Handles effect resolution phase by updating the turn message.
    Specified by:
      `effectResolution` in interface `UiRenderer`

  - ### showSecondsLeft

    public void showSecondsLeft(int count, int secondsLeft)
    Shows the seconds left for a timer. Not implemented for GUI.
    Specified by:
      `showSecondsLeft` in interface `UiRenderer`
    Parameters:
      `count` - the current count
      `secondsLeft` - the seconds remaining

  - ### startFlightPhase

    public void startFlightPhase(Ship ship, Map<Integer,Integer> positions)
    Starts the flight phase by closing the crew selection window and updating positions.
    Specified by:
      `startFlightPhase` in interface `UiRenderer`
    Parameters:
      `ship` - the player's ship
      `positions` - the rocket positions for level 1 games

  - ### showGameList

    public void showGameList(List<Integer> gameList)
    Shows the list of available games in the game selector.
    Specified by:
      `showGameList` in interface `UiRenderer`
    Parameters:
      `gameList` - the list of game IDs