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

import argparse
from pathlib import Path

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

# 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

# Default voice model location (project root)
_DEFAULT_VOICE = Path(__file__).resolve().parent.parent / "en_GB-alan-medium.onnx"


def main() -> None:
    ap = argparse.ArgumentParser(description="The Hitchhiker's Guide to the Galaxy")
    ap.add_argument(
        "--audio", action="store_true",
        help="Enable text-to-speech (requires Piper voice model)",
    )
    ap.add_argument(
        "--voice", type=Path, default=_DEFAULT_VOICE,
        help="Path to Piper .onnx voice model",
    )
    args = ap.parse_args()

    # 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
    )

    # Queue startup timed events
    from h2g2.content.earth import _i_housewreck, _i_vogons
    clock.queue(_i_housewreck, 20, name="I-HOUSEWRECK")
    clock.queue(_i_vogons, 50, name="I-VOGONS")

    # 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"
    )

    # Initialize TTS if requested
    tts = None
    if args.audio:
        from h2g2.engine.tts import TTS
        tts = TTS(args.voice)

    banner = output.flush()
    print(banner, end="")
    if tts:
        tts.speak(banner)

    # Run the game loop
    parser = Parser()
    loop = GameLoop(state, parser, tts=tts)
    state.flags["_game_loop"] = loop
    loop.run()


if __name__ == "__main__":
    main()
