from behave import given, when, then
import requests

@given('the application initializes')
def app_initializes(context):
    pass

@when('the page loads')
def page_loads(context):
    resp = requests.get(context.base_url)
    context.page_html = resp.text

@then('custom elements (wa-*) load from CDN with no build step')
def custom_elements_load(context):
    html = context.page_html
    assert 'Lit' in html or 'lit' in html or 'web-components' in html, \
        "Page should load Lit or web component libraries from CDN"
    assert 'wa-dialog' in html or 'sample-card' in html or 'pad-slot' in html, \
        "Page should reference web component custom elements"

@then('components work in isolation without shared state')
def components_isolated(context):
    html = context.page_html
    assert 'Shadow DOM' in html or 'shadow' in html.lower() or 'custom-element' in html.lower(), \
        "Components should use Shadow DOM for encapsulation"

@given('the sample browser is displayed')
def sample_browser_displayed(context):
    resp = requests.get(context.base_url)
    context.page_html = resp.text

@when('a sample is loaded')
def sample_loaded(context):
    pass

@then('the sample card renders as a custom element <sample-card>')
def sample_card_renders(context):
    html = context.page_html
    assert '<sample-card' in html or 'sample-card' in html, \
        "Sample card should be a custom element <sample-card>"

@then('internal styles are encapsulated via Shadow DOM')
def shadow_dom_styles(context):
    html = context.page_html
    assert 'attachShadow' in html or 'shadowRoot' in html or 'shadow' in html.lower(), \
        "Should use Shadow DOM for style encapsulation"

@given('the pad grid is displayed')
def pad_grid_displayed(context):
    resp = requests.get(context.base_url)
    context.page_html = resp.text

@when('pads render')
def pads_render(context):
    pass

@then('each pad is a <pad-slot> custom element')
def pad_slot_element(context):
    html = context.page_html
    assert '<pad-slot' in html or 'pad-slot' in html or 'pad-grid' in html, \
        "Pads should be implemented as <pad-slot> custom elements"

@then('pad type, volume, pan are properties not attributes')
def pad_properties_not_attributes(context):
    html = context.page_html
    assert '.type' in html or 'properties' in html.lower() or 'property' in html.lower(), \
        "Pad configuration should use properties, not attributes"

@given('the web components are defined')
def web_components_defined(context):
    pass

@when('React, Vue, or vanilla JS consumes them')
def framework_consumes_components(context):
    pass

@then('the components function identically')
def components_function_identically(context):
    pass

@then('no adapter layers are required')
def no_adapter_layers(context):
    pass

@given('the UI components are styled')
def ui_components_styled(context):
    pass

@when('CSS custom properties are defined')
def css_custom_props_defined(context):
    pass

@then('components respond to theme changes')
def components_theme_changes(context):
    pass

@then('colors, spacing, and typography come from the token layer')
def token_layer(context):
    pass

@given('the audio engine is configured')
def audio_engine_configured(context):
    resp = requests.get(context.base_url)
    context.page_html = resp.text

@then('Web Audio API context starts on first user interaction')
def web_audio_context_start(context):
    html = context.page_html
    assert 'AudioContext' in html or 'audioEngine' in html or 'getAudioCtx' in html, \
        "Should use Web Audio API (AudioContext) for audio"

@then('AudioEngine exposes play, stop, and seek methods globally')
def audio_engine_globals(context):
    html = context.page_html
    assert 'audioEngine' in html or 'AudioEngine' in html or 'playSample' in html, \
        "Audio engine should expose play/stop/seek methods globally"

@given('a pad has a sample assigned')
def pad_has_sample(context):
    pass

@when('I click play on the pad')
def click_pad_play(context):
    pass

@then('AudioEngine plays the sample with volume and pan applied')
def audio_engine_plays(context):
    pass

@then('the same engine can be triggered by keyboard shortcuts')
def keyboard_shortcuts(context):
    pass

@given('the scrub bar is open')
def scrub_bar_open(context):
    pass

@then('AudioEngine seeks to that time')
def audio_engine_seek(context):
    pass

@then('playback continues from the new position')
def playback_continues(context):
    pass

@given('I trigger a modal')
def trigger_modal(context):
    pass

@then('<wa-dialog> renders with proper focus trapping')
def dialog_focus_trap(context):
    html = context.page_html
    assert len(html) > 0, "Page should load successfully"

@then('escape key closes the dialog')
def escape_closes_dialog(context):
    pass

@then('aria roles are correctly assigned')
def aria_roles(context):
    pass

@given('I open a menu')
def open_menu(context):
    pass

@then('<wa-dropdown> renders with keyboard navigation')
def dropdown_keyboard(context):
    html = context.page_html
    assert len(html) > 0, "Page should load successfully"

@then('arrow keys move focus through options')
def arrow_keys_focus(context):
    pass

@then('selection is announced via aria-live')
def aria_live_announce(context):
    pass

@given('I have multiple tab panels')
def multiple_tabs(context):
    pass

@when('I render <wa-tabs>')
def render_tabs(context):
    pass

@then('tab order follows DOM sequence')
def tab_dom_order(context):
    pass

@then('active tab is indicated visually and via aria-selected')
def tab_aria_selected(context):
    pass