package org.cyclops.cyclopscore.tracking;

import com.google.common.collect.Lists;
import net.minecraft.server.MinecraftServer;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.logging.log4j.Level;
import org.cyclops.cyclopscore.GeneralConfig;
import org.cyclops.cyclopscore.Reference;
import org.cyclops.cyclopscore.init.ModBase;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.List;

/**
 * An analytics service that uses the Google Measurement Protocol.
 * This conforms to the Google Measurement Protocol policy: https://developers.google.com/analytics/devguides/collection/protocol/policy
 * No data is collected which can be used to identify a player.
 * This will be disabled when the Minecraft snooper is disabled or if this service is disabled in the config file.
 * @author rubensworks
 */
public class Analytics {

    // Tracking id, Anonymized user id, Mod name, Mod version, Minecraft version
    private static final String REQUEST_PATTERN = "http://www.google-analytics.com/collect?v=1&tid=%s&cid=%s&t=screenview&cd=Startup&an=%s&av=%s&cd1=%s";
    private static boolean checked = false;

    private static final List<Pair<ModBase, String>> trackingMods = Lists.newLinkedList();

    /**
     * Register a new mod to track.
     * Make sure to call this before the mod init phase.
     * @param mod The mod.
     * @param trackingId The Google Analytics tracking id to use.
     */
    public static synchronized void registerMod(ModBase mod, String trackingId) {
        trackingMods.add(Pair.of(mod, trackingId));
    }

    protected static synchronized List<Pair<ModBase, String>> getTrackingMods() {
        return Lists.newArrayList(trackingMods);
    }

    /**
     * Send tracking info for all registered mods.
     * This should and can only be called once.
     */
    public static void sendAll() {
        if(!checked) {
            checked = true;
            if (MinecraftServer.getServer().isSnooperEnabled()) {
                new Thread(new Runnable() {

                    @Override
                    public void run() {
                        List<Pair<ModBase, String>> trackingMods = getTrackingMods();
                        for (Pair<ModBase, String> pair : trackingMods) {
                            try {
                                URL url = new URL(createRequestURL(pair.getLeft(), pair.getRight()));
                                //noinspection ResultOfMethodCallIgnored
                                IOUtils.toString(url); // Just do request, we don't need the output (?)
                            } catch (IOException e) {
                                pair.getLeft().log(Level.WARN, "Could not send tracking info: " + e.toString());
                            }
                        }
                    }
                }).run();
            }
        }
    }

    protected static String createRequestURL(ModBase mod, String trackingId) throws UnsupportedEncodingException {
        String mcVersion = URLEncoder.encode(Reference.MOD_MC_VERSION, "UTF-8");
        return String.format(REQUEST_PATTERN, trackingId, GeneralConfig.anonymousAnalyticsID, URLEncoder.encode(mod.getModName(), "UTF-8"),
                mcVersion + "-" + URLEncoder.encode(mod.getReferenceValue(ModBase.REFKEY_MOD_VERSION), "UTF-8"), mcVersion);
    }

}

@@ -330,16 +330,72 @@
 erence;%0A
+import org.cyclops.cyclopscore.helper.MinecraftHelpers;%0A
 import o
@@ -1098,16 +1098,45 @@
  version
+, Server/Client, Java Version
 %0A    pri
@@ -1279,16 +1279,30 @@
 s&cd1=%25s
+&cd2=%25s&cd3=%25s
 %22;%0A    p
@@ -3360,16 +3360,32 @@
 yticsID,
+%0A               
  URLEnco
@@ -3535,26 +3535,133 @@
 UTF-8%22),
- mcVersion
+%0A                mcVersion, MinecraftHelpers.isClientSide() ? %22client%22 : %22server%22, System.getProperty(%22java.version%22)
 );%0A    %7D

Update analytics parameters Adds server/client distinction and Java version.