import pathlib
import urllib.request

import selenium.webdriver

PO_RESULT = '''#
msgid ""
msgstr ""

#. Programmer comment
msgctxt "Disambiguation for context"
msgid "One"
msgstr "New translation"
'''


def test_first(selenium: selenium.webdriver.Remote, running_server_url):
    open_homepage(running_server_url, selenium)
    upload_po(selenium)
    selenium.implicitly_wait(2)
    assert_translations_displayed(selenium)
    go_to_translation_editor(selenium)
    edit_translation(selenium)
    selenium.implicitly_wait(2)
    assert_new_translation_displayed(selenium)
    assert_download_link_works(selenium)


def open_homepage(running_server_url, selenium):
    selenium.get(running_server_url)
    assert selenium.title == 'Dila'


def upload_po(selenium):
    file_upload = selenium.find_element_by_id('po_file')
    file_upload.clear()
    file_upload.send_keys(str(pathlib.Path(__file__).parent.parent / 'test.po'))
    selenium.find_element_by_id('upload_po_file').click()


def assert_translations_displayed(selenium):
    content = selenium.find_element_by_tag_name('body').text
    assert 'File uploaded' in content
    assert 'Disambiguation for context' in content
    assert 'Een' in content


def go_to_translation_editor(selenium):
    selenium.find_element_by_link_text('One').click()


def edit_translation(selenium):
    text_input = selenium.find_element_by_id('translation')
    text_input.clear()
    text_input.send_keys('New translation')
    selenium.find_element_by_id('submit').click()


def assert_new_translation_displayed(selenium):
    content = selenium.find_element_by_tag_name('body').text
    assert 'Translation changed' in content
    assert 'Disambiguation for context' in content
    assert 'New translation' in content


def assert_download_link_works(selenium):
    download_link = selenium.find_element_by_link_text('Download po')
    po_url = download_link.get_attribute('href')
    with urllib.request.urlopen(po_url) as new_po_file:
        new_po = new_po_file.read()
        assert new_po.decode('utf-8') == PO_RESULT
        assert new_po_file.info()['Content-Disposition'] == "attachment; filename=translations.po"

@@ -314,24 +314,202 @@
 , selenium)%0A
+    assert_no_resources_info(selenium)%0A    add_resource(selenium, 'first resource')%0A    add_resource(selenium, 'second resource')%0A    select_resource(selenium, 'first resource')%0A
     upload_p
@@ -512,32 +512,32 @@
 ad_po(selenium)%0A
-
     selenium.imp
@@ -786,16 +786,137 @@
 lenium)%0A
+    go_homepage(selenium)%0A    select_resource(selenium, 'first resource')%0A    assert_no_translations_displayed(selenium)%0A
 %0A%0Adef op
@@ -1029,24 +1029,580 @@
 == 'Dila'%0A%0A%0A
+def assert_no_resources_info(selenium):%0A    content = selenium.find_element_by_tag_name('body').text%0A    assert 'There are no resources.' in content%0A%0A%0Adef add_resource(selenium, name):%0A    resource_name = selenium.find_element_by_id('new-resource-name')%0A    resource_name.clear()%0A    resource_name.send_keys(name)%0A    selenium.find_element_by_id('add-new-resource').click()%0A%0A%0Adef select_resource(selenium, name):%0A    selenium.find_element_by_link_text(name).click()%0A%0A%0Adef go_homepage(selenium):%0A    selenium.find_element_by_link_text('homepage').click()%0A%0A%0A
 def upload_p
@@ -2918,16 +2918,16 @@
 _RESULT%0A
-
         
@@ -3017,8 +3017,260 @@
 ons.po%22%0A
+%0A%0Adef assert_no_translations_displayed(selenium):%0A    content = selenium.find_element_by_tag_name('body').text%0A    assert 'File uploaded' not in content%0A    assert 'Disambiguation for context' not in content%0A    assert 'New translation' not in content%0A

Add multiple resources support to acceptance test.