package android.security.extractor.logic;

import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
//import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
//import org.eclipse.core.runtime.Path;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.gmt.modisco.infra.discoverymanager.ui.launch.parametersdisplay.ParametersTableContentProvider;
import org.eclipse.gmt.modisco.java.actions.DefaultDiscoverer;
import org.eclipse.gmt.modisco.java.actions.DiscoverJavaModelFromJavaProject;
import org.eclipse.gmt.modisco.xml.discoverer.actions.XMLModelDiscoverer;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.m2m.atl.core.ATLCoreException;



public class InitialExtractor {
	
	LinkedList<IJavaProject> projectList = new LinkedList<IJavaProject>();
	LinkedList<IFile> manifestList= new LinkedList<IFile>();
	
	private Integer numberOfApps;

	public InitialExtractor(LinkedList<IJavaProject> projectList, LinkedList<IFile> manifestList,Integer numberOfApps) {
		
		this.projectList=projectList;
		this.manifestList=manifestList;
		this.numberOfApps=numberOfApps;
		
		System.out.print(" Number of list apps----------------->>>>>>>>>>>   "+ this.projectList.size()+ "          " + "\n" );
		System.out.print(" Number of apps----------------->>>>>>>>>>>   "+ this.numberOfApps+ "          " + "\n");	
	}
	
	public String getFullPath(String path) {
		String output = "";
		if (path.startsWith("platform:/resource/")) {
			String workspacePath = ResourcesPlugin.getWorkspace().getRoot().getLocation().toFile().getPath();
			String filePath = path.replaceAll("platform:/resource/", "");
			output = workspacePath + "\\" + filePath.replaceAll("/", "\\\\");
		}
		else if (path.startsWith("file:/")) {
			output = path.replaceAll("file:/", "");
		}
		
		return output;
	}
	
	public void execute() throws IOException, ATLCoreException, CoreException {
		//create model representations of the Java code and XML descriptors
		System.out.print(" running for each app for create initial models  "+ "          "+"\n");
		for(int i = 0; i < numberOfApps; i++) 
		{
			long startTimeForCreateInitialModelsForCurrentApp = System.currentTimeMillis();//for create initial models for each app
			this.generateModels(projectList.get(i));
		//	System.out.print(" yesssss_here   "+ "          "+"\n");
			this.cleanProject();
			long endTimeForCreateInitialModelsForCurrentApp = System.currentTimeMillis();//for end of initial model extractor part
			long totalTimeInitialModelExt = endTimeForCreateInitialModelsForCurrentApp - startTimeForCreateInitialModelsForCurrentApp;
			System.out.println("--------------TimeForCreateInitialModelsFor---------------->>>:"+"   "+ projectList.get(i).getElementName()+ "==" + "  "+ totalTimeInitialModelExt + "\n");
		}
		System.out.print(" end running for each app  "+ "          " + "\n");
		//TODO: merge PIMS
	}
	
	private void cleanProject() throws CoreException {
	}
	
	
	private void generateModels(IJavaProject iJavaProject) throws IOException {
		this.generateModelCode(iJavaProject);
		
	    System.out.print("  method for java model *********** " + "\n");
		
	}
	
	@SuppressWarnings("unchecked")
	public void generateModelCode(IJavaProject javaProject) throws IOException {
		//IJavaProject javaProject = JavaCore.create(project);
		 System.out.print("Create a discoverer for a Java project" + "\n");
		//Create a discoverer for a Java project
		DiscoverJavaModelFromJavaProject javaDiscoverer = new DiscoverJavaModelFromJavaProject();

		//Parameters of the discoverer
		@SuppressWarnings("rawtypes")
		Map javaDiscoveryParameters = new HashMap();
		javaDiscoveryParameters.put(DefaultDiscoverer.PARAMETER_SILENT_MODE, true);
		javaDiscoveryParameters.put(DefaultDiscoverer.PARAMETER_BROWSE_RESULT, false);

		//Execute the discoverer (javaProject is a IJavaProject) and serialize the model to <project_name>.javaxmi
		javaDiscoverer.discoverElement(javaProject, javaDiscoveryParameters);
		return;
	}
	
	@SuppressWarnings("unchecked")
	public void generateModelXML(IFile source) {
        System.out.print(" XML discovery:" + "\n");
        
        XMLModelDiscoverer discoverer = new XMLModelDiscoverer();
     // launch the discovery
        Map xmlDiscoveryParameters = new HashMap();
		xmlDiscoveryParameters.put(XMLModelDiscoverer.PARAMETER_SILENT_MODE, true);
		xmlDiscoveryParameters.put(XMLModelDiscoverer.PARAMETER_SERIALIZE_XMI, true);
     // get the resulting resource containing the XML model
        Resource xmlResource = discoverer.getResourceResult();
        System.out.print("    xmlResource: " + xmlResource + "\n");
		System.out.print("   XML model:" + xmlDiscoveryParameters + "\n");
		System.out.print(" end  XML discovery:" + "\n");
	}	
}
