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

import java.util.Collection;
import java.util.List;
import java.util.StringJoiner;
import java.util.stream.Collectors;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;

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

/**
 * Page to test the NCBI export feature
 */
public class NcbiExportPage extends AbstractPage {

	@FindBy(className = "sample-container")
	List<WebElement> samples;

	@FindBy(className = "sample-no-files")
	List<WebElement> disabledSamples;

	@FindBy(className = "bioSample")
	List<WebElement> bioSampleInputs;

	@FindBy(className = "library_construction_protocol")
	List<WebElement> protocolInputs;

	@FindBy(id = "submit")
	WebElement submitButton;

	@FindBy(id = "bioProject")
	WebElement bioProject;

	@FindBy(id = "organization")
	WebElement organization;

	@FindBy(id = "namespace")
	WebElement namespace;

	@FindBy(className = "remove-button")
	List<WebElement> removeButtons;

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

	public static NcbiExportPage goTo(WebDriver driver, Long projectId, Collection<Long> sampleIds) {

		StringJoiner stringJoiner = new StringJoiner("&");
		sampleIds.forEach(s -> stringJoiner.add("ids%5B%5D=" + s.toString()));

		String url = "projects/" + projectId + "/export/ncbi?" + stringJoiner.toString();
		get(driver, url);
		return PageFactory.initElements(driver, NcbiExportPage.class);
	}

	public List<String> getSampleNames() {
		return samples.stream().map(s -> s.findElement(By.className("sample-name")).getText())
				.collect(Collectors.toList());
	}

	public int countDisabledSamples() {
		return disabledSamples.size();
	}

	public void fillTopLevelProperties(String bioProject, String organization, String namespace) {
		this.bioProject.sendKeys(bioProject);
		this.organization.sendKeys(organization);
		this.namespace.sendKeys(namespace);
	}

	public void fillSamplesWithInfo(String bioSample, String protocol) {
		bioSampleInputs.forEach(b -> b.sendKeys(bioSample));
		protocolInputs.forEach(c -> c.sendKeys(protocol));
	}

	public void submit() {
		submitButton.click();
	}

	public void removeFirstSample() {
		removeButtons.iterator().next().click();
	}

}

@@ -389,16 +389,62 @@
 Factory;
+%0Aimport org.springframework.web.util.UriUtils;
 %0A%0Aimport
@@ -1469,18 +1469,35 @@
 add(
-%22ids%255B%255D
+UriUtils.encodeQuery(%22ids%5B%5D
 =%22 +
@@ -1509,16 +1509,26 @@
 String()
+, %22UTF-8%22)
 ));%0A%0A%09%09S

Use UriUtils to encode query params for NcbiExportPage.java