package org.jboss.forge.maven.dependencies;

import org.sonatype.aether.collection.DependencyCollectionContext;
import org.sonatype.aether.collection.DependencySelector;
import org.sonatype.aether.graph.Dependency;
import org.sonatype.aether.util.graph.selector.StaticDependencySelector;

/**
 * A dependency selector that filters based on their scope and classifier "forge-addon"
 *
 * @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
 */
public class AddonDependencySelector
         implements DependencySelector
{
   private static final String FORGE_ADDON = "forge-addon";
   private final int depth;

   public AddonDependencySelector()
   {
      this.depth = 0;
   }

   public AddonDependencySelector(int depth)
   {
      this.depth = depth;
   }

   @Override
   public boolean selectDependency(Dependency dependency)
   {
      if (depth < 1)
         return true;

      String scope = dependency.getScope();
      String classifier = dependency.getArtifact().getClassifier();

      if ("test".equals(scope))
         return false;

      boolean result = (FORGE_ADDON.equals(classifier) && depth == 1)
               || (!FORGE_ADDON.equals(classifier) && !"provided".equals(scope));
      return result;
   }

   @Override
   public DependencySelector deriveChildSelector(DependencyCollectionContext context)
   {
      if ((depth > 0) && FORGE_ADDON.equals(context.getDependency().getArtifact().getClassifier()))
      {
         return new StaticDependencySelector(false);
      }
      return new AddonDependencySelector(depth + 1);
   }

   @Override
   public boolean equals(Object obj)
   {
      if (this == obj)
      {
         return true;
      }
      else if (null == obj || !getClass().equals(obj.getClass()))
      {
         return false;
      }

      AddonDependencySelector that = (AddonDependencySelector) obj;
      return depth == that.depth;
   }

   @Override
   public int hashCode()
   {
      int hash = 17;
      hash = hash * 31 + depth;
      return hash;
   }

}

@@ -385,16 +385,17 @@
 don%22%0D%0A *
+ 
 %0D%0A * @au
@@ -875,32 +875,85 @@
 endency)%0D%0A   %7B%0D%0A
+      boolean optional = dependency.isOptional();%0D%0A%0D%0A
       if (depth 
@@ -974,20 +974,25 @@
  return 
-true
+!optional
 ;%0D%0A%0D%0A   
@@ -1313,18 +1313,33 @@
 s(scope)
-);
+ && !optional);%0D%0A
 %0D%0A      

Fix optional deps being pulled into addon deployments