package com.atlassian.maven.plugins.amps;

import com.atlassian.maven.plugins.amps.product.ProductHandler;
import com.atlassian.maven.plugins.amps.product.ProductHandlerFactory;
import com.atlassian.maven.plugins.amps.product.studio.StudioProductHandler;
import com.google.common.collect.Lists;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.jfrog.maven.annomojo.annotations.MojoGoal;
import org.jfrog.maven.annomojo.annotations.MojoParameter;

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

/**
 * Creates a zip file containing the previous run's home directory
 * in the proper format to use as test-resources.
 *
 * @since 3.1-m3
 */
@MojoGoal("create-home-zip")
public class CreateHomeZipMojo extends AbstractProductHandlerMojo {

    /**
     * Generated home directory zip file.
     */
    @MojoParameter(expression = "${homeZip}", required = false)
    protected File homeZipFile;

    public void doExecute() throws MojoExecutionException, MojoFailureException
    {
        Product product = getProduct(instanceId);
        ProductHandler productHandler = createProductHandler(product.getId());

        if (ProductHandlerFactory.STUDIO.equals(product.getId()))
        {
            configureStudio(product, (StudioProductHandler) productHandler);
        }

        final File snapshotDir = productHandler.getSnapshotDirectory(product);
        if (homeZipFile == null)
        {
            homeZipFile = new File(productHandler.getBaseDirectory(product), "generated-test-resources.zip");
        }

        productHandler.createHomeZip(snapshotDir, homeZipFile, product);

        getLog().info("Home directory zip created successfully at " + homeZipFile.getAbsolutePath());
    }




    /**
     * Configure the Studio product.
     *
     * @param studioProduct the studio product. Must not be another product, neither null.
     * @param studioProductHandler the Studio product handler
     */
    private void configureStudio(Product studioProduct, StudioProductHandler studioProductHandler) throws MojoExecutionException
    {
        List<ProductExecution> executions = Lists.newArrayList(new ProductExecution(studioProduct, studioProductHandler));
        includeStudioDependentProducts(executions, getMavenGoals());
    }

    private Product getProduct(final String instanceId) throws MojoExecutionException
    {
        Map<String, Product> contexts = getProductContexts(getMavenGoals());

        Product product = contexts.get(instanceId);
        if (product == null)
        {
            throw new MojoExecutionException("There is no instance with name " + instanceId + " defined in the pom.xml");
        }
        return product;
    }
}

@@ -968,20 +968,16 @@
  homeZip
-File
 ;%0A%0A    p
@@ -1449,20 +1449,16 @@
 (homeZip
-File
  == null
@@ -1488,20 +1488,16 @@
  homeZip
-File
  = new F
@@ -1643,20 +1643,16 @@
  homeZip
-File
 , produc
@@ -1733,20 +1733,16 @@
  homeZip
-File
 .getAbso
@@ -1750,24 +1750,135 @@
 utePath());%0A
+%0A        // Make the file the artifact of the project%0A        getMavenGoals().attachArtifact(homeZip, %22zip%22);%0A%0A
     %7D%0A%0A%0A%0A%0A  

AMPS-539 Fix the homeZip discrepancy