package innovimax.mixthem;

import innovimax.mixthem.arguments.Rule;
import innovimax.mixthem.exceptions.MixException;

import java.io.*;
import java.net.URL;
import java.util.Collections;
import java.util.List;
import java.util.logging.ConsoleHandler;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.junit.Assert;
import org.junit.Test;
import org.junit.experimental.theories.*;
import org.junit.runner.RunWith;

/*
	Created by innovimax
	Generic tests for this application
*/
public class GenericTest {
	
   private static Logger LOGGER = Logger.getLogger(GenericTest.class.getName());

   private static void setLogging() {
        System.setProperty("java.util.logging.SimpleFormatter.format", "[%4$s] MixThem: %5$s [%1$tc]%n");
        String prop = System.getProperty("mixthem.logging");
        if (prop == null || prop.equals("true")) {
            LOGGER.setLevel(Level.INFO);
        } else {
            LOGGER.setLevel(Level.OFF);
        }
        LOGGER.addHandler(new ConsoleHandler());        
   }
	
   @Test
   public final void parameter() throws MixException, FileNotFoundException, IOException {
	   setLogging();
	   int testId = 1;
	   boolean result = true;
	   RuleRuns ruleRuns = new RuleRuns();
	   while (true) {
		   System.out.println("TEST n°" + testId);
		   String prefix = "test" + String.format("%03d", testId) +"_";
		   URL url1 = getClass().getResource(prefix + "file1.txt");
		   URL url2 = getClass().getResource(prefix + "file2.txt");
		   System.out.println("URL 1 ("+prefix + "file1.txt"+")="+url1+"; URL 2 ("+prefix + "file2.txt"+")="+url2);
		   if( url1 == null || url2 == null) break;
		   for(Rule rule : Rule.values()) {
			   String resource = prefix+"output-"+ rule.getExtension()+".txt";
			   URL url = getClass().getResource(resource);
			   System.out.println(rule+" implemented = "+rule.isImplemented()+" ; resource ("+resource+") = "+url);
			   if (rule.isImplemented() && url != null) {			   	
				List<RuleRun> runs = ruleRuns.getRuns(rule);
				for (RuleRun run : runs) {
					if (run.accept(testId)) {
						boolean res = check(new File(url1.getFile()), new File(url2.getFile()), new File(url.getFile()), rule, run.getParams());
						System.out.println("RULE PASS : "+res + " / PARAMS" + run.getParams().toString());
						result &= res;   
					}
				}
			   }
		   }   
		   testId++;
	   }
	   Assert.assertTrue(result);
   }	   
   private final static boolean check(File file1, File file2, File expected, Rule rule, List<String> params)  throws MixException, FileNotFoundException, IOException  {
	   ByteArrayOutputStream baos_rule = new ByteArrayOutputStream();
	   MixThem mixThem = new MixThem(file1, file2, baos_rule);
           mixThem.process(rule, params);	   	   
	   //mixThem = new MixThem(file1, file2, System.out);
           //mixThem.process(rule, params);
	   return checkFileEquals(expected, baos_rule.toByteArray());
   }

	private static boolean checkFileEquals(File fileExpected, byte[] result) throws FileNotFoundException, IOException {
	   FileInputStream fisExpected = new FileInputStream(fileExpected);
	   int c;
	   int offset = 0;
	   while ((c = fisExpected.read()) != -1) {
		   if (offset >= result.length) return false;
		   int d = result[offset++];
		   if (c != d) return false;
	   }
	   if (offset > result.length) return false;
	   return true;
	}

}

@@ -905,12 +905,12 @@
 vel.
+F
 IN
-FO
+E
 );%0A 
@@ -1272,35 +1272,29 @@
 %09   
-System.out.println
+LOGGER.info
 (%22TEST 
-n%C2%B0
+N%C2%B0 
 %22 + 
@@ -1501,36 +1501,33 @@
 %09   
-System.out.println(%22
+LOGGER.fine(%22--%3E 
 URL 1 (%22
 +pre
@@ -1522,17 +1522,19 @@
 URL 1 (%22
-+
+ + 
 prefix +
@@ -1552,18 +1552,43 @@
 %22+%22)
-=%22+url1+%22;
+ : %22 + url1);%0A%09%09   LOGGER.fine(%22--%3E
  URL
@@ -1592,17 +1592,19 @@
 URL 2 (%22
-+
+ + 
 prefix +
@@ -1622,11 +1622,15 @@
 %22+%22)
-=%22+
+ : %22 + 
 url2
@@ -1846,76 +1846,126 @@
 %09   
-System.out.println(rule+%22 implemented = %22+rule.isImplemented()+%22 ; r
+LOGGER.info(%22  RULE %22 + rule + %22 (%22 + rule.isImplemented() ? %22%22 : %22NOT %22 + %22IMPLEMENTED)%22);%0A%09%09%09   LOGGER.fine(%22  --%3E R
 esou
@@ -1974,17 +1974,19 @@
 e (%22
-+
+ + 
 resource
 +%22) 
@@ -1985,16 +1985,20 @@
 urce
-+%22) = %22+
+ + %22) : %22 + 
 url)
@@ -2161,24 +2161,30 @@
 t(testId)) %7B
+%09%09%09%09%09%09
 %0A%09%09%09%09%09%09boole
@@ -2309,58 +2309,71 @@
 %09%09%09%09
-System.out.println(%22RULE 
+LOGGER.info(%22    RUN %22 + res ? %22
 PASS
+%22
  : %22
-+res
+FAIL%22
  + %22 
-/
+WITH
  PARAMS
+ 
 %22 + 

change logger messages