# python 3.7
# encoding: utf-8


def test_clicking_rating_not_logged_in(browser):
    """
    Clicking "Your Rating" star refers to login page.
    After successful logging in it returns to Top Rated Movies page.
    """

    # checking if we are at Top 250 page and not logged in
    assert browser.title == 'IMDb Top 250 - IMDb'
    assert browser.find_element_by_xpath('//*[@id="imdb-signin-link"]/span').text == 'Sign in'

    # clicking grey star icon
    browser.find_element_by_class_name('unseen').click()

    # checking if we are at login page
    assert browser.title == 'Sign in with IMDb - IMDb'

    # clicking Sign in via IMDb account
    browser.find_element_by_xpath('//*[@id="signin-options"]/div/div[1]/a[1]').click()

    # checking if we are at login via IMDb account page
    assert browser.title == 'IMDb Sign-In'

    # logging in
    browser.find_element_by_xpath('//*[@id="ap_email"]').send_keys('malwarebytes-test@protonmail.com')
    browser.find_element_by_xpath('//*[@id="ap_password"]').send_keys('malwarebytes')
    browser.find_element_by_xpath('//*[@id="signInSubmit"]').click()

    # checking if we are back to Top 250 page and logged in
    assert browser.title == 'IMDb Top 250 - IMDb'
    assert browser.find_element_by_xpath('//*[@id="nbusername"]').text == 'test-user'


def test_movie_titles_preferred_language(browser):
    """
    Movie titles are in preferred language according to an account settings page.
    """

    # logging in
    assert browser.title == 'IMDb Top 250 - IMDb'
    browser.find_element_by_xpath('//*[@id="imdb-signin-link"]/span').click()
    assert browser.title == 'Sign in with IMDb - IMDb'
    browser.find_element_by_xpath('//*[@id="signin-options"]/div/div[1]/a[1]').click()
    assert browser.title == 'IMDb Sign-In'
    browser.find_element_by_xpath('//*[@id="ap_email"]').send_keys('malwarebytes-test@protonmail.com')
    browser.find_element_by_xpath('//*[@id="ap_password"]').send_keys('malwarebytes')
    browser.find_element_by_xpath('//*[@id="signInSubmit"]').click()

    # setting empty value for language of movie titles
    assert browser.title == 'IMDb Top 250 - IMDb'
    browser.get('https://www.imdb.com/preferences/general')
    assert browser.title == 'Content Settings - IMDb'
    browser.find_element_by_xpath('//*[@id="main"]/div/form/div[1]/div[1]/select/option[1]').click()
    browser.find_element_by_xpath('//*[@id="main"]/div/form/p/input[1]').click()

    # checking title of Batman movie
    browser.get('https://www.imdb.com/chart/top?ref_=nv_mv_250')
    assert browser.title == 'IMDb Top 250 - IMDb'
    assert browser.find_element_by_xpath('//*[@id="main"]/div/span/div/div/div[3]/table/tbody/tr[4]/td[2]/a').text == \
        'The Dark Knight'

    # setting language to Estonian
    browser.get('https://www.imdb.com/preferences/general')
    assert browser.title == 'Content Settings - IMDb'
    browser.find_element_by_xpath('//*[@id="main"]/div/form/div[1]/div[1]/select/option[text()="Estonia"]').click()
    browser.find_element_by_xpath('//*[@id="main"]/div/form/p/input[1]').click()

    # checking if title of Batman movie in Estonian language
    browser.get('https://www.imdb.com/chart/top?ref_=nv_mv_250')
    assert browser.title == 'IMDb Top 250 - IMDb'
    assert browser.find_element_by_xpath('//*[@id="main"]/div/span/div/div/div[3]/table/tbody/tr[4]/td[2]/a').text == \
        'Pimeduse rüütel'

    # postcondition: reverting language of movie titles to default value
    browser.get('https://www.imdb.com/preferences/general')
    assert browser.title == 'Content Settings - IMDb'
    browser.find_element_by_xpath('//*[@id="main"]/div/form/div[1]/div[1]/select/option[1]').click()
    browser.find_element_by_xpath('//*[@id="main"]/div/form/p/input[1]').click()
