package com.github.xpenatan.gdx.backends.dragome;

import java.io.File;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;

import com.dragome.commons.compiler.classpath.Classpath;
import com.dragome.services.ReflectionServiceImpl;
import com.dragome.services.ServiceLocator;
import com.dragome.web.helpers.serverside.StandaloneDragomeAppGenerator;

public class DragomeBuilder {

	public static void build(DragomeBuildConfigurator configurator) {
		DragomeGdxConfigurator gdxConfiguration = new DragomeGdxConfigurator(configurator);
//		URL[] urLs = ((URLClassLoader) (Thread.currentThread().getContextClassLoader())).getURLs();
//		ArrayList<URL> acceptedURL = new ArrayList<>();
//		ArrayList<URL> notAcceptedURL = new ArrayList<>();

		System.out.println("---------------Initializing---------------");

//		for (int i = 0; i < urLs.length; i++) {
//			URL url = urLs[i];
//			String path = url.getPath();
//			ACCEPT_STATE acceptState = acceptPath(path);
//			boolean accept = acceptState == ACCEPT_STATE.ACCEPT;
//			if(acceptState == ACCEPT_STATE.NO_MATCH)
//				accept = configuration.acceptClasspath(url);
//
//			if (accept)
//				acceptedURL.add(url);
//			else
//				notAcceptedURL.add(url);
//		}
		ServiceLocator serviceLocator = ServiceLocator.getInstance();
		serviceLocator.setReflectionService(new ReflectionServiceImpl());
		serviceLocator.setConfigurator(gdxConfiguration);

		String path = new File("webapp").getAbsolutePath();

//		File destinationDirectory = new File(configuration.getTargetDirectory());
//		File webappDirectory = new File(configuration.getTargetDirectory());
		File destinationDirectory = new File(path);
		File webappDirectory = new File(path);
		StandaloneDragomeAppGenerator gen = new StandaloneDragomeAppGenerator(destinationDirectory, webappDirectory, true, true, false);
		gen.execute(false);


//		final Classpath classPath= new Classpath();
//		for(int i = 0; i < acceptedURL.size(); i++) {
//			URL url = acceptedURL.get(i);
//			String file = url.getFile();
//			classPath.addEntry(file);
//		}


	}

	private static ACCEPT_STATE acceptPath(String path) {
		ACCEPT_STATE isValid = ACCEPT_STATE.NO_MATCH;

		if(path.contains("junit"))
			isValid = ACCEPT_STATE.NOT_ACCEPT;
		if(path.contains("hamcrest"))
			isValid = ACCEPT_STATE.NOT_ACCEPT;
		if(path.contains("javaagent-shaded"))
			isValid = ACCEPT_STATE.NOT_ACCEPT;
		if(path.contains("dragome-bytecode-js-compiler"))
			isValid = ACCEPT_STATE.NOT_ACCEPT;
		if(path.contains("gdx-jnigen"))
			isValid = ACCEPT_STATE.NOT_ACCEPT;
		if(path.contains("commons-io"))
			isValid = ACCEPT_STATE.NOT_ACCEPT;
		return isValid;
	}


	enum ACCEPT_STATE {
		ACCEPT, NOT_ACCEPT, NO_MATCH
	}
}

@@ -69,147 +69,8 @@
 le;%0A
-import java.net.URL;%0Aimport java.net.URLClassLoader;%0Aimport java.util.ArrayList;%0A%0Aimport com.dragome.commons.compiler.classpath.Classpath;%0A
 impo
@@ -422,682 +422,77 @@
 r);%0A
-//%09%09URL%5B%5D urLs = ((URLClassLoader) (Thread.currentThread().getContextClassLoader())).getURLs();%0A//%09%09ArrayList%3CURL%3E acceptedURL = new ArrayList%3C%3E();%0A//%09%09ArrayList%3CURL%3E notAcceptedURL = new ArrayList%3C%3E();%0A%0A%09%09System.out.println(%22---------------Initializing---------------%22);%0A%0A//%09%09for (int i = 0; i %3C urLs.length; i++) %7B%0A//%09%09%09URL url = urLs%5Bi%5D;%0A//%09%09%09String path = url.getPath();%0A//%09%09%09ACCEPT_STATE acceptState = acceptPath(path);%0A//%09%09%09boolean accept = acceptState == ACCEPT_STATE.ACCEPT;%0A//%09%09%09if(acceptState == ACCEPT_STATE.NO_MATCH)%0A//%09%09%09%09accept = configuration.acceptClasspath(url);%0A//%0A//%09%09%09if (accept)%0A//%09%09%09%09acceptedURL.add(url);%0A//%09%09%09else%0A//%09%09%09%09notAcceptedURL.add(url);%0A//%09%09%7D
+%0A%09%09System.out.println(%22---------------Initializing---------------%22);%0A
 %0A%09%09S
@@ -677,65 +677,8 @@
 );%0A%0A
-%09%09String path = new File(%22webapp%22).getAbsolutePath();%0A%0A//
 %09%09Fi
@@ -726,37 +726,29 @@
 urat
-ion.getTargetDirectory
+or.getWebAppPath
 ());%0A
-//
 %09%09Fi
@@ -791,119 +791,26 @@
 urat
-ion
+or
 .get
-TargetDirectory());%0A%09%09File destinationDirectory = new File(path);%0A%09%09File webappDirectory = new File(p
+WebAppP
 ath
+()
 );%0A%09
@@ -965,848 +965,8 @@
 e);%0A
-%0A%0A//%09%09final Classpath classPath= new Classpath();%0A//%09%09for(int i = 0; i %3C acceptedURL.size(); i++) %7B%0A//%09%09%09URL url = acceptedURL.get(i);%0A//%09%09%09String file = url.getFile();%0A//%09%09%09classPath.addEntry(file);%0A//%09%09%7D%0A%0A%0A%09%7D%0A%0A%09private static ACCEPT_STATE acceptPath(String path) %7B%0A%09%09ACCEPT_STATE isValid = ACCEPT_STATE.NO_MATCH;%0A%0A%09%09if(path.contains(%22junit%22))%0A%09%09%09isValid = ACCEPT_STATE.NOT_ACCEPT;%0A%09%09if(path.contains(%22hamcrest%22))%0A%09%09%09isValid = ACCEPT_STATE.NOT_ACCEPT;%0A%09%09if(path.contains(%22javaagent-shaded%22))%0A%09%09%09isValid = ACCEPT_STATE.NOT_ACCEPT;%0A%09%09if(path.contains(%22dragome-bytecode-js-compiler%22))%0A%09%09%09isValid = ACCEPT_STATE.NOT_ACCEPT;%0A%09%09if(path.contains(%22gdx-jnigen%22))%0A%09%09%09isValid = ACCEPT_STATE.NOT_ACCEPT;%0A%09%09if(path.contains(%22commons-io%22))%0A%09%09%09isValid = ACCEPT_STATE.NOT_ACCEPT;%0A%09%09return isValid;%0A%09%7D%0A%0A%0A%09enum ACCEPT_STATE %7B%0A%09%09ACCEPT, NOT_ACCEPT, NO_MATCH%0A
 %09%7D%0A%7D

Clean up test code