package com.atlassian.maven.plugins.amps;

import com.atlassian.maven.plugins.amps.product.ProductHandlerFactory;
import com.atlassian.maven.plugins.amps.util.GoogleAmpsTracker;

import org.apache.maven.plugin.MojoExecutionException;
import org.jfrog.maven.annomojo.annotations.MojoParameter;

public abstract class AbstractProductAwareMojo extends AbstractAmpsMojo
{
    /**
     * Product id
     */
    @MojoParameter(expression = "${product}")
    private String product;

    /**
     * Instance to run. If provided, used to determine the product to use, instead of
     * using the product ID.
     */
    @MojoParameter(expression = "${instanceId}")
    protected String instanceId;

    /**
     * Flag to turn on/off google tracking
     *
     * AMPS sends basic usage events to google analytics by default. To disable tracking, you can either pass in
     * -Dallow.google.tacking=false on the command line, or include <allowGoogleTracking>false</allowGoogleTracking> in
     * the amps plugin configuration in your pom.xml or settings.xml
     *
     */
    @MojoParameter(expression = "${allow.google.tracking}", defaultValue = "true")
    protected boolean allowGoogleTracking;

    private GoogleAmpsTracker googleTracker;


    protected String getDefaultProductId() throws MojoExecutionException
    {
        return null;
    }

    protected final String getProductId() throws MojoExecutionException
    {
        if (product == null)
        {
            product = getDefaultProductId();
            if (product == null && ProductHandlerFactory.getIds().contains(getPluginInformation().getId()))
            {
                product = getPluginInformation().getId();
            }
            else if (product == null)
            {
                product = ProductHandlerFactory.REFAPP;
            }
        }
        return product;
    }

    protected GoogleAmpsTracker getGoogleTracker() throws MojoExecutionException
    {
        if(null == googleTracker)
        {
            googleTracker = new GoogleAmpsTracker(getProductId(),getLog());

            if(googleTrackingAllowed()) {
                getLog().info("Google Analytics Tracking is enabled to collect AMPS usage statistics.");
                getLog().info("Although no personal information is sent, you may disable tracking by adding <allowGoogleTracking>false</allowGoogleTracking> to the amps plugin configuration in your pom.xml");
            }
        }

        googleTracker.setEnabled(googleTrackingAllowed());

        return googleTracker;
    }

    protected boolean googleTrackingAllowed() {
        return allowGoogleTracking;
    }
}

@@ -703,16 +703,19 @@
   * 
+%3Cp%3E
 Flag to 
 turn
@@ -714,21 +714,16 @@
  to 
-turn on/off g
+enable G
 oogl
@@ -732,16 +732,21 @@
 tracking
+.%3C/p%3E
 %0A     *%0A
@@ -752,16 +752,19 @@
 %0A     * 
+%3Cp%3E
 AMPS sen
@@ -788,17 +788,17 @@
 ents to 
-g
+G
 oogle an
@@ -842,40 +842,57 @@
 ng, 
-you can either pass in%0A     * -D
+either:%3C/p%3E%0A     * %3Col%3E%0A     * %3Cli%3EAdd %3Ccode%3E&lt;
 allo
@@ -905,54 +905,185 @@
 le.t
+r
 acking
-=
+%3E
 false
- on the command line, or include %3C
+&lt;/allow.google.tracking%3E%3C/code%3E to the%0A     *  %3Ccode%3E&lt;properties%3E%3C/code%3E section of your %3Ccode%3E.m2/settings.xml%3C/code%3E file%3C/li%3E%0A     * %3Cli%3EInclude %3Ccode%3E&lt;
 allo
@@ -1095,33 +1095,36 @@
 leTracking%3Efalse
-%3C
+&lt;
 /allowGoogleTrac
@@ -1128,16 +1128,23 @@
 racking%3E
+%3C/code%3E
  in%0A    
@@ -1188,38 +1188,129 @@
 our 
+%3Ccode%3E
 pom.xml
- or settings.xml
+%3C/code%3E%3C/li%3E%0A     * %3Cli%3Eor pass %3Ccode%3E-Dallow.google.tracking=false%3C/code%3E on the command line.
 %0A     *
+ %3C/ol%3E
 %0A   

Improve instructions for disabling Google tracking.