package org.jerrymouse.gaes.mavenPlugin.base;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;

public abstract class MojoBase extends InfoBase {
	/**
	 * Passes command to the Google App Engine AppCfg runner.
	 * 
	 * @param command
	 *            command to run through AppCfg
	 * @param commandArguments
	 *            arguments to the AppCfg command.
	 */
	protected final void runAppCfg(final String command,
			final String... commandArguments) {
		AppCfgTask appCfgTask = new AppCfgTask();
		runTask(appCfgTask, getArgStr(command, commandArguments));
	}

	protected final void runAppCfgByClass(final String command,
			final String... commandArguments) {
		AppCfgClass appCfgClass = new AppCfgClass(getLibPath());
		appCfgClass.runAppCfg(getArgList(command, commandArguments));
	}

	protected void runTask(BaseTask task, String args) {
		task.execute(getLibPath(), args);
	}

	protected void runMvnCommond(List<String> args) {
		args.add("-D" + getBottomLayer());
		new MvnCommond(args, project.getBasedir(), System.out).exec();
	}

	private List<String> getArgList(final String command,
			final String... commandArguments) {
		final List<String> args = getAppCfgArgs();
		args.add(command);
		for (String commandArgument : commandArguments) {
			args.add(commandArgument);
		}
		return args;
	}

	public String getArgStr(final String command,
			final String... commandArguments) {
		List<String> args = getArgList(command, commandArguments);
		getLog().debug("args:" + args);
		// AppCfg.main(args.toArray(ARG_TYPE));
		StringBuilder argsStrB = new StringBuilder();
		for (String string : args) {
			argsStrB.append(string + " ");
		}
		String argsStr = argsStrB.toString();
		if (argsStr.endsWith(" "))
			argsStr.substring(0, argsStr.length() - 1);
		return argsStr;
	}

	/**
	 * Generate all common Google AppEngine Task Parameters for use in all the
	 * goals.
	 * 
	 * @return List of arguments to add.
	 */
	protected final List<String> getAppCfgArgs() {
		final List<String> args = getCommonArgs();

		addBooleanOption(args, "--disable_prompt", !settings
				.getInteractiveMode());

		addBooleanOption(args, "--enable_jar_splitting", splitJars);
		addBooleanOption(args, "--retain_upload_dir", false);

		return args;
	}

	protected void runMvnCommond(String arg) {
		List<String> args = new ArrayList<String>();
		String[] strs = arg.split(" ");
		args.addAll(Arrays.asList(strs));
		runMvnCommond(args);
	}

	public void execute() throws MojoExecutionException, MojoFailureException {
		getLog().debug(getBottomLayer());
		if (getBottomLayer().equals(GAE)) {
			gaeExecute();
		} else if (getBottomLayer().equals(NORMAL)) {
			normalExecute();
		} else if (getBottomLayer().equals(HADOOP)) {
			hadoopExecute();
		}
	}

	private final void addBooleanOption(final List<String> args,
			final String key, final boolean var) {
		if (var) {
			args.add(key);
		}
	}

	protected abstract void hadoopExecute();

	protected abstract void normalExecute();

	protected abstract void gaeExecute();
}
