/*
 * Copyright (c) 2013 Red Hat, Inc. and/or its affiliates.
 *  
 *  All rights reserved. This program and the accompanying materials
 *  are made available under the terms of the Eclipse Public License v1.0
 *  which accompanies this distribution, and is available at
 *  http://www.eclipse.org/legal/epl-v10.html
 *  
 *  Contributors:
 *      Brad Davis - bradsdavis@gmail.com - Initial API and implementation
*/
package org.jboss.windup.interrogator.impl;

import java.io.File;
import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.io.FileUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTParser;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.ImportDeclaration;
import org.eclipse.jdt.core.dom.TypeDeclaration;
import org.jboss.windup.metadata.type.FileMetadata;
import org.jboss.windup.metadata.type.JavaMetadata;
import org.jboss.windup.metadata.type.ZipEntryMetadata;
import org.jboss.windup.util.BlacklistPackageResolver;
import org.jboss.windup.util.CustomerPackageResolver;


/**
 * Runs through all of the archives, comparing against the blacklist. If the class matches, looks for the source.
 * If the source does not exist, this interrogator will decompile the Class file into a Java file
 * Creates a JavaMetadata object, and passes it down the decorator pipeline.
 * 
 * @author bdavis
 * 
 */
public class JavaInterrogator extends ExtensionInterrogator<JavaMetadata> {
	private static final Log LOG = LogFactory.getLog(JavaInterrogator.class);

	private BlacklistPackageResolver blacklistPackageResolver;
	private CustomerPackageResolver customerPackageResolver;

	public void setBlacklistPackageResolver(
			BlacklistPackageResolver blacklistPackageResolver) {
		this.blacklistPackageResolver = blacklistPackageResolver;
	}
	
	public void setCustomerPackageResolver(CustomerPackageResolver customerPackageResolver) {
		this.customerPackageResolver = customerPackageResolver;
	}

	@Override
	public JavaMetadata archiveEntryToMeta(ZipEntryMetadata archiveEntry) {
		File file = archiveEntry.getFilePointer();
		
		JavaMetadata meta = new JavaMetadata();
		meta.setArchiveMeta(archiveEntry.getArchiveMeta());
		meta.setFilePointer(file);
		populateMeta(meta);

		if(LOG.isDebugEnabled()) {
			LOG.debug("Processing: " + file.getAbsolutePath());
			LOG.debug("Processing: " + meta.getQualifiedClassName());
			
		}
		
		if (!customerPackageResolver.isCustomerPkg(meta.getQualifiedClassName())) {
			LOG.trace("Not customer type: "+meta.getQualifiedClassName());
			return null;
		}
		if (blacklistPackageResolver.containsGenerated(meta.getClassDependencies())) {
			LOG.trace("Class is generated.  Skip profiling.");
			return null;
		}

		// third does the class contain any blacklists?
		if (!blacklistPackageResolver.containsBlacklist(meta.getClassDependencies())) {
			LOG.trace("Class does not contain blacklists.");
			return null;
		}
		
		return meta;
	}

	@Override
	public JavaMetadata fileEntryToMeta(FileMetadata entry) {
		JavaMetadata meta = new JavaMetadata();
		meta.setFilePointer(entry.getFilePointer());
		meta.setArchiveMeta(entry.getArchiveMeta());
		
		populateMeta(meta);
		
		if (!customerPackageResolver.isCustomerPkg(meta.getQualifiedClassName())) {
			LOG.trace("Not customer package.");
			return null;
		}
		if (blacklistPackageResolver.containsGenerated(meta.getClassDependencies())) {
			LOG.trace("Class is generated.  Skip profiling.");
			return null;
		}

		// third does the class contain any blacklists?
		if (!blacklistPackageResolver.containsBlacklist(meta.getClassDependencies())) {
			LOG.trace("Class does not contain blacklists.");
			return null;
		}
		
		return meta;
	}
	
	public void populateMeta(JavaMetadata meta) {
		ASTParser parser = ASTParser.newParser(AST.JLS3);
		parser.setBindingsRecovery(true);
		parser.setResolveBindings(true);
		try {
			parser.setSource(FileUtils.readFileToString(meta.getFilePointer()).toCharArray());
		}
		catch (IOException e) {
			LOG.error("Exception setting source for parser.", e);
			return;
		}
		parser.setKind(ASTParser.K_COMPILATION_UNIT);
		final CompilationUnit cu = (CompilationUnit) parser.createAST(null);
		
		@SuppressWarnings("unchecked")
		List<ImportDeclaration> imports = cu.imports();
		Set<String> clzDependencies = new HashSet<String>();
		
		for(ImportDeclaration id : imports) {
			if(LOG.isDebugEnabled()) {
				LOG.debug("Import: "+id.getName());
			}
			clzDependencies.add(id.getName().getFullyQualifiedName());
		}
		meta.setClassDependencies(clzDependencies);
		meta.setBlackListedDependencies(blacklistPackageResolver.extractBlacklist(clzDependencies));
		
		if(LOG.isDebugEnabled()) {
			for(String id : meta.getBlackListedDependencies()) {
				LOG.debug("Blacklist: "+id);
			}
		}
		
		@SuppressWarnings("unchecked")
		List<TypeDeclaration> types = cu.types();
		for(TypeDeclaration type : types) {
			String fullPackage = cu.getPackage().getName().getFullyQualifiedName();
			fullPackage = fullPackage +"."+type.getName().getFullyQualifiedName();
			meta.setQualifiedClassName(fullPackage);
			
			if(LOG.isDebugEnabled()) {
				LOG.debug("Full Package: "+fullPackage);
			}
			break;
		}
	}
	

}

@@ -874,32 +874,84 @@
 ortDeclaration;%0A
+import org.eclipse.jdt.core.dom.PackageDeclaration;%0A
 import org.eclip
@@ -5106,26 +5106,182 @@
 %0A%09%09%09
-String fullP
+//if class is in package grab that%0A%09%09%09PackageDeclaration packageDeclaration = cu.getPackage();%0A%09%09%09String packageName = %22%22;%0A%09%09%09if(packageDeclaration != null) %7B%0A%09%09%09%09p
 ackage
+Name
  = c
@@ -5328,21 +5328,73 @@
 edName()
-;%0A%09%09%09
+ + %22.%22;%0A%09%09%09%7D%0A%09%09%09%0A%09%09%09//generate full class name%0A%09%09%09String 
 fullPack
@@ -5403,25 +5403,22 @@
 e = 
-fullP
+p
 ackage
- +%22.%22+
+Name + 
 type

fix NPE when getting package for class not in a package. Resolves issue #39.