import gradio as gr
from game_world import GameWorld, GameSession

# Initialize game
world = GameWorld()
game_session = GameSession(world)

def main_game_loop(message, history):
    """Main game loop function for Gradio interface"""
    return game_session.run_action(message, history)

# Create Gradio interface
def create_interface():
    with gr.Blocks(theme=gr.themes.Soft(), title="Mythic Realms") as demo:
        gr.HTML("""
        <div style="text-align: center; padding: 20px;">
            <h1>🏰 Mythic Realms: AI Fantasy RPG</h1>
            <p>Embark on an epic adventure in Aethermoor, a world of floating cities and sky whales!</p>
            <p><em>Type 'start game' to begin your journey</em></p>
        </div>
        """)
        
        chatbot = gr.Chatbot(
            height=500,
            placeholder="🎮 Ready to start your adventure? Type 'start game' to begin!",
            show_label=False,
            container=True,
            bubble_full_width=False
        )
        
        with gr.Row():
            msg = gr.Textbox(
                placeholder="What do you do next? (e.g., 'Look around', 'Talk to Captain Zara', 'Explore the docks')",
                container=False,
                scale=4
            )
            submit_btn = gr.Button("🚀 Act", scale=1, variant="primary")
        
        with gr.Row():
            gr.Examples(
                examples=[
                    "Start game",
                    "Look around the docks",
                    "Talk to Captain Zara Windwhisper",
                    "Explore the market",
                    "Ask about sky ships",
                    "Check my inventory",
                    "Look for adventure"
                ],
                inputs=msg
            )
        
        # Set up the chat interface
        def respond(message, history):
            response = main_game_loop(message, history)
            history.append((message, response))
            return history, ""
        
        msg.submit(respond, [msg, chatbot], [chatbot, msg])
        submit_btn.click(respond, [msg, chatbot], [chatbot, msg])
        
        gr.HTML("""
        <div style="text-align: center; margin-top: 20px; padding: 10px; background-color: #aba0ab; border-radius: 10px;">
            <p><strong>🎯 Game Tips:</strong></p>
            <p>• Use natural language to describe your actions</p>
            <p>• Interact with NPCs to learn about the world</p>
            <p>• Manage your inventory wisely</p>
            <p>• Explore different locations for unique adventures</p>
        </div>
        """)
    
    return demo

# Launch the app
if __name__ == "__main__":
    demo = create_interface()
    demo.launch()