package ca.corefacility.bioinformatics.irida.ria.integration.pages.projects;

import java.util.List;

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

import ca.corefacility.bioinformatics.irida.ria.integration.pages.AbstractPage;

/**
 * Page class for the project settings processing page
 */
public class ProjectSettingsProcessingPage extends AbstractPage {
	@FindBy(className = "t-analysis-template")
	private List<WebElement> existingTemplates;

	@FindBy(className = "t-create-template")
	private List<WebElement> createAnalysisButton;

	@FindBy(className = "t-remove-template")
	private List<WebElement> removeAnalysisButtons;

	@FindBy(className = "t-confirm-remove")
	private WebElement confirmRemoveButton;

	@FindBy(className = "t-template-modal")
	private WebElement templateModal;

	@FindBy(className = "t-select-template")
	private List<WebElement> selectTemplateButtons;

	public ProjectSettingsProcessingPage(WebDriver driver) {
		super(driver);
	}

	public static ProjectSettingsProcessingPage goToPage(WebDriver driver, Long projectId) {
		waitForTime(800);
		get(driver, "projects/" + projectId + "/settings/processing");
		return initPage(driver);
	}

	public static ProjectSettingsProcessingPage initPage(WebDriver driver) {
		return PageFactory.initElements(driver, ProjectSettingsProcessingPage.class);
	}

	public boolean isCreateAnalysisButtonVisible() {
		return createAnalysisButton.size() > 0;
	}

	public void clickCreateAnalysis() {
		this.createAnalysisButton.get(0).click();
		WebDriverWait wait = new WebDriverWait(driver, 2);
		wait.until(ExpectedConditions.visibilityOf(templateModal));
	}

	public void removeFirstAnalysis() {
		removeAnalysisButtons.get(0).click();

		WebDriverWait wait = new WebDriverWait(driver, 2);
		wait.until(ExpectedConditions.elementToBeClickable(confirmRemoveButton));
		confirmRemoveButton.click();
	}

	public int countAutomatedAnalyses() {
		return existingTemplates.size();
	}

	public void selectAutomatedTemplateByIndex(int index) {
		selectTemplateButtons.get(index).click();
	}
}

@@ -91,24 +91,55 @@
 util.List;%0A%0A
+import org.openqa.selenium.By;%0A
 import org.o
@@ -1314,28 +1314,8 @@
 ) %7B%0A
-%09%09waitForTime(800);%0A
 %09%09ge
@@ -1375,16 +1375,225 @@
 sing%22);%0A
+%09%09WebDriverWait wait = new WebDriverWait(driver, 10L);%0A%09%09wait.until(ExpectedConditions.visibilityOfElementLocated(%0A%09%09%09%09By.xpath(%22//h2%5Bcontains(@class, 'ant-typography') and text()='Automated Processing'%5D%22)));%0A
 %09%09return

Update ProjectSettingsProcessingPage to wait for page initialization before proceeding with tests.