import pytest
from datetime import datetime, timedelta


@pytest.fixture()
def chrome_options(chrome_options):
    chrome_options.headless = True
    return chrome_options


@pytest.fixture()
def username():
    now = datetime.now()
    return "test" + now.strftime("%y%m%d%H%M%S")


def test_basicregistration(username, base_url, selenium):

    password = "RkqZBfA3hnBzDHMJ5Bum"
    url = "accounts/register/basic/"

    selenium.get(base_url+url)
    selenium.find_element_by_id("id_username").send_keys(username)
    selenium.find_element_by_id("id_email").send_keys(username+"@example.com")
    selenium.find_element_by_id("id_password1").send_keys(password)
    selenium.find_element_by_id("id_password2").send_keys(password)
    selenium.find_element_by_xpath("/html/body/main/div/form/input[2]").click()

    # Check that username is in profile page
    log_state = selenium.find_element_by_id("log_state")
    assert username in log_state.text

    # Check that only Basic services are enabled
    date = datetime.now() + timedelta(days=30)
    date_str = date.strftime("%Y-%m-%d")

    # Tuples with the expected values for the service when they are
    # disabled and enabled
    enabled_state = ("Yes", date_str, "30 days", "---", False)
    disabled_state = ("No", "-", "-", "---", False)

    services = {"Tunnel": disabled_state,
                "Chat": enabled_state,
                "Mail": disabled_state,
                "Social": enabled_state,
                "XMPP": disabled_state}

    for (i, (k, v)) in enumerate(services.items(), 1):
        xpath = "/html/body/main/article/table/tbody/tr[{}]/th".format(i)
        srv_title = selenium.find_element_by_xpath(xpath).text
        xpath = "/html/body/main/article/table/tbody/tr[{}]/td[1]".format(i)
        srv_active = selenium.find_element_by_xpath(xpath).text
        xpath = "/html/body/main/article/table/tbody/tr[{}]/td[2]".format(i)
        srv_ren = selenium.find_element_by_xpath(xpath).text
        xpath = "/html/body/main/article/table/tbody/tr[{}]/td[3]".format(i)
        srv_cred = selenium.find_element_by_xpath(xpath).text
        xpath = "/html/body/main/article/table/tbody/tr[{}]/td[4]/form/input[2]".format(i)
        srv_actions = selenium.find_element_by_xpath(xpath)
        assert srv_title == k
        values = (
            srv_active, srv_ren, srv_cred,
            srv_actions.get_attribute(name='value'),
            srv_actions.is_enabled()
        )
        assert values == v
