package org.jerrymouse.gaes.mavenPlugin.base;

import java.io.File;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.project.MavenProject;
import org.apache.maven.settings.Settings;
import org.apache.tools.ant.Task;

/**
 * Base MOJO class for working with the Google App Engine SDK.
 * 
 * @author rhansen@kindleit.net
 */
public abstract class InfoBase extends AbstractMojo {

	public static final String PLUGIN_VERSION = "0.9";
	public static final String GAE = "gae";
	public static final String NORMAL = "normal";
	public static final String HADOOP = "hadoop";

	protected static final String[] ARG_TYPE = new String[0];

	/**
	 * The Maven project reference.
	 * 
	 * @parameter expression="${project}"
	 * @required
	 * @readonly
	 */
	protected MavenProject project;

	/**
	 * The Maven settings reference.
	 * 
	 * @parameter expression="${settings}"
	 * @required
	 * @readonly
	 */
	protected Settings settings;

	/**
	 * Overrides where the Project War Directory is located.
	 * 
	 * @parameter expression="war"
	 * @required
	 */
	protected String appDir;

	/**
	 * Specifies where the Google App Engine SDK is located.
	 * 
	 * @parameter expression="${appengine.sdk.root}"
	 * @required
	 */
	protected String sdkDir;

	/**
	 * @parameter expression="${gae}"
	 */
	protected String gae;

	/**
	 * @parameter expression="${normal}"
	 */
	protected String normal;

	/**
	 * @parameter expression="${hadoop}"
	 */
	protected String hadoop;

	/**
	 * Split large jar files (> 10M) into smaller fragments.
	 * 
	 * @parameter expression="${gae.deps.split}" default-value="false"
	 */
	protected boolean splitJars;

	protected final List<String> getCommonArgs() {
		final List<String> args = new ArrayList<String>(8);
		return args;
	}

	public String getSdkDir() {
		return sdkDir;
	}

	public void setSdkDir(String sdkDir) {
		this.sdkDir = sdkDir;
	}

	protected String getLibPath() {
		return sdkDir + "/lib/appengine-tools-api.jar";
	}

	protected String getBottomLayer() {
		if (gae != null) {
			return GAE;
		} else if (normal != null) {
			return NORMAL;
		} else if (hadoop != null) {
			return HADOOP;
		} else {
			return new ConfigFile(project, null).getBottomLayerFile();
		}
	}

	protected String getGaeEmail() {
		return new ConfigFile(project, null).getGaeEmail();
	}

	protected String getGaePassword() {
		return new ConfigFile(project, null).getGaePassword();
	}

	@Override
	public void execute() throws MojoExecutionException, MojoFailureException {
	}

	protected Properties getProperties() {
		return new ConfigFile(project, null).getProperties();
	}

}
