package android.security.extractor.actions;



import java.util.LinkedList;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.window.Window;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IWorkbenchWindowActionDelegate;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaCore;

import android.security.extractor.logic.InitialExtractor;
import android.security.extractor.logic.AndroidModelExtractor;
import android.security.extractor.ui.AndroidSecurityExtractionWizard;
import android.security.extractor.ui.AndroidSecurityExtractorWizardPage;


public class ExtractorAction implements IWorkbenchWindowActionDelegate {
	private IWorkbenchWindow window;
	private String  javaProjects;
	IProject [] projects;
	IProject [] realProjects;
	LinkedList<IJavaProject> projectList = new LinkedList<IJavaProject>();
	LinkedList<IJavaProject> projectListAfterExtInitialModels = new LinkedList<IJavaProject>();
	LinkedList<IJavaProject> projectListAfterExtAndroidModels = new LinkedList<IJavaProject>();
	LinkedList<IFile> manifestList= new LinkedList<IFile>();
	LinkedList<IFile> javaModelList= new LinkedList<IFile>();
	LinkedList<IFile> androidModels= new LinkedList<IFile>();
	IFile emptyModel;
	/**
	 * The constructor.
	 */
	public ExtractorAction() {
	}

	
	public void run(IAction action) {
		
		try {
			
			projectList.equals(null);
		
			AndroidSecurityExtractionWizard wizard = new AndroidSecurityExtractionWizard();
			WizardDialog dialog = new WizardDialog(window.getShell(), wizard);
			
			if (dialog.open() == Window.OK) {
				
				AndroidSecurityExtractorWizardPage page = (AndroidSecurityExtractorWizardPage)wizard.getPage("SelectProjectAndXMLDescriptors");
				
				IProject javaProject = page.getSelectedProject();
				
				IWorkspaceRoot workspaceRoot=javaProject.getProject().getWorkspace().getRoot(); 
				IProject[] projects = workspaceRoot.getProjects();
				//For create listOfProject and listOfManifestModels:
				for(int i = 0; i < projects.length; i++) 
				 {
				    IProject project = projects[i];
				    if(project.hasNature(JavaCore.NATURE_ID) && project.isOpen() && project.getName()!= "RemoteSystemsTempFiles") 
				   {
				    	if (project.getFile(project.getName() + ".javaxmi").exists())
				    	{
				    		 continue;
				    	}
				       projectList.add(JavaCore.create(project));
				       manifestList.add(project.getFile("AndroidManifest.xml"));
				    }
				 }
				
				System.out.println("--------------pathOfWorkspace----------------:"+workspaceRoot+"\n");
              
				//for time:
				long startTime = System.currentTimeMillis();//for start running whole tool
				long startTimeInitialModelExt = System.currentTimeMillis();//for start of initial model extractor part
				
				System.out.println("--------------NumberOfAndroidProject----------------:"+ projectList.size() +"\n");
				
				System.out.println("--------------Start extract Initial Models----------------:"+"                   "+ "\n");
				InitialExtractor ext = new InitialExtractor(projectList, manifestList, projectList.size());
				ext.execute();
				System.out.println("--------------end extract Initial Models----------------:"+"                   "+"\n");
				
				long endTimeInitialModelExt = System.currentTimeMillis();//for end of initial model extractor part
				long totalTimeInitialModelExt = endTimeInitialModelExt - startTimeInitialModelExt;
				System.out.println("--------------totalTimeOfInitialModelExtractor----------------:"+totalTimeInitialModelExt+"\n");
				
				//for extract Android Security Models:
				System.out.println("--------------Start extract Android Security Models----------------:"+"                   "+"\n");
				for(int i = 0; i < projects.length; i++) 
				 {
				    IProject project = projects[i];
				    if(project.hasNature(JavaCore.NATURE_ID) && project.isOpen() && project.getName()!= "RemoteSystemsTempFiles") 
				   {
				    	if (project.getFile(project.getName().toString() + "_" + "AndroidApplicationSecurityAspects.xmi").exists())
				    	{System.out.println("yes this app has android model"+"                   ");
				    		 continue;
				    	}
				       projectListAfterExtInitialModels.add(JavaCore.create(project));
				       manifestList.add(project.getFile("AndroidManifest.xml"));
				       javaModelList.add(project.getFile(project.getName()+".javaxmi"));
				    }
				 }
				
				AndroidModelExtractor ex1=new AndroidModelExtractor(projectListAfterExtInitialModels, manifestList, javaModelList, projectList.size(), projects);
				 ex1.execute();
				 System.out.println("--------------end extract Android Security Models----------------:"+"                  "+"\n");
				 //end of extract Android Security Models and also ICC model
				 
			
				//end
				long endTime   = System.currentTimeMillis();
				long totalTime = endTime - startTime;
				System.out.println("--------------totalTime----------------:"+totalTime+"\n");
				
				
				this.javaProjects= "file:/"+ ResourcesPlugin.getWorkspace().getRoot().getLocation();
		        
				System.out.print(" end of running tool "+ this.javaProjects.toString()+"            "+"\n");
			
			}
		} 
	catch (Exception e) {
			e.printStackTrace();
		}
	}

	
	public void selectionChanged(IAction action, ISelection selection) {
	}


	public void dispose() {
	}

	public void init(IWorkbenchWindow window) {
		this.window = window;
	}
}