import pytest
from django.urls import reverse
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

from cutepaste.tests.marks import slow

pytestmark = pytest.mark.usefixtures("data_files")


@slow
def test_index(live_server, webdriver):
    webdriver.get(live_server.url + reverse("files:ls", args=[""]))
    directories = webdriver.find_elements_by_css_selector(".dir-item")
    files = webdriver.find_elements_by_css_selector(".file-item")
    assert len(directories) == 1
    assert len(files) == 2


@slow
def test_navigate_to_directory(live_server, webdriver):
    webdriver.get(live_server.url + reverse("files:ls", args=[""]))

    dir1_links = webdriver.find_elements_by_link_text("dir1/")
    assert len(dir1_links) == 1

    dir1_link = dir1_links[0]

    dir1_link.click()

    dir1_file1_links = webdriver.find_elements_by_link_text("dir1_file1.txt")

    assert len(dir1_file1_links) == 1


@slow
def test_remove_file(live_server, webdriver):
    webdriver.get(live_server.url + reverse("files:ls", args=[""]))

    file1_links = webdriver.find_elements_by_link_text("file1.txt")
    assert len(file1_links) == 1

    file1_checkboxes = webdriver.find_elements_by_css_selector("input[data-entry='file1.txt']")
    assert len(file1_checkboxes) == 1

    file1_checkbox = file1_checkboxes[0]
    file1_checkbox.click()

    trash_button = webdriver.find_element_by_id("trash-button")

    trash_button.click()

    WebDriverWait(webdriver, 1).until(
        EC.invisibility_of_element_located((By.LINK_TEXT, "file1.txt")))

    file1_links = webdriver.find_elements_by_link_text("file1.txt")
    assert len(file1_links) == 0


@slow
def test_copy_paste_file(live_server, webdriver):
    webdriver.get(live_server.url + reverse("files:ls", args=[""]))

    file1_links = webdriver.find_elements_by_link_text("file1.txt")
    assert len(file1_links) == 1

    file1_checkboxes = webdriver.find_elements_by_css_selector("input[data-entry='file1.txt']")
    assert len(file1_checkboxes) == 1

    file1_checkbox = file1_checkboxes[0]
    file1_checkbox.click()

    copy_button = webdriver.find_element_by_id("copy-button")

    copy_button.click()

    dir1_links = webdriver.find_elements_by_link_text("dir1/")
    assert len(dir1_links) == 1

    dir1_link = dir1_links[0]

    dir1_link.click()

    WebDriverWait(webdriver, 1).until(
        EC.invisibility_of_element_located((By.LINK_TEXT, "file1.txt")))

    paste_button = webdriver.find_element_by_id("paste-button")

    paste_button.click()

    file1_links = webdriver.find_elements_by_link_text("file1.txt")
    assert len(file1_links) == 1

    dir_up_links = webdriver.find_elements_by_link_text("../")
    assert len(dir_up_links) == 1

    dir_up_link = dir_up_links[0]

    dir_up_link.click()

    WebDriverWait(webdriver, 1).until(
        EC.invisibility_of_element_located((By.LINK_TEXT, "dir1_file1.txt")))

    file1_links = webdriver.find_elements_by_link_text("file1.txt")
    assert len(file1_links) == 1

@slow
def test_cut_paste_file(live_server, webdriver):
    webdriver.get(live_server.url + reverse("files:ls", args=[""]))

    file1_links = webdriver.find_elements_by_link_text("file1.txt")
    assert len(file1_links) == 1

    file1_checkboxes = webdriver.find_elements_by_css_selector("input[data-entry='file1.txt']")
    assert len(file1_checkboxes) == 1

    file1_checkbox = file1_checkboxes[0]
    file1_checkbox.click()

    cut_button = webdriver.find_element_by_id("cut-button")

    cut_button.click()

    dir1_links = webdriver.find_elements_by_link_text("dir1/")
    assert len(dir1_links) == 1

    dir1_link = dir1_links[0]

    dir1_link.click()

    WebDriverWait(webdriver, 1).until(
        EC.invisibility_of_element_located((By.LINK_TEXT, "file1.txt")))

    paste_button = webdriver.find_element_by_id("paste-button")

    paste_button.click()

    file1_links = webdriver.find_elements_by_link_text("file1.txt")
    assert len(file1_links) == 1

    dir_up_links = webdriver.find_elements_by_link_text("../")
    assert len(dir_up_links) == 1

    dir_up_link = dir_up_links[0]

    dir_up_link.click()

    WebDriverWait(webdriver, 1).until(
        EC.invisibility_of_element_located((By.LINK_TEXT, "dir1_file1.txt")))

    file1_links = webdriver.find_elements_by_link_text("file1.txt")
    assert len(file1_links) == 0

@@ -3100,16 +3100,17 @@
 ) == 1%0A%0A
+%0A
 @slow%0Ade
@@ -4429,28 +4429,736 @@
 ssert len(file1_links) == 0%0A
+%0A%0A@slow%0Adef test_select_all_then_none(live_server, webdriver):%0A    webdriver.get(live_server.url + reverse(%22files:ls%22, args=%5B%22%22%5D))%0A%0A    select_all_button = webdriver.find_element_by_id(%22select-all-button%22)%0A%0A    select_all_button.click()%0A%0A    checkboxes = webdriver.find_elements_by_css_selector(%22input%5Bdata-entry%5D%22)%0A    assert len(checkboxes) == 3%0A    assert all(%5Bcheckbox.is_selected() for checkbox in checkboxes%5D)%0A%0A    select_none_button = webdriver.find_element_by_id(%22select-none-button%22)%0A%0A    select_none_button.click()%0A%0A    checkboxes = webdriver.find_elements_by_css_selector(%22input%5Bdata-entry%5D%22)%0A    assert len(checkboxes) == 3%0A    assert all(%5Bnot checkbox.is_selected() for checkbox in checkboxes%5D)%0A

Add select all/none test