package pt.fccn.sobre.arquivo.pages;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

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

public class CommonQuestionsPage {
	
	WebDriver driver;
	List< String > CommonQuestionsPT;
	List< String > CommonQuestionsEN;
	private final String dir = "sobreTestsFiles";
	private final int timeout = 50;
	
	public CommonQuestionsPage( WebDriver driver ) throws FileNotFoundException{
		this.driver = driver;
		CommonQuestionsPT = new ArrayList< String >( );
		CommonQuestionsEN = new ArrayList< String >( );
		if( !loadQuestions( "CommonQuestions_pt.txt" , "pt" ) ) 
			throw new FileNotFoundException( );
		
		if( !loadQuestions( "CommonQuestions_en.txt" , "en" ) )
			throw new FileNotFoundException( );
		
	}
	
	
	private boolean loadQuestions( String filename , String language ) {
		Scanner s;
		try {
			s = new Scanner( new File( dir.concat( File.separator ).concat( filename ) ) , "utf-8" );
			while ( s.hasNext( ) ) {
				if( language.equals( "pt" ) )
					CommonQuestionsPT.add( s.next( ) );
				else
					CommonQuestionsEN.add( s.next( ) );
			}
			s.close( );
			return true;
		} catch ( FileNotFoundException e ) {
			e.printStackTrace( );
			return false;
		}
	}
	
	
	public boolean inspectQuestions( String language ) {
		System.out.println( "[inspectQuestions]" );
    	String xpathDivs = "//*[@id=\"post-2096\"]/div/div/div/h3";
    	try{
    		List< WebElement > results = ( new WebDriverWait( driver, timeout ) )
	                .until( ExpectedConditions
	                        .visibilityOfAllElementsLocatedBy(
	                        		      By.xpath( xpathDivs )
	                        )
	        );
	        
	        for( WebElement elem : results ) { 
	    		System.out.println( "Elemento Text = " + elem.getText( ) );
	    		
	    	}
	        
	    	return true;
    	} catch( Exception e ){
            System.out.println("Error in checkOPSite");
            e.printStackTrace();
            return false;
    	}

	}
}

@@ -90,16 +90,49 @@
 eption;%0A
+import java.nio.charset.Charset;%0A
 import j
@@ -1670,16 +1670,34 @@
 iv/h3%22;%0A
+    %09int idx = 0;%0A
     %09try
@@ -1986,118 +1986,411 @@
    %0A
-%09        for( WebElement elem : results ) %7B %0A%09    %09%09System.out.println( %22Elemento Text = %22 + elem.getText( ) )
+    %09%09if( CommonQuestionsPT.size( ) != results.size( ) )%0A    %09%09%09return false;%0A    %09%09%0A%09        for( WebElement elem : results ) %7B %0A%09    %09%09String question = elem.getText( );%0A%09    %09%09Charset.forName( %22UTF-8%22 ).encode( question );%0A%09    %09%09System.out.println( %22question = %22 + question + %22 != %22 + CommonQuestionsPT.get( idx ) );%0A%09    %09%09if( !question.equals( CommonQuestionsPT.get( idx ) ) )%0A%09    %09%09%09return false
 ;%0A%09 
@@ -2390,24 +2390,30 @@
 lse;%0A%09    %09%09
+idx++;
 %0A%09    %09%7D%0A%09  

work progress v4