import pytest
import time
from src.api_hook_client import ApiHookClient

def test_text_viewer_state_update(live_gui) -> None:
 """
 
  
   Verifies that we can set text viewer state and it is reflected in GUI state.
 """
 client = ApiHookClient()
 client.click("btn_reset")
 time.sleep(2)
 
 label = "Test Viewer Label"
 content = "This is test content for the viewer."
 text_type = "markdown"

 client.push_event("custom_callback", {"callback": "_set_attr", "args": ["text_viewer_title", label]})
 client.push_event("custom_callback", {"callback": "_set_attr", "args": ["text_viewer_content", content]})
 client.push_event("custom_callback", {"callback": "_set_attr", "args": ["text_viewer_type", text_type]})

 # Wait for text_type to settle
 state = None
 start_time = time.time()
 while time.time() - start_time < 5:
  state = client.get_gui_state()
  if state and state.get('text_viewer_type') == text_type:
   break
  time.sleep(0.1)

 # Now get current show_windows, update it, and set it back
 current_windows = state.get('show_windows', {})
 current_windows["Text Viewer"] = True
 client.push_event("custom_callback", {"callback": "_set_attr", "args": ["show_windows", current_windows]})

 # Poll for show_text_viewer compat flag
 start_time = time.time()
 while time.time() - start_time < 5:
  state = client.get_gui_state()
  if state and state.get('show_text_viewer') == True:
   break
  time.sleep(0.1)

 assert state is not None
 assert state.get('show_text_viewer') == True  # API hook still provides this for compat
 assert state.get('text_viewer_title') == label
 assert state.get('text_viewer_type') == text_type