package cz.moneta.test.harness.connectors.web;

import cz.moneta.test.harness.support.web.Lookup;
import org.openqa.selenium.WebElement;

import java.util.List;

public class ElementsChecks {

    private final SeleniumWebConnector connector;

    public ElementsChecks(SeleniumWebConnector connector) {
        this.connector = connector;
    }

    public boolean isElementPresent(String path, Lookup lookup) {
        return !connector.getDriver().findElements(connector.resolveLookup(lookup).apply(path)).isEmpty();
    }

    public boolean isElementEnabled(String path, Lookup lookup) {
        return connector.waits().waitForLazyElement(path, lookup).isEnabled();
    }

    public void isAtLeastOneElementPresent(String xpath, Lookup lookup) {
        connector.waits()
                .waitForLazyElement(xpath, lookup);
    }

    public void checkElementContent(String path, String content, Lookup lookup) {
        List<WebElement> elements = connector.waits().waitForLazyElements(path, lookup);
        elements.stream()
                .map(WebElement::getText)
                .filter(text -> text.contains(content))
                .findFirst()
                .orElseThrow(() -> new AssertionError(String.format("Cannot find element that contains text %s", content)));
    }
}
