package tables.wikipedia.pages;

import com.frameworkium.core.ui.annotations.Visible;
import com.frameworkium.core.ui.element.StreamTable;
import com.frameworkium.core.ui.pages.BasePage;
import com.frameworkium.core.ui.pages.PageFactory;
import org.openqa.selenium.NotFoundException;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.CacheLookup;
import org.openqa.selenium.support.FindBy;

import java.util.stream.Stream;

public class EnglishCountiesPage extends BasePage<EnglishCountiesPage> {

    @Visible
    @CacheLookup
    @FindBy(css = "table.wikitable") // luckily there's only one
    private StreamTable listTable;

    public static EnglishCountiesPage open() {
        return PageFactory.newInstance(EnglishCountiesPage.class,
                "https://en.wikipedia.org/wiki/List_of_ceremonial_counties_of_England");
    }

    public int populationOf(String countyName) {
        String lookupHeader = "County";
        String targetHeader = "Population";
        String population = listTable
                .getCellsByLookup(lookupHeader, countyName, targetHeader)
                .findFirst()
                .orElseThrow(NotFoundException::new)
                .getText()
                .replaceAll(",", "");
        return Integer.parseInt(population);
    }

    public Stream<Integer> densities() {
        return listTable
                .getColumn(e -> e.getText().startsWith("Density"))
                .map(WebElement::getText)
                .map(density -> density.replaceAll(",", ""))
                .map(Integer::parseInt);
    }
}

@@ -408,16 +408,53 @@
 indBy;%0A%0A
+import java.util.function.Predicate;%0A
 import j
@@ -953,70 +953,270 @@
     
-String lookupHeader = %22County%22;%0A        String
+Predicate%3CWebElement%3E headerLookUp = e -%3E e.getText().trim().equals(%22County%22);%0A        Predicate%3CWebElement%3E lookUpCellMatcher = e -%3E e.getText().trim().equals(countyName);%0A        Predicate%3CWebElement%3E
  target
+Col
 Header
- = 
+Lookup = e -%3E e.getText().trim().startsWith(
 %22Pop
@@ -1223,16 +1223,17 @@
 ulation%22
+)
 ;%0A      
@@ -1302,40 +1302,47 @@
 kup(
-lookupHeader, countyName
+headerLookUp, lookUpCellMatcher
 , target
 Head
@@ -1337,22 +1337,31 @@
 , target
+Col
 Header
+Lookup
 )%0A      

Fix failing Wikipedia Test