# retoor <retoor@molodetz.nl>

from tests.conftest import BASE_URL

SUBMIT = "#create-issue-modal button[type='submit']"
SPINNER = "#create-issue-modal button[type='submit'] .btn-spinner"


def _open_form(page):
    page.goto(f"{BASE_URL}/issues", wait_until="domcontentloaded")
    trigger = page.locator("button:has-text('Report Issue')")
    title = page.locator("#issue-title")
    trigger.click()
    try:
        title.wait_for(state="visible", timeout=5000)
    except Exception:
        trigger.click()
        title.wait_for(state="visible", timeout=10000)
    title.fill("Spinner regression title")
    page.fill("#issue-description", "Steps to reproduce the spinner regression")


def test_submit_disables_button_and_shows_spinner(alice):
    page, _ = alice
    page.route(
        "**/issues/create",
        lambda route: route.fulfill(
            status=200,
            content_type="application/json",
            body='{"uid":"spin-job","status_url":"/issues/jobs/spin-job"}',
        ),
    )
    page.route(
        "**/issues/jobs/spin-job",
        lambda route: route.fulfill(
            status=200,
            content_type="application/json",
            body='{"uid":"spin-job","kind":"issue_create","status":"pending"}',
        ),
    )
    _open_form(page)
    page.click(SUBMIT)
    page.locator(f"{SUBMIT}.is-loading").wait_for(state="visible")
    assert page.locator(SUBMIT).is_disabled()
    assert page.locator(SPINNER).is_visible()


def test_submit_prevents_double_posting(alice):
    page, _ = alice
    create_calls = []
    page.route(
        "**/issues/create",
        lambda route: (
            create_calls.append(1),
            route.fulfill(
                status=200,
                content_type="application/json",
                body='{"uid":"once-job","status_url":"/issues/jobs/once-job"}',
            ),
        )[-1],
    )
    page.route(
        "**/issues/jobs/once-job",
        lambda route: route.fulfill(
            status=200,
            content_type="application/json",
            body='{"uid":"once-job","kind":"issue_create","status":"pending"}',
        ),
    )
    _open_form(page)
    page.click(SUBMIT)
    page.locator(f"{SUBMIT}.is-loading").wait_for(state="visible")
    page.evaluate(
        "document.querySelector('form[data-issue-create]')"
        ".dispatchEvent(new Event('submit', {cancelable: true, bubbles: true}))"
    )
    page.wait_for_timeout(300)
    assert len(create_calls) == 1


def test_submit_reenables_button_on_error(alice):
    page, _ = alice
    page.route(
        "**/issues/create",
        lambda route: route.fulfill(
            status=503,
            content_type="application/json",
            body='{"ok":false,"message":"The issue tracker is not configured"}',
        ),
    )
    _open_form(page)
    page.click(SUBMIT)
    page.locator(f"{SUBMIT}:not(.is-loading)").wait_for(state="visible")
    assert not page.locator(SUBMIT).is_disabled()
    assert not page.locator(SPINNER).is_visible()
