package tools.ic;

import java.util.*;
import java.lang.reflect.*;
import java.net.URLClassLoader;
import java.net.URL;
import java.net.MalformedURLException;
import java.io.*;

public class InterfaceComparer {
	private static boolean error = false;
	private static void compareClasses(Class<?> cleanroomClass, Class<?> studentClass) {
		boolean equals = false;
		ArrayList<Method> studentMethods = new ArrayList<Method>(Arrays.asList(studentClass.getDeclaredMethods()));
		// checkMethods
		for(Method cleanroomMethod : cleanroomClass.getDeclaredMethods()){
			System.out.println("[cleanroom]" + cleanroomMethod);
			for(Method studentMethod : studentMethods) {
				System.out.println("[student]" + studentMethod);
				if(cleanroomMethod.toString().equals(studentMethod.toString())) {
					equals = true;
					studentMethods.remove(studentMethod);
					break;
				}
			}	

			if(equals){
				equals = false;
			}else{
				error = true;
				System.err.println("WARNING - Method " +cleanroomMethod + " does not exists in student code or does not match with student counterpart");
			}
		}		
	}

	private static String getSimpleFileName(String path){
		int idx = path.lastIndexOf("/");
		return idx >= 0 ? path.substring(idx + 1) : path;
	}


	public static void main(String args[]){
		String pathToCleanroom = "/cleanroom/";
		URL[] cleanroomSearchUrls = null;
		String cleanroomUrl = "file://" + System.getProperty("user.dir") + pathToCleanroom;
		System.out.println("[url] " + cleanroomUrl);
		try {
			cleanroomSearchUrls = new URL[]{ new URL(cleanroomUrl) };
		
		} catch (MalformedURLException mue) {
			throw new Error("WARNING - " + mue.getMessage());
		}

		File f = new File("./"+pathToCleanroom);
		ClassLoader cl = ClassLoader.getSystemClassLoader();
		ClassLoader ul = new URLClassLoader(cleanroomSearchUrls);

		for(File path : f.listFiles()) {
			if (path.isFile()) {
				String pathString = path.toString();
				if(pathString.endsWith(".class")){
					// get simple Name
					String fileName = getSimpleFileName(pathString);
					// strip fileextension
					fileName = fileName.substring(0, fileName.lastIndexOf('.'));	
					Class<?> cleanroomClass = null;
					Class<?> studentClass = null;

					try{
						cleanroomClass = ul.loadClass(fileName);
					
						// compile student source
						// This is dirty
				//		Runtime rt = Runtime.getRuntime();
				//		String cmd = "javac " + fileName + ".java";
				//		System.out.println("[cmd] " + cmd);
				//		Process pr = rt.exec(cmd);
				//		pr.waitFor();

						studentClass = cl.loadClass(fileName);

					} catch (ClassNotFoundException cnfe) {
						throw new Error("WARNING - class [" + cnfe.getMessage()+"] not found");
					} catch (IOException ioe) {
						throw new Error("WARNING - Error while compiling student source ");
					} catch (InterruptedException ie) {
						throw new Error("WARNING - Error while compiling student source ");
					}
					compareClasses(cleanroomClass,studentClass);
				}
			}
	
		}

		if(error){
			throw new Error();
		}
	}
}

@@ -1757,68 +1757,8 @@
 r();
-%0A%09%09ClassLoader ul = new URLClassLoader(cleanroomSearchUrls);
 %0A%0A%09%09
@@ -2156,33 +2156,31 @@
 %7B%0A%09%09%09%09%09%09
-cleanroom
+student
 Class = 
 ul.loadC
@@ -2171,17 +2171,17 @@
 Class = 
-u
+c
 l.loadCl
@@ -2218,23 +2218,25 @@
 compile 
-student
+cleanroom
  source%0A
@@ -2243,33 +2243,8 @@
 %09%09%09%09
-%09%09// This is dirty%0A%09%09%09%09//
 %09%09Ru
@@ -2280,18 +2280,16 @@
 ();%0A%09%09%09%09
-//
 %09%09String
@@ -2330,18 +2330,16 @@
 a%22;%0A%09%09%09%09
-//
 %09%09System
@@ -2372,18 +2372,16 @@
 d);%0A%09%09%09%09
-//
 %09%09Proces
@@ -2405,18 +2405,16 @@
 d);%0A%09%09%09%09
-//
 %09%09pr.wai
@@ -2424,31 +2424,33 @@
 r();%0A%0A%09%09%09%09%09%09
-student
+cleanroom
 Class = cl.l

load student class first, #12