import time
from config import get_password, get_username
from playwright.sync_api import Page


def login(page: Page, url: str, landing_url: str):
    raise RuntimeError("default login not supported")


def login_kenan_flagler(page: Page, url: str, landing_url: str) -> None:
    page.goto(url)

    page.wait_for_load_state('load')
    if page.url.startswith(landing_url):
        return

    with page.expect_navigation():
        page.locator("text=ONYEN Login").click()

    time.sleep(0.5)
    page.locator("input[type=email]").fill(get_username())
    with page.expect_navigation():
        page.locator("input[type=submit]").click()

    time.sleep(1)
    page.locator("input[type=password]").fill(get_password())
    with page.expect_navigation():
        page.click('input[type=submit]')

    if page.url.endswith("/login"):
        # 2-factor auth
        page.locator("div[role=\"button\"]:has-text(\"Text\")").click()

        print("Enter code: ", end="")
        code = input()
        code = code.strip()

        page.locator("[aria-label=\"Code\"]").fill(code)
        with page.expect_navigation():
            page.locator("text=Verify").click()
        page.locator("[aria-label=\"Don\\'t\\ show\\ this\\ again\"]").check()
        page.locator("text=Yes").click()

    time.sleep(0.5)
    assert page.url.startswith(landing_url)

[((20, 4, 20, 19), 'time.sleep', 'time.sleep', ({(20, 15, 20, 18): '(0.5)'}, {}), '(0.5)', False, 'import time\n'), ((25, 4, 25, 17), 'time.sleep', 'time.sleep', ({(25, 15, 25, 16): '(1)'}, {}), '(1)', False, 'import time\n'), ((44, 4, 44, 19), 'time.sleep', 'time.sleep', ({(44, 15, 44, 18): '(0.5)'}, {}), '(0.5)', False, 'import time\n'), ((21, 43, 21, 57), 'config.get_username', 'get_username', ({}, {}), '()', False, 'from config import get_password, get_username\n'), ((26, 46, 26, 60), 'config.get_password', 'get_password', ({}, {}), '()', False, 'from config import get_password, get_username\n')]