/*
 * Copyright 2012 Nominum, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.nominum.build;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;

import play.twirl.compiler.TemplateCompilationError;
import scala.collection.JavaConversions;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Compiles scala.html files to scala source files and compiles routes.
 *
 * @requiresDependencyResolution compile
 */
@Mojo(name="compile-templates",defaultPhase=LifecyclePhase.GENERATE_SOURCES)
public class TemplateCompilerMojo extends AbstractMojo {

    @Parameter(defaultValue="${project}",required=true,readonly=true)
    private MavenProject project;

    /**
     * Location of the compiled templates.
     */
    @Parameter(defaultValue="${project.build.directory}/generated-sources/play-templates",required=true)
    private File generatedSourcesDirectory;

    /**
     * Location of the source files.
     */
    @Parameter(defaultValue="${project.build.sourceDirectory}",required=true)
    private File sourceDirectory;

    /**
     * Location of the play conf directory.
     */
    @Parameter(defaultValue="${project.basedir}/conf",required=true)
    private File confDirectory;

    @Parameter(defaultValue="true", required=true)
    private Boolean generateReverseRouter;

    public void execute()
        throws MojoExecutionException {
        try {
            compileTemplatesAndRoutes(absolutePath(confDirectory),
                absolutePath(generatedSourcesDirectory), project, absolutePath(sourceDirectory),
                generateReverseRouter);
        } catch (TemplateCompilationError e) {
            String msg = String.format("Error in template %s:%s %s", e.source().getPath(), e.line(), e.message());
            throw new MojoExecutionException(msg);
        }
    }

    /** This static method is usable by other Mojos */
    public static void compileTemplatesAndRoutes(File confDirectory, File outputDir, MavenProject project, File sourceDir, boolean generateReverseRouter) throws MojoExecutionException {
        project.addCompileSourceRoot(outputDir.getAbsolutePath());

        if (!outputDir.exists()) {
            boolean created = outputDir.mkdirs();
            if (!created) throw new MojoExecutionException("Failed to create output directory");
        }

        PlayRoutesCompiler routesCompiler = new PlayRoutesCompiler();
        routesCompiler.compile(confDirectory, outputDir,
                new scala.collection.mutable.ArrayBuffer<String>(), generateReverseRouter);

        List<File> classpathFiles = new ArrayList<File>();
        String classpath = System.getProperty("java.class.path");
        for (String path : classpath.split(":")) {
            classpathFiles.add(new File(path));
        }

        TemplateCompiler templateCompiler =
                new TemplateCompiler(JavaConversions.asScalaBuffer(classpathFiles).toList(), true);
        templateCompiler.compile(sourceDir, outputDir);

    }

    /** 
     * Convert Files with relative paths to be relative from the project basedir. 
     */
    private File absolutePath(File file) {
        if (file.isAbsolute()) {
            return file;
        }
        return new File(project.getBasedir(), file.getPath());
    }
}

@@ -2095,16 +2095,98 @@
 outer;%0A%0A
+    @Parameter(defaultValue=%22true%22, required=false)%0A    private Boolean forJava;%0A%0A
     publ
@@ -2203,16 +2203,16 @@
 ecute()%0A
-
         
@@ -2458,16 +2458,45 @@
 seRouter
+,%0A                    forJava
 );%0A     
@@ -2847,92 +2847,354 @@
 ory,
- File outputDir, MavenProject project, File sourceDir, boolean generateReverseRouter
+%0A                                                 File outputDir,%0A                                                 MavenProject project,%0A                                                 File sourceDir,%0A                                                 boolean generateReverseRouter,%0A                                                 boolean forJava
 ) th
@@ -4076,20 +4076,23 @@
 List(), 
-true
+forJava
 );%0A     

Add forJava parameter