#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Input handling for Storm Games.

Provides functionality for:
- Text input dialogs
- Game pause functionality
- Exit handling
"""

import pygame
import time
import wx
from .speech import speak

def get_input(prompt="Enter text:", text=""):
    """Display a dialog box for text input.

    Args:
        prompt (str): Prompt text to display (default: "Enter text:")
        text (str): Initial text in input box (default: "")

    Returns:
        str: User input text, or None if cancelled
    """
    app = wx.App(False)
    dialog = wx.TextEntryDialog(None, prompt, "Input", text)
    dialog.SetValue(text)
    if dialog.ShowModal() == wx.ID_OK:
        userInput = dialog.GetValue()
    else:
        userInput = None
    dialog.Destroy()
    return userInput

def pause_game():
    """Pauses the game until user presses backspace."""
    speak("Game paused, press backspace to resume.")
    pygame.event.clear()
    try:
        pygame.mixer.pause()
    except:
        pass

    try:
        pygame.mixer.music.pause()
    except:
        pass

    while True:
        event = pygame.event.wait()
        if event.type == pygame.KEYDOWN and event.key == pygame.K_BACKSPACE: 
            break

    try:
        pygame.mixer.unpause()
    except:
        pass

    try:
        pygame.mixer.music.unpause()
    except:
        pass

    pygame.event.pump()

def check_for_exit():
    """Check if user has pressed escape key.

    Returns:
        bool: True if escape was pressed, False otherwise
    """
    for event in pygame.event.get():
        if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
            return True
        return False
    pygame.event.pump()