package com.auth0.net;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.codec.binary.Base64;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

@SuppressWarnings("WeakerAccess")
class Telemetry {
    static final String HEADER_NAME = "Auth0-Client";

    private static final String NAME_KEY = "name";
    private static final String VERSION_KEY = "version";
    private static final String CORE_KEY = "core";
    private static final String ENV_KEY = "env";
    private static final String JAVA_KEY = "java";

    private final String name;
    private final String version;
    private final String core;
    private final Map<String, String> env;
    private final String value;

    Telemetry(String name, String version) {
        this(name, version, null);
    }

    Telemetry(String name, String version, String core) {
        this.name = name;
        this.version = version;
        this.core = core;

        Map<String, Object> values = new HashMap<>();
        if (name != null) {
            values.put(NAME_KEY, name);
        }
        if (version != null) {
            values.put(VERSION_KEY, version);
        }

        HashMap<String, String> tmpEnv = new HashMap<>();
        tmpEnv.put(JAVA_KEY, Runtime.class.getPackage().getSpecificationVersion());
        if (core != null) {
            tmpEnv.put(CORE_KEY, core);
        }
        this.env = Collections.unmodifiableMap(tmpEnv);
        values.put(ENV_KEY, env);

        String tmpValue;
        try {
            String json = new ObjectMapper().writeValueAsString(values);
            tmpValue = Base64.encodeBase64URLSafeString(json.getBytes());
        } catch (JsonProcessingException e) {
            tmpValue = null;
            e.printStackTrace();
        }
        value = tmpValue;
    }

    public String getName() {
        return name;
    }

    public String getVersion() {
        return version;
    }

    //Visible for testing
    String getLibraryVersion() {
        return core;
    }

    //Visible for testing
    Map<String, String> getEnvironment() {
        return env;
    }

    public String getValue() {
        return value;
    }
}

@@ -289,16 +289,23 @@
 ccess%22)%0A
+public 
 class Te
@@ -801,24 +801,31 @@
 value;%0A%0A    
+public 
 Telemetry(St
@@ -899,16 +899,23 @@
  %7D%0A%0A    
+public 
 Telemetr

make Telemetry a public class