/*
 * Copyright 2012 Red Hat, Inc. and/or its affiliates.
 *
 * Licensed under the Eclipse Public License version 1.0, available at
 * http://www.eclipse.org/legal/epl-v10.html
 */
package org.jboss.forge.addons;

import javax.inject.Inject;

import org.jboss.forge.container.addons.AddonRegistry;
import org.jboss.forge.dependencies.builder.CoordinateBuilder;
import org.jboss.forge.facets.AbstractFacet;
import org.jboss.forge.maven.plugins.ConfigurationBuilder;
import org.jboss.forge.maven.plugins.ConfigurationElementBuilder;
import org.jboss.forge.maven.plugins.ExecutionBuilder;
import org.jboss.forge.maven.plugins.MavenPlugin;
import org.jboss.forge.maven.plugins.MavenPluginBuilder;
import org.jboss.forge.maven.projects.MavenPluginFacet;
import org.jboss.forge.projects.Project;
import org.jboss.forge.projects.ProjectFacet;

/**
 * @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
 *
 */
public class ForgeAddonFacet extends AbstractFacet<Project> implements ProjectFacet
{

   @Inject
   private AddonRegistry addonRegistry;

   @Override
   public boolean install()
   {
      Project project = getOrigin();
      // TODO: Change to @RequiresFacet
      if (!project.hasFacet(MavenPluginFacet.class))
      {
         MavenPluginFacet facet = addonRegistry.getExportedInstance(MavenPluginFacet.class).get();
         facet.setOrigin(project);
         project.install(facet);
      }
      MavenPluginFacet pluginFacet = getOrigin().getFacet(MavenPluginFacet.class);
      MavenPlugin forgeAddon = MavenPluginBuilder
               .create()
               .setCoordinate(CoordinateBuilder.create().setGroupId("org.apache.maven.plugins")
                        .setArtifactId("maven-compiler-plugin"))
               .addExecution(
                        ExecutionBuilder
                                 .create()
                                 .setId("create-forge-addon")
                                 .setPhase("package")
                                 .addGoal("jar")
                                 .setConfig(
                                          ConfigurationBuilder.create().addConfigurationElement(
                                                   ConfigurationElementBuilder.create().setName("classifier")
                                                            .setText("forge-addon"))));
      pluginFacet.addPlugin(forgeAddon);
      return true;
   }

   @Override
   public boolean isInstalled()
   {
      return false;
   }

   @Override
   public void setOrigin(Project origin)
   {
      super.setOrigin(origin);
   }
}

@@ -240,63 +240,8 @@
 t;%0A%0A
-import org.jboss.forge.container.addons.AddonRegistry;%0A
 impo
@@ -291,32 +291,32 @@
 rdinateBuilder;%0A
+
 import org.jboss
@@ -340,24 +340,68 @@
 tractFacet;%0A
+import org.jboss.forge.facets.FacetFactory;%0A
 import org.j
@@ -901,16 +901,17 @@
 I%3C/a%3E%0A *
+ 
 %0A */%0Apub
@@ -993,17 +993,16 @@
 Facet%0A%7B%0A
-%0A
    @Inje
@@ -1019,33 +1019,26 @@
 ate 
-AddonRegistry addonRegist
+FacetFactory facto
 ry;%0A
@@ -1117,24 +1117,25 @@
 etOrigin();%0A
+%0A
       // TOD
@@ -1137,17 +1137,31 @@
 / TODO: 
-C
+Implement and c
 hange to
@@ -1275,98 +1275,47 @@
 t = 
-addonRegistry.getExportedInstance(MavenPluginFacet.class).get();%0A         facet.setOrigin(
+factory.create(MavenPluginFacet.class, 
 proj
@@ -1357,16 +1357,16 @@
 t);%0A
-
       %7D%0A
     
@@ -1361,16 +1361,17 @@
       %7D%0A
+%0A
       Ma

Update ForgeAddonFacet to use FacetFactory