package io.pivotal.cla.webdriver.pages.github;

import static org.assertj.core.api.Assertions.assertThat;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.WebDriverWait;

import com.google.common.base.Predicate;

import io.pivotal.cla.webdriver.pages.SignClaPage;

/**
 * Represents a Pull Request Page. For example:
 *
 * https://github.com/pivotal-cla/cla-test/pull/1
 *
 * @author Rob Winch
 *
 */
public class GitHubPullRequestPage {
	final WebDriver driver;

	public GitHubPullRequestPage(WebDriver driver) {
		super();
		this.driver = driver;
	}

	public void assertCommentPleaseSignFor(String gitHubUsername) {
		List<WebElement> comments = driver.findElements(By.cssSelector(".comment-content"));
		String text = "@" + gitHubUsername + " Please sign the Contributor License Agreement! Click here to manually synchronize the status of this Pull Request. See the FAQ for frequently asked questions.";

		waitForText(text);
		assertThat(comments).extracting(e-> e.getText().replaceAll("\n", " ")).contains(text);
	}

	public void assertCommentThankYouFor(String gitHubUsername) {
		List<WebElement> comments = driver.findElements(By.cssSelector(".comment-content"));
		String text = "@" + gitHubUsername + " Thank you for signing the Contributor License Agreement!";

		waitForText(text);
		assertThat(comments).extracting(WebElement::getText).contains(text);
	}

	public void assertBuildStatusSign() {
		List<WebElement> buildStatuses = driver.findElements(By.cssSelector(".build-status-item"));
		String text = "Details ci/pivotal-cla — Please sign the Contributor License Agreement!";

		waitForText(text);
		assertThat(buildStatuses).extracting(e-> e.getText().replaceAll("\n", " ")).contains(text);
	}

	public void assertBuildStatusSuccess() {
		List<WebElement> buildStatuses = driver.findElements(By.cssSelector(".build-status-item"));
		String text = "Details ci/pivotal-cla — Thank you for signing the Contributor License Agreement!";

		waitForText(text);
		assertThat(buildStatuses).extracting(e-> e.getText().replaceAll("\n", " ")).contains(text);
	}

	public SignClaPage details() {
		WebElement details = driver.findElement(By.cssSelector(".build-status-item a.build-status-details"));
		details.click();

		new GitHubAuthorizePage(driver).authorizeIfNecessary();

		return PageFactory.initElements(driver, SignClaPage.class);
	}

	private void waitForText(String text) {
		new WebDriverWait(driver, 60).until(new Predicate<WebDriver>() {
			@Override
			public boolean apply(WebDriver input) {
				return input.getPageSource().replaceAll("\n", " ").contains(text);
			}
		});
	}

}

@@ -104,32 +104,8 @@
 t;%0A%0A
-import java.util.List;%0A%0A
 impo
@@ -761,95 +761,8 @@
 ) %7B%0A
-%09%09List%3CWebElement%3E comments = driver.findElements(By.cssSelector(%22.comment-content%22));%0A
 %09%09St
@@ -998,65 +998,26 @@
 hat(
-comments).extracting(e-%3E e.getText().replaceAll(%22%5Cn%22, %22 %22
+getBodyText(driver
 )).c
@@ -1102,95 +1102,8 @@
 ) %7B%0A
-%09%09List%3CWebElement%3E comments = driver.findElements(By.cssSelector(%22.comment-content%22));%0A
 %09%09St
@@ -1237,48 +1237,27 @@
 hat(
-comments).extracting(WebElement::getText
+getBodyText(driver)
 ).co
@@ -1317,102 +1317,8 @@
 ) %7B%0A
-%09%09List%3CWebElement%3E buildStatuses = driver.findElements(By.cssSelector(%22.build-status-item%22));%0A
 %09%09St
@@ -1443,70 +1443,26 @@
 hat(
-buildStatuses).extracting(e-%3E e.getText().replaceAll(%22%5Cn%22, %22 %22
+getBodyText(driver
 )).c
@@ -1526,102 +1526,8 @@
 ) %7B%0A
-%09%09List%3CWebElement%3E buildStatuses = driver.findElements(By.cssSelector(%22.build-status-item%22));%0A
 %09%09St
@@ -1662,70 +1662,26 @@
 hat(
-buildStatuses).extracting(e-%3E e.getText().replaceAll(%22%5Cn%22, %22 %22
+getBodyText(driver
 )).c
@@ -2152,83 +2152,241 @@
 %09%09%09%09
-return input.getPageSource().replaceAll(%22%5Cn%22, %22 %22).contains(text);%0A%09%09%09%7D%0A%09%09%7D
+String body = getBodyText(input);%0A%09%09%09%09return body.contains(text);%0A%09%09%09%7D%0A%09%09%7D);%0A%09%7D%0A%0A%09private String getBodyText(WebDriver driver) %7B%0A%09%09WebElement body = driver.findElement(By.tagName(%22body%22));%0A%09%09return body.getText().replaceAll(%22%5Cn%22, %22 %22
 );%0A%09

Make GitHub assertions more lenient