package interactivefeaturelocator.test;

import java.io.File;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IMethod;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jface.viewers.IDecoration;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.ide.IDE;
import org.eclipse.ui.part.FileEditorInput;
import org.ntlab.trace.MethodExecution;

public class Test {
	private IJavaProject javaProject;
	private IWorkspaceRoot root;
	
	public Test() throws JavaModelException {
		root = ResourcesPlugin.getWorkspace().getRoot();
		IProject project = root.getProject("jedit");
		javaProject = JavaCore.create(project);
		IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
		page.getActiveEditor();
//		System.out.println(javaProject.getChildren()[0].getElementName());
//		IFile file = new IFile("C:\\Users\\student\\Desktop\\fujioka\\workspace_FeatureLocation\\InteractiveFeatureLocator\\src\\interactivefeaturelocator\\test\\Test.java");
//		page.openEditor(input, editorId);
//		IType type = javaProject.findType("org.gjt.sp.jedit.gui.LogViewer$MyFilteredListModel");
//		System.out.println(type.getDeclaringType().getElementName());
//		if (type.isClass()) {
//			System.out.println("Class!");
//			IMethod[] methods = type.getMethods();
//			for (IMethod method : methods) {
//				System.out.println(method.getElementName());
////				method.get
//			}
//		} else {
//			System.out.println("Not Class");
//		}
	}
	
	public IMethod m(String methodSigniture) throws JavaModelException {
		String[] fragments = methodSigniture.split("\\s", 3);
		String signature = fragments[fragments.length-1];
		int n = signature.split("\\(")[0].lastIndexOf(".");
		String classSignature = signature.substring(0, n);
		String methodData = signature.substring(n+1);
		String[] fragments2 = methodData.split("\\(");
		String methodName = fragments2[0];
//		int m = fragments2[1].indexOf("\\)");
		String s = fragments2[1].substring(0, fragments2[1].length()-1);
		String[] parameters = s.split(",");
//		String[] parameters = fragments2[1].substring(0, fragments2[1].indexOf("\\)")).split(",");
		IType type = javaProject.findType(classSignature);
//		System.out.println(type.getDeclaringType().getElementName());
//		if (type.isClass()) {
//			System.out.println("Class!");
//			IMethod[] methods = type.getMethods();
//			for (IMethod method : methods) {
//				System.out.println(method.getElementName());
////				method.get
//			}
//		} else {
//			System.out.println("Not Class");
//		}
//		System.out.println("type: " + type.getElementName());
		if (type.getMethods().length != 0) {
			IMethod method = type.getMethod(methodName, parameters);
			return method;
		}
		return null;
	}
	
	public void openEditor(MethodExecution me) {
		String signature = me.getSignature();
		System.out.println("Signature: " + signature);
		IPath path = new Path("/jedit/jEdit/org/gjt/sp/jedit/GUIUtilities.java");
		IFile file = root.getFile(path);
		IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
		try {
			IDE.openEditor(page, file);
			System.out.println("open: " + file.getName());
		} catch (PartInitException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
