# retoor <retoor@molodetz.nl>

from playwright.sync_api import expect
from tests.conftest import BASE_URL
from tests.e2e.project.files import _make_project_ui
def _open_project_with_delete(page, title):
    proj_url = _make_project_ui(page, title)
    slug = proj_url.rstrip("/").split("/")[-1]
    return proj_url, slug
def _click_delete(page):
    page.locator(".project-actions-more").click()
    page.locator(".context-menu-item:has-text('Delete')").click()


def test_data_confirm_opens_custom_dialog(alice):
    page, _ = alice
    _open_project_with_delete(page, "Confirm Dialog Open")
    _click_delete(page)
    expect(page.locator(".dialog-overlay.visible")).to_be_visible()
    expect(page.locator(".dialog-overlay.visible .dialog-message")).to_contain_text(
        "Delete"
    )


def test_data_confirm_cancel_aborts(alice):
    page, _ = alice
    proj_url, _ = _open_project_with_delete(page, "Confirm Cancel")
    _click_delete(page)
    page.locator(".dialog-overlay.visible .dialog-cancel").click()
    expect(page.locator(".dialog-overlay")).not_to_be_visible()
    resp = page.goto(proj_url, wait_until="domcontentloaded")
    assert resp.status == 200


def test_dialog_escape_cancels(alice):
    page, _ = alice
    _open_project_with_delete(page, "Confirm Escape")
    _click_delete(page)
    expect(page.locator(".dialog-overlay.visible")).to_be_visible()
    page.keyboard.press("Escape")
    expect(page.locator(".dialog-overlay")).not_to_be_visible()


def test_dialog_backdrop_click_cancels(alice):
    page, _ = alice
    _open_project_with_delete(page, "Confirm Backdrop")
    _click_delete(page)
    overlay = page.locator(".dialog-overlay.visible")
    expect(overlay).to_be_visible()
    overlay.click(position={"x": 5, "y": 5})
    expect(page.locator(".dialog-overlay")).not_to_be_visible()
