package pt.fccn.sobre.arquivo.tests;

import static org.junit.Assert.assertEquals;

import java.util.List;
import java.util.Locale;

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;
import pt.fccn.mobile.arquivo.utils.LocalizedString;
import pt.fccn.sobre.arquivo.pages.IndexSobrePage;


/**
 * 
 * @author pedro.gomes.fccn@gmail.com
 *
 */

public class Soft404MessageTest extends WebDriverTestBaseParalell {

	private static final String WAYBACK_404_PAGE_EXAMPLE = "/about-the-archive/publications-1/documents/peopleware-rarc-article-in-jpg/image_preview";
	
	public Soft404MessageTest(String os, String version, String browser, String deviceName, String deviceOrientation) {
		super(os, version, browser, deviceName, deviceOrientation);
		
	}
	
	@Test
	@Retry
	public void soft404MessageTestPT()  {
		soft404MessageTest(LocaleUtils.PORTUGUESE);
	}
	
	@Test
	@Retry
	public void soft404MessageTestEN() {
		soft404MessageTest(LocaleUtils.ENGLISH);
		switchLanguage( );
	}
	

	public void soft404MessageTest(Locale locale) {
		
		if (!this.testURL.toLowerCase().contains("preprod.sobre.arquivo.pt")) {
			driver.get(this.testURL + WAYBACK_404_PAGE_EXAMPLE);
			
			waitUntilElementIsVisibleAndGet(By.id("post-9818"));
			
			LocalizedString listText = new LocalizedString().pt("Visite uma versão anterior desta página em 2017-02-27 no Arquivo.pt").en("Browse a previous version of this page in 2017-02-28 on Arquivo.pt");
			
			appendError(() -> assertEquals("Verify text from Arquivo.pt link", listText.apply(locale),
					driver.findElement(By.id("onArquivo")).getText()));
			
			List<WebElement> wes = driver.findElementsByXPath("//*[@id=\"onArquivo\"]");
			WebElement we = wes.get(0);
			String href = we.getAttribute("href");
			assertEquals("Check link to wayback link", href, "https://arquivo.pt/wayback/20170227184149/https://sobre.arquivo.pt" + WAYBACK_404_PAGE_EXAMPLE);
		}
	}
	
	private void switchLanguage( ){
    	String xpathEnglishVersion = "//*[@id=\"menu-item-4424-en\"]/a";
      	if( driver.findElement( By.xpath( xpathEnglishVersion ) ).getText( ).equals( "English" ) ) {
      		System.out.println( "Change language to English" );
      		driver.findElement( By.xpath( xpathEnglishVersion ) ).click( );
      		IndexSobrePage.sleepThread( );
      	}
    }

}

@@ -218,16 +218,127 @@
 Element;
+%0Aimport org.openqa.selenium.support.ui.ExpectedConditions;%0Aimport org.openqa.selenium.support.ui.WebDriverWait;
 %0A%0Aimport
@@ -1094,241 +1094,127 @@
 Test
-PT
 ()
- 
  %7B%0A%09%09
-soft404MessageTest(LocaleUtils.PORTUGUESE);%0A%09%7D%0A%09%0A%09@Test%0A%09@Retry%0A%09public void soft404MessageTestEN() %7B%0A%09%09soft404MessageTest(LocaleUtils.ENGLISH);%0A%09%09switchLanguage( );%0A%09%7D%0A%09%0A%0A%09public void soft404MessageTest(Locale locale) %7B%0A%09%09
+%0A%09%09//The test does not work in preprod.sobre.arquivo.pt. Since the Arquivo.pt only collect the sobre.arquivo.pt.
 %0A%09%09i
@@ -1407,212 +1407,141 @@
 %0A%09%09%09
+ 
 %0A%09%09%09
-LocalizedString listText = new LocalizedString().pt(%22Visite uma vers%C3%A3o anterior desta p%C3%A1gina em 2017-02-27 no Arquivo.pt%22).en(%22Browse a previous version of this page in 2017-02-28 on Arquivo.pt%22);%0A%09%09%09
+//The language of the page is not based on the %22English or Portugues%22 button, but depends on the language of the search engine. 
 %0A%09%09%09
@@ -1611,30 +1611,76 @@
 k%22, 
-listText.apply(locale)
+%22Browse a previous version of this page in 2017-02-28 on Arquivo.pt%22
 ,%0A%09%09
@@ -1929,21 +1929,16 @@
  wayback
- link
 %22, href,
@@ -2042,129 +2042,117 @@
 ;%0A%09%09
-%7D%0A%09%7D%0A%09%0A%09private void switchLanguage( )%7B%0A    %09String xpathEnglishVersion = %22//*%5B@id=%5C%22menu-item-4424-en%5C%22%5D/a%22;%0A      %09if( 
+%09%0A%09%09%09//Test search%0A%09%09%09appendError(() -%3E assertEquals(%22Verify text from search%22, %22Maybe try searching?%22,%0A%09%09%09%09%09
 driv
@@ -2170,262 +2170,220 @@
 ent(
- 
 By.
-xpath( xpathEnglishVersion ) ).getText( ).equals( %22English%22 ) ) %7B%0A      %09%09System.out.println( %22Change language to English%22 );%0A      %09%09driver.findElement( By.xpath( xpathEnglishVersion ) ).click( );%0A      %09%09IndexSobrePage.sleepThread( );%0A      %09%7D%0A    
+id(%22messageSearch%22)).getText()));%0A%09%09%09%0A%09%09%09appendError(%22Check if page is not archived%22, () -%3E new WebDriverWait(driver, 20)%0A%09%09%09%09%09.until(ExpectedConditions.visibilityOfElementLocated(By.id(%22___gcse_2%22))));%0A%0A%09%09%7D%0A%09
 %7D%0A%0A%7D

Fix 404 test