"""The main menu state"""

from __future__ import annotations

import g
import game.screens.menus
import game.world_tools
from game.screens import Reset, ScreenResult
from game.screens.game_screens import MainScreen

class MainMenu(game.screens.menus.ListMenu):
    """Main/escape menu."""
    __slots__ = ()

    def __init__(self) -> None:
        """Initialize the main menu."""
        items = [
            game.screens.menus.SelectItem("New game", self.new_game),
            game.screens.menus.SelectItem("Quit", self.quit),
        ]
        if hasattr(g, "world"):
            items.insert(0, game.screens.menus.SelectItem("Continue", self.continue_))

        super().__init__(
            items=tuple(items),
            selected=0,
            x=5,
            y=5,
        )

    @staticmethod
    def continue_() -> ScreenResult:
        """Return to the game."""
        return Reset(MainScreen())

    @staticmethod
    def new_game() -> ScreenResult:
        """Begin a new game."""
        g.world = game.world_tools.new_world()
        return Reset(MainScreen())

    @staticmethod
    def quit() -> ScreenResult:
        """Close the program."""
        raise SystemExit()
