/*
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.xwiki.test.ui.po;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.StaleElementReferenceException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedCondition;

/**
 * Represents the page breadcrumb.
 *
 * @version $Id$
 * @since 7.2M3
 */
public class BreadcrumbElement extends BaseElement
{
    private WebElement container;

    public BreadcrumbElement(WebElement container)
    {
        this.container = container;
    }

    public List<String> getPath()
    {
        List<WebElement> pathElements = getDriver().findElementsWithoutWaiting(this.container, By.tagName("li"));
        List<String> path = new ArrayList<>();
        for (WebElement pathElement : pathElements) {
            path.add(pathElement.getText());
        }
        return path;
    }

    public String getPathAsString()
    {
        return StringUtils.join(getPath(), '/');
    }

    public void clickPathElement(String linkText)
    {
        this.container.findElement(By.linkText(linkText)).click();
    }

    public boolean hasPathElement(String text, boolean isCurrent, boolean withLink)
    {
        List<WebElement> result;
        if (isCurrent) {
            result = getDriver().findElementsWithoutWaiting(this.container,
                By.xpath("li[contains(@class, 'active')]/a[. = '" + text + "']"));
        } else {
            if (withLink) {
                result = getDriver().findElementsWithoutWaiting(this.container, By.xpath("//a[. = '" + text + "']"));
            } else {
                // if the user has not the right to see the parent, there is no link to the parent, only a <li> with the
                // name of the document
                result = getDriver().findElementsWithoutWaiting(this.container, By.xpath("//li[. = '" + text + "']"));
            }
        }
        return result.size() > 0;
    }
    
    public void expand()
    {
        // Remember the id of the container so that we can retrieve it when it will be removed and re-inserted.
        final String containerId = this.container.getAttribute("id");
        
        clickPathElement("…");
        
        // Expanding the breadcrumb remove the current container and replace it by an updated one (with the full 
        // hierarchy inside).
        // So we wait until the container is re-inserted without any "ellipsis" item.
        getDriver().waitUntilCondition(new ExpectedCondition<Boolean>()
        {
            @Override
            public Boolean apply(WebDriver driver)
            {   
                try {
                    WebElement container = getDriver().findElementById(containerId);
                    return container != null && !getDriver().hasElementWithoutWaiting(container, By.className("ellipsis"));
                } catch (StaleElementReferenceException e) {
                    // Happens when the container is removed between the 2 driver actions (findElementById() and 
                    // hasElementWithoutWaiting()). It's a race condition.
                    return false;
                }

            }
        });
        
        // Update the internal reference
        this.container = getDriver().findElementById(containerId);
    }
    
    public boolean canBeExpanded()
    {
        return hasPathElement("…", false, true);
    }
}

@@ -1076,172 +1076,18 @@
 ium.
-StaleElementReferenceException;%0Aimport org.openqa.selenium.WebDriver;%0Aimport org.openqa.selenium.WebElement;%0Aimport org.openqa.selenium.support.ui.ExpectedCondition
+WebElement
 ;%0A%0A/
@@ -2745,36 +2745,32 @@
 ze() %3E 0;%0A    %7D%0A
-    
 %0A    public void
@@ -2963,32 +2963,24 @@
 bute(%22id%22);%0A
-        
 %0A        cli
@@ -2999,24 +2999,16 @@
 t(%22%E2%80%A6%22);%0A
-        
 %0A       
@@ -3112,17 +3112,16 @@
 the full
- 
 %0A       
@@ -3142,27 +3142,16 @@
 inside).
-%0A        //
  So we w
@@ -3251,719 +3251,77 @@
 ntil
-Condition(new ExpectedCondition%3CBoolean%3E()%0A        %7B%0A            @Override%0A            public Boolean apply(WebDriver driver)%0A            %7B   %0A                try %7B%0A                    WebElement container = getDriver().findElementById(containerId);%0A                    return container != null && !getDriver().hasElementWithoutWaiting(container, By.className(%22ellipsis%22));%0A                %7D catch (StaleElementReferenceException e) %7B%0A                    // Happens when the container is removed between the 2 driver actions (findElementById() and %0A                    // hasElementWithoutWaiting()). It's a race condition.%0A                    return false;%0A                %7D%0A%0A            %7D%0A        %7D);%0A        
+ElementDisappears(By.cssSelector(%22#%22 + containerId + %22 .ellipsis%22));%0A
 %0A   
@@ -3431,20 +3431,16 @@
 ;%0A    %7D%0A
-    
 %0A    pub

Simplify the page object, hoping this will fix the flickering test (CreatePageNestedDocumentsTest#createNestedDocumentsFromURL).