# Class Stage2Controller

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

public class Stage2Controller extends Object
Controller for the ship assembly phase (Stage 2) of the game. Manages component selection from face-up cards or deck, placement on the ship grid, viewing opponent ships, and timer management for level 2 games. Coordinates between the main assembly view and the selected tile controller for component manipulation.

- ## Field Summary

  Fields
  Modifier and Type
  Field
  Description
  `javafx.scene.control.Button`
  `endBuildButton`
  `javafx.scene.control.Button`
  `refresh`
  `javafx.scene.control.Button`
  `use1Button`
  `javafx.scene.control.Button`
  `use2Button`

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `Stage2Controller()`

- ## Method Summary

  Modifier and Type
  Method
  Description
  `void`
  `clearComponentImage(int x, int y)`
  Clears the component image at the specified grid position.
  `void`
  `deck1Button(javafx.event.ActionEvent actionEvent)`
  Handles deck 1 button click to view the first sub-deck.
  `void`
  `deck2Button(javafx.event.ActionEvent actionEvent)`
  Handles deck 2 button click to view the second sub-deck.
  `void`
  `deck3Button(javafx.event.ActionEvent actionEvent)`
  Handles deck 3 button click to view the third sub-deck.
  `void`
  `enableDrawButton()`
  Enables the draw component button.
  `void`
  `finishComponentError(String message)`
  Displays an error message and disables the draw component button.
  `ClientModel`
  `getClient()`
  Gets the client model instance.
  `VirtualServer`
  `getServer()`
  Gets the virtual server instance.
  `void`
  `placeComponentImage(int x, int y, Component component)`
  Places a component image at the specified grid position.
  `void`
  `refreshListComponent(javafx.event.ActionEvent actionEvent)`
  Refreshes the face-up component list.
  `void`
  `registerGridImageView(String key, javafx.scene.image.ImageView imageView)`
  Registers an ImageView in the grid images map.
  `void`
  `setGui(Gui gui)`
  Sets the GUI instance for this controller.
  `void`
  `setGuiRenderer(GuiRenderer guiRenderer)`
  Sets the GUI renderer for this controller.
  `void`
  `setLevel(int level)`
  Sets the game level for this controller.
  `void`
  `setSelectedTileController(SelectedTileController selectedTileController)`
  Sets the SelectedTileController for this controller.
  `void`
  `setServerAndClient(VirtualServer server, ClientModel clientModel)`
  Sets the server and client model for this controller.
  `void`
  `setShip(Ship ship)`
  Sets the ship for this controller and displays the main cabin.
  `void`
  `showLevel2Ship()`
  Shows the level 2 ship image and hides the level 1 ship image.
  `void`
  `showOpponentShip(Ship ship)`
  Shows an opponent's ship in a new window.
  `void`
  `startHourGlass()`
  Starts the hourglass timer on the server.
  `void`
  `subDeck(List<Card> subDeck)`
  Shows the sub-deck cards in a new window.
  `void`
  `updateFaceUpComponentList(List<Component> components)`
  Updates the face-up component list view with the provided components.
  `void`
  `updateShipView()`
  Updates the ship view based on the current game level.
  `void`
  `updateTimer(int counter, int seconds)`
  Updates the timer display and starts the countdown.

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

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

- ## Field Details

  - ### use2Button

    public javafx.scene.control.Button use2Button

  - ### use1Button

    public javafx.scene.control.Button use1Button

  - ### refresh

    public javafx.scene.control.Button refresh

  - ### endBuildButton

    public javafx.scene.control.Button endBuildButton

- ## Constructor Details

  - ### Stage2Controller

    public Stage2Controller()

- ## Method Details

  - ### setSelectedTileController

    public void setSelectedTileController(SelectedTileController selectedTileController)
    Sets the SelectedTileController for this controller.
    Parameters:
      `selectedTileController` - the SelectedTileController to set

  - ### setServerAndClient

    public void setServerAndClient(VirtualServer server, ClientModel clientModel)
    Sets the server and client model for this controller. Initializes the view ship buttons.
    Parameters:
      `server` - the virtual server for network communication
      `clientModel` - the client model containing game state

  - ### getServer

    public VirtualServer getServer()
    Gets the virtual server instance.
    Returns:
      the current VirtualServer

  - ### getClient

    public ClientModel getClient()
    Gets the client model instance.
    Returns:
      the current ClientModel

  - ### setLevel

    public void setLevel(int level)
    Sets the game level for this controller.
    Parameters:
      `level` - the game level (1 or 2)

  - ### showLevel2Ship

    public void showLevel2Ship()
    Shows the level 2 ship image and hides the level 1 ship image.

  - ### updateShipView

    public void updateShipView()
    Updates the ship view based on the current game level. Shows appropriate ship image and enables level 2 specific controls.

  - ### setShip

    public void setShip(Ship ship) throws IOException
    Sets the ship for this controller and displays the main cabin.
    Parameters:
      `ship` - the ship to set
    Throws:
      `IOException` - if the main cabin image cannot be loaded

  - ### registerGridImageView

    public void registerGridImageView(String key, javafx.scene.image.ImageView imageView)
    Registers an ImageView in the grid images map.
    Parameters:
      `key` - the grid position key (format: "x,y")
      `imageView` - the ImageView to register

  - ### placeComponentImage

    public void placeComponentImage(int x, int y, Component component)
    Places a component image at the specified grid position.
    Parameters:
      `x` - the x-coordinate
      `y` - the y-coordinate
      `component` - the component to place

  - ### setGuiRenderer

    public void setGuiRenderer(GuiRenderer guiRenderer)
    Sets the GUI renderer for this controller.
    Parameters:
      `guiRenderer` - the GuiRenderer to set

  - ### enableDrawButton

    public void enableDrawButton()
    Enables the draw component button.

  - ### clearComponentImage

    public void clearComponentImage(int x, int y)
    Clears the component image at the specified grid position.
    Parameters:
      `x` - the x-coordinate
      `y` - the y-coordinate

  - ### updateFaceUpComponentList

    public void updateFaceUpComponentList(List<Component> components)
    Updates the face-up component list view with the provided components.
    Parameters:
      `components` - the list of face-up components to display

  - ### updateTimer

    public void updateTimer(int counter, int seconds)
    Updates the timer display and starts the countdown.
    Parameters:
      `counter` - the current timer count
      `seconds` - the seconds remaining

  - ### startHourGlass

    public void startHourGlass() throws Exception
    Starts the hourglass timer on the server.
    Throws:
      `Exception` - if the server request fails

  - ### setGui

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

  - ### refreshListComponent

    public void refreshListComponent(javafx.event.ActionEvent actionEvent)
    Refreshes the face-up component list.
    Parameters:
      `actionEvent` - the action event

  - ### showOpponentShip

    public void showOpponentShip(Ship ship)
    Shows an opponent's ship in a new window.
    Parameters:
      `ship` - the opponent's ship to display

  - ### subDeck

    public void subDeck(List<Card> subDeck)
    Shows the sub-deck cards in a new window. Disables all controls while viewing the sub-deck.
    Parameters:
      `subDeck` - the list of cards in the sub-deck

  - ### deck1Button

    public void deck1Button(javafx.event.ActionEvent actionEvent) throws IOException
    Handles deck 1 button click to view the first sub-deck.
    Parameters:
      `actionEvent` - the action event
    Throws:
      `IOException` - if the request fails

  - ### deck2Button

    public void deck2Button(javafx.event.ActionEvent actionEvent) throws IOException
    Handles deck 2 button click to view the second sub-deck.
    Parameters:
      `actionEvent` - the action event
    Throws:
      `IOException` - if the request fails

  - ### deck3Button

    public void deck3Button(javafx.event.ActionEvent actionEvent) throws IOException
    Handles deck 3 button click to view the third sub-deck.
    Parameters:
      `actionEvent` - the action event
    Throws:
      `IOException` - if the request fails

  - ### finishComponentError

    public void finishComponentError(String message)
    Displays an error message and disables the draw component button.
    Parameters:
      `message` - the error message to display