package pt.fccn.sobre.arquivo.pages;

import java.io.FileNotFoundException;
import java.nio.charset.Charset;
import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import pt.fccn.arquivo.tests.util.AnalyzeURLs;

public class NewsOnMediaPage {

	WebDriver driver;
	private final int timeout = 50;
	
	public NewsOnMediaPage( WebDriver driver ) throws FileNotFoundException{
		this.driver = driver;
		
	}
	
	public boolean checkNewsLinks( String language ) {
		System.out.println( "[checkNewsLinks]" );
		String xpatha = "";
		if( language.equals( "EN" ) ){
			xpatha = "//*[@id=\"post-2812\"]/div/div/ul/li/a"; //get links
			switchLanguage( );
		}
		else
			xpatha = "//*[@id=\"post-2635\"]/div/div/ul/li/a"; //get links
		
		try{
    		List< WebElement > results = ( new WebDriverWait( driver, timeout ) )
	                .until( ExpectedConditions
	                        .visibilityOfAllElementsLocatedBy(
	                        		      By.xpath( xpatha )
	                        )
	        );
    		
    		System.out.println( "[News Links] results size = " + results.size( ) );
    		for( WebElement elem : results ) {
    			String url = elem.getAttribute( "href" );
    			int statusCode = AnalyzeURLs.linkExists( url );
    			if( !AnalyzeURLs.checkOk( statusCode ) ) {
    				System.out.println( "Failed: text[" + Charset.forName( "UTF-8" ).encode( elem.getText( ) ) + "] link[" + url + "] status-code[" + statusCode + "]" );
    				return false;
    			}
    		}
    		
	    	return true;
    	} catch( NoSuchElementException e ){
            System.out.println( "Error in checkNewsLinks" );
            e.printStackTrace( );
            return false;
    	}
		
	}
	
    /**
    * Change to the English version
    */
    private void switchLanguage( ){
    	String xpathEnglishVersion = "//*[@id=\"menu-item-3862-en\"]/a";
    	//TODO //*[@id=\"menu-item-3862-en\"]/a -> new template 
      	if( driver.findElement( By.xpath( xpathEnglishVersion ) ).getText( ).equals( "English" ) ) {
      		System.out.println( "Change language to English" );
      		driver.findElement( By.xpath( xpathEnglishVersion ) ).click( );
      		IndexSobrePage.sleepThread( );
      	}
    }
	
	
}

@@ -743,25 +743,27 @@
 String xpath
-a
+Div
  = %22%22;%0A%09%09if(
@@ -790,33 +790,35 @@
 N%22 ) )%7B%0A%09%09%09xpath
-a
+Div
  = %22//*%5B@id=%5C%22po
@@ -831,32 +831,24 @@
 2%5C%22%5D/div/div
-/ul/li/a
 %22; //get lin
@@ -891,17 +891,19 @@
 %09%09%09xpath
-a
+Div
  = %22//*%5B
@@ -932,16 +932,8 @@
 /div
-/ul/li/a
 %22; /
@@ -957,20 +957,16 @@
 ry%7B%0A
+%0A 
     %09%09
-List%3C 
 WebE
@@ -976,17 +976,14 @@
 ent 
-%3E results
+divTag
  = (
@@ -1025,53 +1025,69 @@
  ) )
-%0A%09                .until( 
+ /* Wait Up to 50 seconds should throw RunTime
 Ex
+c
 pe
-ctedConditions%0A
+tion*/%0A   
 %09   
@@ -1103,56 +1103,87 @@
     
+.until(%0A
    
+%09
      
-.visibilityOfAll
+           %09%09ExpectedConditions.presenceOf
 Element
-s
 Located
-By
 (%0A
-%09
    
- 
+%09
     
@@ -1198,20 +1198,12 @@
     
-    %09%09      
+%09%09%09%09
 By.x
@@ -1217,24 +1217,31 @@
 path
-a )%0A%09
+Div ) ) );%0A
    
+%09
         
     
@@ -1240,35 +1240,135 @@
     
-             )%0A%09       
+%0A%09%09%09//find all the a tags in the div tag%0A   %09%09   %09List%3C WebElement %3E allAnchors = divTag.findElements( By.tagName( %22a%22 )
  );%0A
+   
     
@@ -1429,22 +1429,25 @@
 e = %22 + 
-result
+allAnchor
 s.size( 
@@ -1480,22 +1480,25 @@
  elem : 
-result
+allAnchor
 s ) %7B%0A  
@@ -1606,16 +1606,175 @@
 %0A    %09%09%09
+System.out.println( %22Failed: text%5B%22 + Charset.forName( %22UTF-8%22 ).encode( elem.getText( ) ) + %22%5D link%5B%22 + url + %22%5D status-code%5B%22 + statusCode + %22%5D%22 );%0A    %09%09%09/*
 if( !Ana
@@ -1996,16 +1996,18 @@
     %09%09%09%7D
+*/
 %0A    %09%09%7D

Add NaviagationTest