package pt.fccn.sobre.arquivo.pages;

import java.io.File;
import java.io.FileNotFoundException;
import java.nio.charset.Charset;
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( );
			
			for( String question : CommonQuestionsPT ) {
				System.out.println( "QuestionPT => " + question );
			}
			
			for( String question : CommonQuestionsEN ) {
				System.out.println( "QuestionEN => " + question );
			}
			
			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";
    	int idx = 0;
    	try{
    		List< WebElement > results = ( new WebDriverWait( driver, timeout ) )
	                .until( ExpectedConditions
	                        .visibilityOfAllElementsLocatedBy(
	                        		      By.xpath( xpathDivs )
	                        )
	        );
	        System.out.println( "List["+CommonQuestionsPT.size( )+"] != Results["+results.size( )+"]" );
    		if( CommonQuestionsPT.size( ) != results.size( ) )
    			return false;
    		
	        for( WebElement elem : results ) { 
	    		String question = elem.getText( );
	    		Charset.forName( "UTF-8" ).encode( question );
	    		System.out.println( "question = " + question + " != " + CommonQuestionsPT.get( idx ) );
	    		if( !question.equals( CommonQuestionsPT.get( idx ) ) )
	    			return false;
	    		idx++;
	    	}
	        
	    	return true;
    	} catch( Exception e ){
            System.out.println("Error in checkOPSite");
            e.printStackTrace();
            return false;
    	}

	}
}

@@ -50,50 +50,203 @@
 .io.
-File;%0Aimport java.io.FileNotFoundException
+BufferedReader;%0Aimport java.io.File;%0Aimport java.io.FileInputStream;%0Aimport java.io.FileNotFoundException;%0Aimport java.io.IOException;%0Aimport java.io.InputStream;%0Aimport java.io.InputStreamReader
 ;%0Aim
@@ -1290,122 +1290,296 @@
 %0A%09%09%09
-s = new Scanner( new File( dir.concat( File.separator ).concat( filename ) ) , %22utf-8%22 
+String line;%0A%0A%09%09    InputStream fis = new FileInputStream( dir.concat( File.separator ).concat( filename ) );%0A%09%09    InputStreamReader isr = new InputStreamReader(fis, Charset.forName(%22UTF-8%22));%0A%09%09    BufferedReader br = new BufferedReader(isr
 );%0A%09%09
-%09
+    
 while (
- s.hasNext( ) 
+(line = br.readLine()) != null
 ) %7B%0A
@@ -1636,33 +1636,28 @@
 ionsPT.add( 
-s.next( )
+line
  );%0A%09%09%09%09else
@@ -1689,29 +1689,59 @@
 dd( 
-s.next( ) );%0A%09%09%09%7D
+line );%0A%09%09%09%7D%0A%09%09%09br.close( );%0A%09%09%09isr.close( );
 %0A%09%09%09
+fi
 s.cl
@@ -2027,24 +2027,29 @@
 eption e
+xFile
  ) %7B%0A%09%09%09
 e.printS
@@ -2040,16 +2040,21 @@
  ) %7B%0A%09%09%09
+exFil
 e.printS
@@ -2087,16 +2087,97 @@
 lse;%0A%09%09%7D
+ catch ( IOException exIo ) %7B%0A%09%09%09exIo.printStackTrace( );%0A%09%09%09return false;%0A%09%09%7D%0A%09%09
 %0A%09%7D%0A%09%0A%09%0A

work progress v7