package com.atlassian.webdriver.bitbucket.page.readme;

import com.atlassian.pageobjects.elements.PageElement;
import com.atlassian.pageobjects.elements.query.Conditions;
import com.atlassian.pageobjects.elements.timeout.TimeoutType;
import com.atlassian.webdriver.bitbucket.element.LegacyElementPageObject;
import com.google.common.base.Supplier;
import org.openqa.selenium.By;

import javax.annotation.Nonnull;

import static com.atlassian.pageobjects.elements.query.Poller.waitUntilFalse;
import static com.atlassian.pageobjects.elements.query.Poller.waitUntilTrue;

public class Readme extends LegacyElementPageObject {

    public Readme(@Nonnull PageElement element) {
        super(element);
    }

    public Readme(By locator, TimeoutType timeoutType) {
        super(locator, timeoutType);
    }

    private void waitUntilVisible() {
        waitUntilTrue(this.timed().isVisible());
    }

    public void waitUntilNotVisible() {
        waitUntilFalse(this.timed().isVisible());
    }

    public String getReadmeTitle() {
        waitUntilVisible();
        return this.find(By.className("filebrowser-readme-title")).getText();
    }

    public String getReadmeHtml() {
        waitUntilVisible();
        final By by = By.className("filebrowser-readme-content");
        waitUntilTrue(Conditions.forSupplier(5000, new Supplier<Boolean>() {
            @Override
            public Boolean get() {
                return !find(by).getText().isEmpty();
            }
        }));
        return find(by).getText();
    }

}
