package pt.fccn.mobile.arquivo.tests.imagesearch;

import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import java.util.List;
import java.util.Optional;

import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import pt.fccn.arquivo.selenium.Retry;
import pt.fccn.arquivo.selenium.WebDriverTestBaseParalell;
import pt.fccn.mobile.arquivo.utils.LocaleUtils;

/**
 *
 * @author ivo.branco@fccn.pt
 *
 */
public class ImageSearchDirectUrlTest extends WebDriverTestBaseParalell {

	private static final String IMAGE_SEARCH_DIRECT_URL = "/images.jsp?l=en&size=all&type=&tools=off&safeSearch=on&query=fccn&btnSubmit=Search&dateStart=26%2F06%2F2007&dateEnd=27%2F06%2F2007";

	public ImageSearchDirectUrlTest(String os, String version, String browser, String deviceName,
			String deviceOrientation) {
		super(os, version, browser, deviceName, deviceOrientation);
	}

	@Test
	@Retry
	public void testImageSearchDirectUrlNoLanguage() throws Exception {
		testImageSearchDirectUrl(testURL + IMAGE_SEARCH_DIRECT_URL, Optional.empty());
	}

	@Test
	@Retry
	public void testImageSearchDirectUrlPt() throws Exception {
		testImageSearchDirectUrl(testURL + IMAGE_SEARCH_DIRECT_URL + "&" + LocaleUtils.languagePTUrlQueryParameter(),
				Optional.of("Imagens"));
	}

	@Test
	@Retry
	public void testImageSearchDirectUrlEn() throws Exception {
		testImageSearchDirectUrl(testURL + IMAGE_SEARCH_DIRECT_URL + "&" + LocaleUtils.languageENUrlQueryParameter(),
				Optional.of("Images"));
	}

	private void testImageSearchDirectUrl(String url, Optional<String> imageButtonText) {
		driver.get(url);

		WebElement firstImage = waitUntilElementIsVisibleAndGet(By.id("imageResults0"));
		assertNotNull("Should exist at least one image", firstImage);

		appendError(() -> {
			List<WebElement> photoDivList = driver.findElements(By.xpath("//*[@id=\"photos\"]/div"));
			assertEquals("Verify results count", 1, photoDivList.size());
		});

		appendError(() -> assertThat("Check image original origin/domain",
				driver.findElement(By.xpath("//*[@id=\"imageResults0\"]/p[1]")).getText(),
				containsString("sag.fccn.pt")));

		if (imageButtonText.isPresent()) {
			appendError(() -> assertEquals("Check page language by verifying images button text", imageButtonText.get(),
					driver.findElementById("ImageButton").getText()));
		}
	}

}

@@ -1060,37 +1060,33 @@
 ry%0A%09public void 
-testI
+i
 mageSearchDirect
@@ -1098,16 +1098,20 @@
 Language
+Test
 () throw
@@ -1118,37 +1118,33 @@
 s Exception %7B%0A%09%09
-testI
+i
 mageSearchDirect
@@ -1138,32 +1138,36 @@
 eSearchDirectUrl
+Test
 (testURL + IMAGE
@@ -1229,37 +1229,33 @@
 ry%0A%09public void 
-testI
+i
 mageSearchDirect
@@ -1258,16 +1258,20 @@
 rectUrlP
+TTes
 t() thro
@@ -1279,37 +1279,33 @@
 s Exception %7B%0A%09%09
-testI
+i
 mageSearchDirect
@@ -1299,32 +1299,36 @@
 eSearchDirectUrl
+Test
 (testURL + IMAGE
@@ -1454,29 +1454,25 @@
 public void 
-testI
+i
 mageSearchDi
@@ -1479,17 +1479,21 @@
 rectUrlE
-n
+NTest
 () throw
@@ -1508,21 +1508,17 @@
 ion %7B%0A%09%09
-testI
+i
 mageSear
@@ -1528,16 +1528,20 @@
 irectUrl
+Test
 (testURL
@@ -1668,13 +1668,9 @@
 oid 
-testI
+i
 mage
@@ -1684,16 +1684,20 @@
 irectUrl
+Test
 (String 

Rename test case