/*
 * Copyright (c) 2014 by Stefan Ferstl <st.ferstl@gmail.com>
 *
 * 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.github.ferstl.depgraph;

import org.apache.maven.artifact.resolver.filter.ArtifactFilter;
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.plugins.annotations.ResolutionScope;

import com.github.ferstl.depgraph.dot.DotBuilder;

/**
 * Creates a dependency graph of a maven module.
 */
@Mojo(
    name = "graph",
    aggregator = false,
    defaultPhase = LifecyclePhase.NONE,
    requiresDependencyCollection = ResolutionScope.TEST,
    requiresDirectInvocation = false,
    threadSafe = true)
public class DependencyGraphMojo extends AbstractGraphMojo {

  /**
   * If set to {@code true}, the created graph will show version information an all artifacts. Depending on the flags
   * {@link #showVersions} and {@link #showConflicts}, the version will either be shown directly in the artifact nodes
   * or on the dependency edges.
   *
   * @since 1.0.0
   */
  @Parameter(property = "showVersions", defaultValue = "false")
  boolean showVersions;

  /**
   * If set to {@code true}, the graph will additionally contain conflicting dependencies. Note that the dependency
   * graph may not be 100% accurate when this flag is enabled and the plugin is executed with a Maven version greater
   * or equal 3.0!
   *
   * @since 1.0.0
   */
  @Parameter(property = "showConflicts", defaultValue = "false")
  boolean showConflicts;

  /**
   * If set to {@code true}, the graph will additionally contain duplicate dependencies. Note that the dependency
   * graph may not be 100% accurate when this flag is enabled and the plugin is executed with a Maven version greater
   * or equal 3.0!
   *
   * @since 1.0.0
   */
  @Parameter(property = "showDuplicates", defaultValue = "false")
  boolean showDuplicates;

  @Override
  protected GraphFactory createGraphFactory(ArtifactFilter artifactFilter) {
    DotBuilder dotBuilder = createGraphBuilder();
    GraphBuilderAdapter adapter = createGraphBuilderAdapter();

    return new SimpleGraphFactory(adapter, artifactFilter, dotBuilder);
  }

  private DotBuilder createGraphBuilder() {
    DotBuilder dotBuilder = new DotBuilder()
      .useNodeRenderer(NodeRenderers.VERSIONLESS_ID)
      .useNodeLabelRenderer(NodeRenderers.ARTIFACT_ID_LABEL);

    if (requiresFullGraph()) {
      // For the full graph we display the versions on the edges
      dotBuilder.useEdgeRenderer(new DependencyEdgeRenderer(this.showVersions, this.showDuplicates, this.showConflicts));
    } else if (this.showVersions) {
      // On the effective dependency graph we can display the versions within the nodes
      dotBuilder.useNodeLabelRenderer(NodeRenderers.ARTIFACT_ID_VERSION_LABEL);
    }

    return dotBuilder;
  }

  private GraphBuilderAdapter createGraphBuilderAdapter() {
    GraphBuilderAdapter adapter;
    if (requiresFullGraph()) {
      adapter = new GraphBuilderAdapter(this.dependencyTreeBuilder, this.localRepository);
    } else {
      adapter = new GraphBuilderAdapter(this.dependencyGraphBuilder);
    }
    return adapter;
  }

  private boolean requiresFullGraph() {
    return this.showConflicts || this.showDuplicates;
  }
}

@@ -947,17 +947,16 @@
 nScope;%0A
-%0A
 import c
@@ -996,16 +996,68 @@
 Builder;
+%0Aimport com.github.ferstl.depgraph.dot.NodeRenderer;
 %0A%0A/**%0A *
@@ -1378,16 +1378,230 @@
 Mojo %7B%0A%0A
+  /**%0A   * If set to %7B@code true%7D, the created graph will show the %7B@code groupId%7D on all artifacts.%0A   * @since 1.0.3%0A   */%0A  @Parameter(property = %22showGroupIds%22, defaultValue = %22false%22)%0A  boolean showGroupIds;%0A%0A
   /**%0A  
@@ -3169,80 +3169,64 @@
 _ID)
+;%0A
 %0A    
-  .useNodeLabelRenderer(NodeRenderers.ARTIFACT_ID_LABEL
+boolean fullGraph = requiresFullGraph(
 );%0A
-%0A
     if (
 requ
@@ -3221,33 +3221,25 @@
     if (
-requiresF
+f
 ullGraph
 ()) %7B%0A  
@@ -3222,34 +3222,32 @@
    if (fullGraph
-()
 ) %7B%0A      // For
@@ -3430,212 +3430,85 @@
    %7D
- else if (this.showVersions) %7B%0A      // On the effective dependency graph we can display the versions within the nodes%0A      dotBuilder.useNodeLabelRenderer(NodeRenderers.ARTIFACT_ID_VERSION_LABEL);%0A    %7D
+%0A%0A    dotBuilder.useNodeLabelRenderer(determineNodeLabelRenderer(fullGraph));
 %0A%0A  
@@ -3959,11 +3959,517 @@
 tes;%0A  %7D
+%0A%0A  private NodeRenderer determineNodeLabelRenderer(boolean isFullGraph) %7B%0A    NodeRenderer renderer = NodeRenderers.ARTIFACT_ID_LABEL;%0A%0A    if (this.showGroupIds && this.showVersions && !isFullGraph) %7B%0A      renderer = NodeRenderers.GROUP_ID_ARTIFACT_ID_VERSION_LABEL;%0A    %7D else if (this.showVersions && !isFullGraph) %7B%0A      renderer = NodeRenderers.ARTIFACT_ID_VERSION_LABEL;%0A    %7D else if (this.showGroupIds) %7B%0A      renderer = NodeRenderers.GROUP_ID_ARTIFACT_ID_LABEL;%0A    %7D%0A%0A    return renderer;%0A  %7D
 %0A%7D%0A

Add flag to show groupIds in the generated graph