#!/usr/bin/env python3
"""The Hitchhiker's Guide to the Galaxy — Python text adventure engine."""

from h2g2.engine.world import World
from h2g2.engine.state import GameState
from h2g2.engine.output import Output
from h2g2.engine.clock import Clock
from h2g2.engine.parser import Parser
from h2g2.engine.loop import GameLoop

# Import verb handlers (registers them via decorators)
import h2g2.engine.verbs  # noqa: F401

# Import content modules
from h2g2.content import globals_content, earth, vogon, heart, unearth, dark


def main() -> None:
    # Build the world
    world = World()
    globals_content.register(world)
    earth.register(world)

    # Initialize engine
    output = Output()
    clock = Clock()
    state = GameState(world, output, clock)

    # Set starting conditions
    state.protagonist = world.protagonist
    state.here = world.get_room("BEDROOM")
    state.winner = world.protagonist
    state.lying_down = True  # start in bed

    # Initialize content-specific flags
    state.flags["headache"] = True
    state.flags["groggy"] = False
    state.flags["groggy_counter"] = 0
    state.flags["house_demolished"] = False
    state.flags["earth_demolished"] = False
    state.flags["in_front_of_bulldozer"] = False
    state.flags["ford_arrived"] = False
    state.flags["ford_has_satchel"] = True
    state.flags["prosser_in_mud"] = False
    state.flags["beer_counter"] = 0
    state.flags["babel_fish_in_ear"] = False
    state.flags["poem_enjoyed"] = False
    state.flags["holding_no_tea"] = True
    state.flags["dead_counter"] = 0
    state.flags["vogon_prob"] = 100
    state.flags["heart_prob"] = 0
    state.flags["traal_prob"] = 60
    state.flags["fleet_prob"] = 0
    state.flags["whale_prob"] = 0

    # Register content that needs state access
    vogon.register(world, state)
    heart.register(world, state)
    unearth.register(world, state)
    dark.register(world, state)

    # Register inventory extras
    state.inventory_extras.append(
        lambda s: "a splitting headache" if s.flags.get("headache") else None
    )
    state.inventory_extras.append(
        lambda s: "no tea" if s.flags.get("holding_no_tea") else None
    )

    # Banner
    output.tell(
        "\n *** THE HITCHHIKER'S GUIDE TO THE GALAXY: "
        "Interactive Science Fiction ***\n\n"
        "Copyright (c) 1984 by Infocom, Inc. All Rights Reserved.\n"
        "Python engine by Claude.\n\n"
    )

    # Opening text
    output.tell(
        "You wake up. The room is spinning very gently round your head. "
        "Or at least it would be if you could see it which you can't.\n\n"
    )

    print(output.flush(), end="")

    # Run the game loop
    parser = Parser()
    loop = GameLoop(state, parser)
    loop.run()


if __name__ == "__main__":
    main()
