package com.autonomy.abc.selenium.indexes.tree;

import com.autonomy.abc.selenium.indexes.Index;
import com.hp.autonomy.frontend.selenium.element.ChevronContainer;
import com.hp.autonomy.frontend.selenium.element.Collapsible;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

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

public abstract class IndexCategoryNode implements IndexNodeElement, Collapsible, Iterable<IndexNodeElement> {
    private final WebElement container;
    private final WebDriver driver;
    private final Collapsible collapsible;
    private final IndexNodeElement delegate;

    protected IndexCategoryNode(IndexNodeElement inside, WebElement element, WebDriver webDriver) {
        delegate = inside;
        container = element;
        driver = webDriver;
        collapsible = new ChevronContainer(element, driver);
    }

    @Override
    public void select() {
        delegate.select();
    }

    @Override
    public void deselect() {
        delegate.deselect();
    }

    @Override
    public boolean isSelected() {
        return delegate.isSelected();
    }

    @Override
    public void expand() {
        collapsible.expand();
    }

    @Override
    public void collapse() {
        collapsible.collapse();
    }

    @Override
    public boolean isCollapsed() {
        return collapsible.isCollapsed();
    }

    @Override
    public String getName() {
        return container.getText();
    }

    public List<String> getSelectedNames() {
        List<String> selected = new ArrayList<>();
        for (IndexNodeElement child : this) {
            if (child.isSelected()) {
                selected.add(child.getName());
            }
        }
        return selected;
    }

    public Index getIndex(int i) {
        return new Index(getIndexNodes().get(i).getName());
    }

    public void seeMore() {
        container.findElement(By.className("toggle-more")).click();
    }

    protected abstract List<IndexNodeElement> getIndexNodes();

    protected abstract IndexNodeElement find(String name);

    protected abstract IndexCategoryNode findCategory(String name);

    @Override
    public Iterator<IndexNodeElement> iterator() {
        return getIndexNodes().iterator();
    }

    public WebElement getContainer() {
        return container;
    }

    protected WebDriver getDriver() {
        return driver;
    }
}

@@ -250,16 +250,67 @@
 ium.By;%0A
+import org.openqa.selenium.NoSuchElementException;%0A
 import o
@@ -1987,24 +1987,80 @@
 seeMore() %7B%0A
+        // TODO: only seeMore on Find%0A        try %7B%0A    
         cont
@@ -2111,24 +2111,126 @@
 )).click();%0A
+        %7D catch (NoSuchElementException e) %7B%0A            /* only exists on Find, not ISO */%0A        %7D%0A
     %7D%0A%0A    p

Fix index filter on ISO