/*-
 * Copyright (c) 2012-2015 Red Hat, 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 org.fedoraproject.xmvn.mojo;

import java.io.IOException;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

import javax.inject.Inject;
import javax.inject.Named;

import org.apache.maven.model.Model;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.ResolutionScope;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.util.xml.Xpp3Dom;
import org.codehaus.plexus.util.xml.pull.MXSerializer;
import org.codehaus.plexus.util.xml.pull.XmlSerializer;

import org.fedoraproject.xmvn.artifact.Artifact;
import org.fedoraproject.xmvn.artifact.DefaultArtifact;
import org.fedoraproject.xmvn.model.ModelProcessor;

/**
 * @author Mikolaj Izdebski
 */
@Mojo( name = "builddep", aggregator = true, requiresDependencyResolution = ResolutionScope.NONE )
@Named
public class BuilddepMojo
    extends AbstractMojo
{
    static class NamespacedArtifact
    {
        String namespace;

        Artifact artifact;

        public NamespacedArtifact( String namespace, Artifact artifact )
        {
            this.namespace = namespace != null ? namespace : "";
            this.artifact = artifact;
        }

        @Override
        public int hashCode()
        {
            return artifact.hashCode() ^ namespace.hashCode();
        }

        @Override
        public boolean equals( Object rhs )
        {
            NamespacedArtifact other = (NamespacedArtifact) rhs;
            return namespace.equals( other.namespace ) && artifact.equals( other.artifact );
        }
    }

    @Parameter( defaultValue = "${reactorProjects}", readonly = true, required = true )
    private List<MavenProject> reactorProjects;

    private final ModelProcessor modelProcessor;

    // Injected through reflection by XMvn lifecycle participant
    private List<String[]> resolutions;

    @Inject
    public BuilddepMojo( ModelProcessor modelProcessor )
    {
        this.modelProcessor = modelProcessor;
    }

    private static void addOptionalChild( Xpp3Dom parent, String tag, String value, String defaultValue )
    {
        if ( defaultValue == null || !value.equals( defaultValue ) )
        {
            Xpp3Dom child = new Xpp3Dom( tag );
            child.setValue( value );
            parent.addChild( child );
        }
    }

    private static Xpp3Dom toXpp3Dom( NamespacedArtifact namespacedArtifact, String tag )
    {
        Artifact artifact = namespacedArtifact.artifact;
        Xpp3Dom parent = new Xpp3Dom( tag );

        addOptionalChild( parent, "namespace", namespacedArtifact.namespace, "" );
        addOptionalChild( parent, "groupId", artifact.getGroupId(), null );
        addOptionalChild( parent, "artifactId", artifact.getArtifactId(), null );
        addOptionalChild( parent, "extension", artifact.getExtension(), "jar" );
        addOptionalChild( parent, "classifier", artifact.getClassifier(), "" );
        addOptionalChild( parent, "version", artifact.getVersion(), "SYSTEM" );

        return parent;
    }

    private static void serialize( NamespacedArtifact artifact, XmlSerializer serializer, String namespace, String tag )
        throws IOException
    {
        Xpp3Dom dom = toXpp3Dom( artifact, tag );
        dom.writeToSerializer( namespace, serializer );
    }

    @Override
    public void execute()
        throws MojoExecutionException
    {
        if ( resolutions == null )
            return;

        Set<Artifact> artifacts = new LinkedHashSet<>();
        for ( MavenProject project : reactorProjects )
        {
            Model model = project.getModel();
            String modelId = model.getLocation( "" ).getSource().getModelId();
            BuildDependencyVisitor visitor = new BuildDependencyVisitor( modelId );
            modelProcessor.processModel( model, visitor );
            artifacts.addAll( visitor.getArtifacts() );
        }

        Set<NamespacedArtifact> deps = new LinkedHashSet<>();
        for ( String[] resolution : resolutions )
        {
            Artifact artifact = new DefaultArtifact( resolution[0] );
            String compatVersion = resolution[1];
            String namespace = resolution[2];

            if ( artifacts.contains( artifact ) )
            {
                deps.add( new NamespacedArtifact( namespace, artifact.setVersion( compatVersion ) ) );
            }
        }

        serializeArtifacts( deps );
    }

    private void serializeArtifacts( Set<NamespacedArtifact> artifacts )
        throws MojoExecutionException
    {
        try (Writer writer = Files.newBufferedWriter( Paths.get( ".xmvn-builddep" ), StandardCharsets.UTF_8 ))
        {
            XmlSerializer s = new MXSerializer();
            s.setProperty( "http://xmlpull.org/v1/doc/properties.html#serializer-indentation", "  " );
            s.setProperty( "http://xmlpull.org/v1/doc/properties.html#serializer-line-separator", "\n" );
            s.setOutput( writer );
            s.startDocument( "US-ASCII", null );
            s.comment( " Build dependencies generated by XMvn " );
            s.text( "\n" );
            s.startTag( null, "dependencies" );

            for ( NamespacedArtifact dependencyArtifact : artifacts )
                serialize( dependencyArtifact, s, null, "dependency" );

            s.endTag( null, "dependencies" );
        }
        catch ( IOException e )
        {
            throw new MojoExecutionException( "Unable to write builddep file", e );
        }
    }
}

@@ -1425,16 +1425,73 @@
 ializer;
+%0Aimport org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;
 %0A%0Aimport
@@ -2506,24 +2506,106 @@
    %7D%0A    %7D%0A%0A
+    private final Logger logger = LoggerFactory.getLogger( BuilddepMojo.class );%0A%0A
     @Paramet
@@ -4442,16 +4442,111 @@
  null )%0A
+        %7B%0A            logger.warn( %22Skipping buiddep: XMvn lifecycle participant is absent%22 );%0A
         
@@ -4556,16 +4556,26 @@
  return;
+%0A        %7D
 %0A%0A      

Print a warning when builddep MOJO is running with pure Maven