package org.synyx.urlaubsverwaltung.ui.pages;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.synyx.urlaubsverwaltung.ui.Page;

import static org.openqa.selenium.support.ui.ExpectedConditions.elementToBeClickable;
import static org.synyx.urlaubsverwaltung.ui.PageConditions.elementHasAttributeWithValue;

public class NavigationPage implements Page {

    private static final By NEW_APPLICATION_SELECTOR = By.id("application-new-link");
    private static final By AVATAR_SELECTOR = By.cssSelector("[data-test-id=avatar]");
    private static final By AVATAR_POPUPMENU_SELECTOR = By.cssSelector("[data-test-id=avatar-popupmenu]");
    private static final By LOGOUT_SELECTOR = By.cssSelector("[data-test-id=logout]");

    private final WebDriver driver;
    private final WebDriverWait wait;

    public NavigationPage(WebDriver driver) {
        this.driver = driver;
        this.wait = new WebDriverWait(driver, 5);
    }

    @Override
    public boolean isVisible(WebDriver driver) {
        return newApplicationLinkVisible(driver);
    }

    public void logout() {

        wait.until(elementHasAttributeWithValue(AVATAR_SELECTOR, "aria-expanded", "false"));
        wait.until(elementHasAttributeWithValue(AVATAR_POPUPMENU_SELECTOR, "aria-hidden", "true"));

        driver.findElement(AVATAR_SELECTOR).click();

        wait.until(elementHasAttributeWithValue(AVATAR_SELECTOR, "aria-expanded", "true"));
        wait.until(elementHasAttributeWithValue(AVATAR_POPUPMENU_SELECTOR, "aria-hidden", "false"));
        wait.until(elementToBeClickable(LOGOUT_SELECTOR));

        driver.findElement(LOGOUT_SELECTOR).click();
    }

    /**
     * Click "new application" link.
     */
    public void clickNewApplication() {
        wait.until(elementToBeClickable(NEW_APPLICATION_SELECTOR));
        driver.findElement(NEW_APPLICATION_SELECTOR).click();
    }

    private static boolean newApplicationLinkVisible(WebDriver driver) {
        final WebElement element = driver.findElement(NEW_APPLICATION_SELECTOR);
        return element != null && element.isDisplayed();
    }
}

@@ -559,289 +559,8 @@
 k%22);
-%0A    private static final By AVATAR_SELECTOR = By.cssSelector(%22%5Bdata-test-id=avatar%5D%22);%0A    private static final By AVATAR_POPUPMENU_SELECTOR = By.cssSelector(%22%5Bdata-test-id=avatar-popupmenu%5D%22);%0A    private static final By LOGOUT_SELECTOR = By.cssSelector(%22%5Bdata-test-id=logout%5D%22);
 %0A%0A  
@@ -630,16 +630,57 @@
 it wait;
+%0A    private final AvatarMenu avatarMenu;
 %0A%0A    pu
@@ -799,16 +799,66 @@
 er, 5);%0A
+        this.avatarMenu = new AvatarMenu(driver);%0A
     %7D%0A%0A 
@@ -1007,202 +1007,732 @@
 ) %7B%0A
-%0A        wait.until(elementHasAttributeWithValue(AVATAR_SELECTOR, %22aria-expanded%22, %22false%22));%0A        wait.until(elementHasAttributeWithValue(AVATAR_POPUPMENU_SELECTOR, %22aria-hidden%22, %22true%22));%0A
+        avatarMenu.logout();%0A    %7D%0A%0A    /**%0A     * Click %22new application%22 link.%0A     */%0A    public void clickNewApplication() %7B%0A        wait.until(elementToBeClickable(NEW_APPLICATION_SELECTOR));%0A        driver.findElement(NEW_APPLICATION_SELECTOR).click();%0A    %7D%0A%0A    private static boolean newApplicationLinkVisible(WebDriver driver) %7B%0A        final WebElement element = driver.findElement(NEW_APPLICATION_SELECTOR);%0A        return element != null && element.isDisplayed();%0A    %7D%0A%0A    private static class AvatarMenu %7B%0A%0A        private static final By AVATAR_SELECTOR = By.cssSelector(%22%5Bdata-test-id=avatar%5D%22);%0A        private static final By AVATAR_POPUPMENU_SELECTOR = By.cssSelector(%22%5Bdata-test-id=avatar-popupmenu%5D%22);
 %0A   
@@ -1740,54 +1740,342 @@
     
-driver.findElement(AVATAR_SELECTOR).click();%0A%0A
+private static final By LOGOUT_SELECTOR = By.cssSelector(%22%5Bdata-test-id=logout%5D%22);%0A%0A        private final WebDriver driver;%0A        private final WebDriverWait wait;%0A%0A        AvatarMenu(WebDriver driver) %7B%0A            this.driver = driver;%0A            this.wait = new WebDriverWait(driver, 5);%0A        %7D%0A%0A        void logout() %7B%0A%0A    
     
@@ -2157,17 +2157,22 @@
 %22, %22
-tru
+fals
 e%22));%0A
+    
     
@@ -2262,78 +2262,22 @@
 %22, %22
-fals
+tru
 e%22));%0A
+%0A
     
-    wait.until(elementToBeClickable(LOGOUT_SELECTOR));%0A%0A
     
@@ -2299,22 +2299,22 @@
 Element(
-LOGOUT
+AVATAR
 _SELECTO
@@ -2329,352 +2329,285 @@
 ();%0A
-    %7D%0A%0A    /**%0A     * Click %22new application%22 link.%0A     */%0A    public void clickNewApplication() %7B%0A        wait.until(elementToBeClickable(NEW_APPLICATION_SELECTOR));%0A        driver.findElement(NEW_APPLICATION_SELECTOR).click(
+%0A            wait.until(elementHasAttributeWithValue(AVATAR_SELECTOR, %22aria-expanded%22, %22true%22));%0A            wait.until(elementHasAttributeWithValue(AVATAR_POPUPMENU_SELECTOR, %22aria-hidden%22, %22false%22)
 );%0A    
-%7D%0A%0A
     
-private static boolean newApplicationLinkVisible(WebDriver driver) %7B%0A        final WebElement element =
+    wait.until(elementToBeClickable(LOGOUT_SELECTOR));%0A%0A           
  dri
@@ -2618,39 +2618,30 @@
 findElement(
-NEW_APPLICATION
+LOGOUT
 _SELECTOR);%0A
@@ -2630,32 +2630,40 @@
 LOGOUT_SELECTOR)
+.click()
 ;%0A        return
@@ -2660,56 +2660,9 @@
     
-return element != null && element.isDisplayed();
+%7D
 %0A   

move navigation ui test avatar stuff into separate inner class