"""The main menu state"""

from __future__ import annotations

import g
import game.menus
import game.world_tools
from game.state import Reset, StateResult
from game.states.InGame import InGame

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

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

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

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

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

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