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

import static org.junit.Assert.assertEquals;

import java.util.List;

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 <ivo.branco@fccn.pt>
 *
 */
public class URLSearchTableTest extends WebDriverTestBaseParalell {

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

	@Test
	@Retry
	public void urlSearchTableTestPT() {
		LocaleUtils.changeLanguageToPT(this);
		urlSearchTableTest("fccn.pt", " Tabela ", "13 out");
	}

	@Test
	@Retry
	public void urlSearchTableTestEN() {
		LocaleUtils.changeLanguageToEN(this);
		urlSearchTableTest("fccn.pt", " Table ", "13 Oct");
	}

	private void urlSearchTableTest(String url, String tableText, String firstResultText) {

		run("Search fccn.pt url", () -> {
			driver.findElement(By.id("txtSearch")).clear();
			driver.findElement(By.id("txtSearch")).sendKeys(url);
			driver.findElement(By.xpath("//*[@id=\"buttonSearch\"]/button")).click();
		});
		
		//We have a iframe inside the page. Thus, to find the elements we need to change the iframe
		driver.switchTo().defaultContent(); // we are now outside both frames
		driver.switchTo().frame("url_search_iframe");
		
		run("Change to Table mode if not in it", () -> {
			WebElement resultsGridCurrentType = driver.findElementByXPath("//*[@id=\"layoutTV\"]/button/h4");
			if (!resultsGridCurrentType.getText().contains(tableText)) {
				driver.findElementByXPath("//*[@id=\"layoutTV\"]/button").click();
			}
		});
		
		System.out.println("Current url: " + driver.getCurrentUrl());
		
		run("Check if first version match", () -> {
			waitUntilElementIsVisibleAndGet(By.id("conteudo-versoes"));
			List<WebElement> firstRow = driver.findElementsByXPath("//*[@id=\"1\" and @class=\"trTV\"]/td");
			WebElement firstCell = firstRow.get(0);
			WebElement anchor = firstCell.findElement(By.xpath(".//a"));
			String visibleDate = anchor.getText();
			assertEquals("Check if first version match", firstResultText, visibleDate);			
		});
	}
}

@@ -110,16 +110,41 @@
 il.List;
+%0Aimport java.util.Locale;
 %0A%0Aimport
@@ -329,16 +329,114 @@
 ralell;%0A
+import pt.fccn.mobile.arquivo.utils.BundleMessage;%0Aimport pt.fccn.mobile.arquivo.utils.DateUtils;%0A
 import p
@@ -931,16 +931,40 @@
 %2213 out%22
+, LocaleUtils.PORTUGUESE
 );%0A%09%7D%0A%0A%09
@@ -1106,16 +1106,37 @@
 %2213 Oct%22
+, LocaleUtils.ENGLISH
 );%0A%09%7D%0A%0A%09
@@ -1219,16 +1219,31 @@
 sultText
+, Locale locale
 ) %7B%0A%0A%09%09r
@@ -2500,13 +2500,236 @@
 %09%0A%09%09%7D);%0A
+%09%09%0A%09%09appendError(%22Verify specific timetamp%22, () -%3E %7B%0A%09%09%09String timestamp = %2219961013145650%22;%0A%09%09%09WebElement dayWE = waitUntilElementIsVisibleAndGet(By.id(timestamp));%0A%09%09%09assertEquals(firstResultText, dayWE.getText());%0A%09%09%7D);%0A
 %09%7D%0A%7D%0A

add new step to verify a specific timestamp