/**
 * 
 */
package at.porscheinformatik.seleniumcomponents.clarity;

import static at.porscheinformatik.seleniumcomponents.WebElementSelector.*;

import org.openqa.selenium.Keys;

import at.porscheinformatik.seleniumcomponents.AbstractSeleniumComponent;
import at.porscheinformatik.seleniumcomponents.AbstractSeleniumContainer;
import at.porscheinformatik.seleniumcomponents.SeleniumComponent;
import at.porscheinformatik.seleniumcomponents.SeleniumComponentFactory;
import at.porscheinformatik.seleniumcomponents.SeleniumComponentList;
import at.porscheinformatik.seleniumcomponents.WebElementSelector;
import at.porscheinformatik.seleniumcomponents.component.HtmlComponent;
import at.porscheinformatik.seleniumcomponents.component.InputComponent;

/**
 * @author Daniel Furtlehner
 * @param <RowT> type of rows in this grid
 */
public abstract class AbstractClarityDataGridComponent<RowT extends AbstractSeleniumComponent>
    extends AbstractSeleniumComponent
{
    private final HtmlComponent gridContainer = new HtmlComponent(this, selectByClassName("datagrid"));
    private final DataGridHeadComponent head = new DataGridHeadComponent(gridContainer);
    private final DataGridBodyComponent<RowT> body;

    protected AbstractClarityDataGridComponent(SeleniumComponent parent, SeleniumComponentFactory<RowT> rowFactory)
    {
        super(parent, selectByTagName("clr-datagrid"));

        body = new DataGridBodyComponent<>(this, rowFactory);
    }

    public void stringFilterColumn(int columnIndex, String filterQuery)
    {
        DataGridColumnComponent column = head.getColumn(columnIndex);

        column.stringFilter(filterQuery);
    }

    public SeleniumComponentList<RowT> getEntries()
    {
        return body.findAllChilds();
    }

    private static class DataGridHeadComponent extends AbstractSeleniumContainer<DataGridRowComponent>
    {
        DataGridHeadComponent(SeleniumComponent parent)
        {
            super(parent, selectByClassName("datagrid-header"), selectByClassName("datagrid-row"),
                DataGridRowComponent::new);
        }

        public DataGridColumnComponent getColumn(int index)
        {
            return findAllChilds().get(0).findAllChilds().get(index);
        }
    }

    private static class DataGridBodyComponent<RowT extends AbstractSeleniumComponent>
        extends AbstractSeleniumContainer<RowT>
    {
        DataGridBodyComponent(SeleniumComponent parent, SeleniumComponentFactory<RowT> rowFactory)
        {
            super(parent, selectByClassName("datagrid"), selectByCss(".datagrid-table > .datagrid-row"), rowFactory);
        }
    }

    private static class DataGridRowComponent extends AbstractSeleniumContainer<DataGridColumnComponent>
    {
        protected DataGridRowComponent(SeleniumComponent parent, WebElementSelector selector)
        {
            super(parent, selector, selectByClassName("datagrid-column"), DataGridColumnComponent::new);
        }
    }

    private static class DataGridColumnComponent extends AbstractSeleniumComponent
    {
        private final HtmlComponent filterContainer = new HtmlComponent(this, selectByTagName("clr-dg-filter"));
        private final InputComponent stringFilter = new InputComponent(filterContainer);

        DataGridColumnComponent(SeleniumComponent parent, WebElementSelector selector)
        {
            super(parent, selector);
        }

        public void stringFilter(String filterQuery)
        {
            filterContainer.click();

            stringFilter.clear();
            stringFilter.sendKeys(filterQuery);

            stringFilter.sendKeys(Keys.ENTER);
        }
    }
}

@@ -63,16 +63,87 @@
 arity;%0A%0A
+import static at.porscheinformatik.seleniumcomponents.SeleniumUtils.*;%0A
 import s
@@ -3117,24 +3117,130 @@
 onent%0A    %7B%0A
+        // With newer Clarity versions the datagrid filters are in the body and not in the column anymore%0A
         priv
@@ -3387,24 +3387,36 @@
 ringFilter =
+%0A           
  new InputCo
@@ -3423,31 +3423,65 @@
 mponent(
-filterContainer
+root(this), selectByCss(%22.datagrid-filter input%22)
 );%0A%0A    

Fix for newese clarity version