package org.quattor.maven;

import java.io.File;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import java.util.regex.Pattern;

import org.apache.maven.plugin.MojoExecutionException;
import org.quattor.pan.Compiler;
import org.quattor.pan.CompilerOptions;
import org.quattor.pan.CompilerResults;
import org.quattor.pan.exceptions.SyntaxException;
import org.quattor.pan.output.Formatter;
import org.quattor.pan.output.FormatterComparator;
import org.quattor.pan.output.FormatterUtils;

/**
 * @description perform a full build of pan language templates
 * @goal pan-build
 * @phase compile
 */
public class PanBuildMojo extends AbstractPanMojo {

	/**
	 * @description subdirectory with object templates
	 * @parameter expression="${panc.profiles}" default-value="profiles"
	 * @required
	 */
	private String profiles = "profiles";

	/**
	 * @description directory for generated profiles
	 * @parameter expression="${panc.outputDir}"
	 *            default-value="${basedir}/target"
	 * @required
	 */
	private File outputDir;

	/**
	 * @description maximum number of iterations
	 * @parameter expression="${panc.maxIteration}" default-value=10000
	 * @required
	 */
	private int maxIteration = 10000;

	/**
	 * @description maximum number of recursions
	 * @parameter expression="${panc.maxRecursion}" default-value=10
	 * @required
	 */
	private int maxRecursion = 10;

	/**
	 * @description list of formats for output files (comma-separated list)
	 * @parameter expression="${panc.formats}" default-value="pan,dep"
	 * @required
	 */
	private String formats = "pan,dep";

	private Set<Formatter> formatters;

	public void execute() throws MojoExecutionException {

		setFormatter();

		createoutputDir();

		CompilerOptions options = createCompilerOptions();

		File profileDirectory = new File(sourceDirectory, profiles);

		Set<File> objects = PluginUtils.collectPanSources(profileDirectory);

		CompilerResults results = Compiler.run(options, null, objects);

		boolean hadError = results.print(verbose);

		if (hadError) {
			throw new MojoExecutionException("pan language syntax check failed");
		}

	}

	private void setFormatters() throws MojoExecutionException {
		formatters = new TreeSet<Formatter>(FormatterComparator.getInstance());
		
		if (formatter == null) {
			throw new MojoExecutionException("unknown formatter: "
					+ formatterName);
		}
	}

	private void createoutputDir() throws MojoExecutionException {
		if (!outputDir.isDirectory()) {
			if (!outputDir.mkdirs()) {
				throw new MojoExecutionException("error creating " + outputDir);
			}
		}
	}

	private CompilerOptions createCompilerOptions()
			throws MojoExecutionException {

		List<Pattern> debugIncludePatterns = new LinkedList<Pattern>();
		List<Pattern> debugExcludePatterns = new LinkedList<Pattern>();
		int iterationLimit = 5000;
		int callDepthLimit = 50;
		File sessionDirectory = null;
		int nthread = 0;
		boolean forceBuild = false;
		File annotationDirectory = null;
		File annotationBaseDirectory = null;
		LinkedList<File> includeDirectories = new LinkedList<File>();
		includeDirectories.add(sourceDirectory);

		Set<Formatter> formatters = new TreeSet<Formatter>();
		formatters.add(formatter);

		try {
			return new CompilerOptions(debugIncludePatterns,
					debugExcludePatterns, iterationLimit, callDepthLimit,
					formatters, outputDir, sessionDirectory,
					includeDirectories, nthread,
					CompilerOptions.DeprecationWarnings.OFF, forceBuild,
					annotationDirectory, annotationBaseDirectory, null);

		} catch (SyntaxException e) {
			throw new MojoExecutionException(
					"error creating compiler options: " + e.getMessage());
		}
	}

}

@@ -1729,20 +1729,21 @@
 ormatter
+s
 ();%0A
-
 %0A%09%09creat
@@ -2305,117 +2305,63 @@
 %09%0A%09%09
-if (formatter == null) %7B%0A%09%09%09throw new MojoExecutionException(%22unknown formatter: %22%0A%09%09%09%09%09+ formatterName);%0A%09%09%7D
+// TODO: Add method to transform formats to formatters.
 %0A%09%7D%0A
@@ -3059,24 +3059,24 @@
 st%3CFile%3E();%0A
+
 %09%09includeDir
@@ -3111,94 +3111,8 @@
 );%0A%0A
-%09%09Set%3CFormatter%3E formatters = new TreeSet%3CFormatter%3E();%0A%09%09formatters.add(formatter);%0A%0A
 %09%09tr

fix compilation errors