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.Level;

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 {
	
   @Test
   public final void parameter() throws MixException, FileNotFoundException, IOException {
	   MixThem.setLogging(Level.FINE);
	   int testId = 1;
	   boolean result = true;
	   RuleRuns ruleRuns = new RuleRuns();
	   while (true) {
		   MixThem.LOGGER.info("TEST N° " + testId);
		   String prefix = "test" + String.format("%03d", testId) +"_";
		   URL url1 = getClass().getResource(prefix + "file1.txt");
		   URL url2 = getClass().getResource(prefix + "file2.txt");
		   MixThem.LOGGER.fine("--> URL 1 (" + prefix + "file1.txt"+") : " + url1);
		   MixThem.LOGGER.fine("--> URL 2 (" + prefix + "file2.txt"+") : " + url2);
		   if( url1 == null || url2 == null) break;
		   for(Rule rule : Rule.values()) {
			   MixThem.LOGGER.info("RULE " + rule + " (" + (rule.isImplemented() ? "" : "NOT ") + "IMPLEMENTED)");
			   if (rule.isImplemented()) {
				List<RuleRun> runs = ruleRuns.getRuns(rule);
				for (RuleRun run : runs) {
					if (run.accept(testId)) {
						String resource = prefix + "output-" + rule.getExtension();
						if (run.hasSuffix()) {
							resource += "-" + run.getSuffix();
						}
						resource += ".txt";
						URL url = getClass().getResource(resource);
			   			MixThem.LOGGER.fine("--> Resource (" + resource + ") : " + url);
						if (url != null) {							
							boolean res = check(new File(url1.getFile()), new File(url2.getFile()), new File(url.getFile()), rule, run.getParams());
							MixThem.LOGGER.info("RUN " + (res ? "PASS" : "FAIL") + " WITH 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;
	}

}

@@ -1318,16 +1318,242 @@
 ) %7B%0A%09%09%09%09
+   String params = prefix + %22params-%22 + rule.getExtension();%0A%09%09%09%09   URL urlP = getClass().getResource(resource);%0A%09%09%09%09   if (urlP != null) %7B%0A%09%09%09%09%09   MixThem.LOGGER.fine(%22--%3E Params (%22 + params + %22) : %22 + urlP);%0A%09%09%09%09   %7D%0A%09%09%09%09   
 List%3CRul
@@ -1586,19 +1586,22 @@
 uns(
-rule
+urlP
 );%0A%09%09%09%09
+   
 for 
@@ -1632,40 +1632,11 @@
 %09%09%09%09
-if (run.accept(testId)) %7B%0A%09%09%09%09%09%09
+   
 Stri
@@ -1692,25 +1692,27 @@
 ion();%0A%09%09%09%09%09
-%09
+   
 if (run.hasS
@@ -1724,25 +1724,27 @@
 ()) %7B%0A%09%09%09%09%09%09
-%09
+   
 resource += 
@@ -1775,17 +1775,21 @@
 %09%09%09%09
-%09
+   
 %7D%0A%09%09%09%09%09
-%09
+   
 reso
@@ -1809,17 +1809,19 @@
 %22;%0A%09%09%09%09%09
-%09
+   
 URL url 
@@ -1863,14 +1863,13 @@
 %0A%09%09%09
+%09%09
    
-%09%09%09
 MixT
@@ -1934,17 +1934,19 @@
 );%0A%09%09%09%09%09
-%09
+   
 if (url 
@@ -1959,23 +1959,18 @@
 l) %7B
-%09%09%09%09%09%09%09
 %0A%09%09%09%09%09%09
-%09
+   
 bool
@@ -2092,17 +2092,19 @@
 ;%0A%09%09%09%09%09%09
-%09
+   
 MixThem.
@@ -2203,17 +2203,19 @@
 ;%0A%09%09%09%09%09%09
-%09
+   
 result &
@@ -2224,31 +2224,26 @@
 res;
-   
 %0A%09%09%09%09%09
-%09%7D%0A%09%09%09%09%09
+   
 %7D%0A%09%09%09%09
+   
 %7D%0A%09%09
@@ -2254,19 +2254,16 @@
 %7D%0A%09%09   %7D
-   
 %0A%09%09   te

implement rule runs from file