# retoor <retoor@molodetz.nl>

from playwright.sync_api import expect
from tests.conftest import BASE_URL

MUTE = "form[action='/mute/bob_test'] button"
UNMUTE = "form[action='/mute/unmute/bob_test'] button"


def _confirm(page):
    page.locator(".dialog-overlay.visible .dialog-confirm").click()


def _reset_mute(page):
    page.goto(f"{BASE_URL}/profile/bob_test", wait_until="domcontentloaded")
    if page.is_visible(UNMUTE):
        page.click(UNMUTE)
        _confirm(page)
        page.wait_for_url("**/profile/bob_test", wait_until="domcontentloaded")


def test_mute_and_unmute_buttons_toggle(alice):
    page, _ = alice
    _reset_mute(page)
    assert page.is_visible(MUTE)

    page.click(MUTE)
    _confirm(page)
    page.wait_for_url("**/profile/bob_test", wait_until="domcontentloaded")
    assert page.is_visible(UNMUTE)

    page.click(UNMUTE)
    _confirm(page)
    page.wait_for_url("**/profile/bob_test", wait_until="domcontentloaded")
    assert page.is_visible(MUTE)


def test_mute_button_opens_confirmation_dialog(alice):
    page, _ = alice
    _reset_mute(page)
    page.click(MUTE)
    expect(page.locator(".dialog-overlay.visible")).to_be_visible()
    expect(page.locator(".dialog-overlay.visible .dialog-message")).to_contain_text(
        "Mute"
    )
    page.locator(".dialog-overlay.visible .dialog-cancel").click()
    expect(page.locator(".dialog-overlay")).not_to_be_visible()
    assert page.is_visible(MUTE)
