# retoor <retoor@molodetz.nl>

import time

from devplacepy.database import get_setting, set_setting
from tests.conftest import BASE_URL, CACHE_VERSION_PROPAGATION_SECONDS

IOS_SETTING = "ios_app_url"
IOS_APP_URL = "https://apps.apple.com/app/devplace/id6797215143"
LANDING_FOOTER_BADGE = ".landing-footer .app-store-link"
SITE_FOOTER_BADGE = ".site-footer .app-store-link"
NAV_BADGE = ".topnav-right .app-store-nav-link"
MOBILE_BADGE = ".topnav-mobile-panel .app-store-mobile-link"


def _configure():
    original = get_setting(IOS_SETTING, "")
    set_setting(IOS_SETTING, IOS_APP_URL)
    time.sleep(CACHE_VERSION_PROPAGATION_SECONDS)
    return original


def _assert_badge(badge):
    badge.wait_for(state="visible")
    assert badge.get_attribute("href") == IOS_APP_URL
    assert badge.get_attribute("target") == "_blank"
    assert badge.get_attribute("rel") == "noopener noreferrer"


def test_footer_app_store_badge_on_landing_page(page, app_server):
    original = _configure()
    try:
        page.goto(f"{BASE_URL}/", wait_until="domcontentloaded")
        _assert_badge(page.locator(LANDING_FOOTER_BADGE))
    finally:
        set_setting(IOS_SETTING, original)


def test_footer_app_store_badge_on_regular_page(page, app_server):
    original = _configure()
    try:
        page.goto(f"{BASE_URL}/feed", wait_until="domcontentloaded")
        _assert_badge(page.locator(SITE_FOOTER_BADGE))
    finally:
        set_setting(IOS_SETTING, original)


def test_topnav_app_store_badge_next_to_login_signup(page, app_server):
    original = _configure()
    try:
        page.goto(f"{BASE_URL}/", wait_until="domcontentloaded")
        _assert_badge(page.locator(NAV_BADGE))
        assert page.locator(".topnav-right a[href='/auth/login']").is_visible()
        assert page.locator(".topnav-right a[href='/auth/signup']").is_visible()
    finally:
        set_setting(IOS_SETTING, original)


def test_mobile_menu_app_store_badge(page, app_server):
    original = _configure()
    try:
        page.set_viewport_size({"width": 390, "height": 844})
        page.goto(f"{BASE_URL}/", wait_until="domcontentloaded")
        hamburger = page.locator("#hamburger-btn")
        hamburger.wait_for(state="visible")
        hamburger.click()
        page.locator("#mobile-panel.open").wait_for(state="visible")
        _assert_badge(page.locator(MOBILE_BADGE))
    finally:
        set_setting(IOS_SETTING, original)


def test_badges_hide_when_ios_app_url_is_cleared(page, app_server):
    original = _configure()
    try:
        page.goto(f"{BASE_URL}/", wait_until="domcontentloaded")
        assert page.locator(LANDING_FOOTER_BADGE).count() == 1
        set_setting(IOS_SETTING, "")
        time.sleep(CACHE_VERSION_PROPAGATION_SECONDS)
        page.goto(f"{BASE_URL}/", wait_until="domcontentloaded")
        assert page.locator(LANDING_FOOTER_BADGE).count() == 0
        assert page.locator(NAV_BADGE).count() == 0
        assert page.locator(MOBILE_BADGE).count() == 0
        page.goto(f"{BASE_URL}/feed", wait_until="domcontentloaded")
        assert page.locator(SITE_FOOTER_BADGE).count() == 0
    finally:
        set_setting(IOS_SETTING, original)






