package com.proofpoint.event.client;

import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import org.codehaus.jackson.JsonEncoding;
import org.codehaus.jackson.JsonFactory;
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.map.JsonSerializer;
import org.codehaus.jackson.map.ObjectMapper;

import javax.inject.Inject;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Map;
import java.util.Set;

import static com.proofpoint.event.client.EventJsonSerializer.createEventJsonSerializer;

public class JsonEventWriter
{
    private final ObjectMapper objectMapper;
    private final Map<Class<?>, JsonSerializer> serializers;

    @Inject
    public JsonEventWriter(ObjectMapper objectMapper, Set<EventTypeMetadata<?>> eventTypes, HttpEventClientConfig config)
    {
        Preconditions.checkNotNull(objectMapper, "objectMapper is null");
        Preconditions.checkNotNull(eventTypes, "eventTypes is null");
        Preconditions.checkNotNull(config, "config is null");

        this.objectMapper = objectMapper;

        ImmutableMap.Builder<Class<?>, JsonSerializer> serializerBuilder = ImmutableMap.builder();

        for (EventTypeMetadata<?> eventType : eventTypes) {
            serializerBuilder.put(eventType.getEventClass(), createEventJsonSerializer(eventType, config.getJsonVersion()));
        }
        this.serializers = serializerBuilder.build();
    }

    public <T> void writeEvents(EventClient.EventGenerator<T> events, OutputStream out)
            throws IOException
    {
        Preconditions.checkNotNull(events, "events is null");
        Preconditions.checkNotNull(out, "out is null");

        JsonFactory jsonFactory = objectMapper.getJsonFactory();
        final JsonGenerator jsonGenerator = jsonFactory.createJsonGenerator(out, JsonEncoding.UTF8);

        jsonGenerator.writeStartArray();

        events.generate(new EventClient.EventPoster<T>()
        {
            @Override
            public void post(T event)
                    throws IOException
            {
                JsonSerializer<T> serializer = serializers.get(event.getClass());
                if (serializer == null) {
                    throw new InvalidEventException("Event class [%s] has not been registered as an event", event.getClass().getName());
                }

                serializer.serialize(event, jsonGenerator, null);
            }
        });

        jsonGenerator.writeEndArray();
        jsonGenerator.flush();
    }
}

@@ -725,32 +725,35 @@
 , JsonSerializer
+%3C?%3E
 %3E serializers;%0A%0A
@@ -751,16 +751,16 @@
 lizers;%0A
-
 %0A    @In
@@ -1195,16 +1195,19 @@
 rializer
+%3C?%3E
 %3E serial
@@ -2181,17 +2181,20 @@
 lizer = 
-s
+getS
 erialize
@@ -2198,30 +2198,14 @@
 izer
-s.get
 (event
-.getClass()
 );%0A 
@@ -2529,24 +2529,24 @@
 EndArray();%0A
-
         json
@@ -2569,11 +2569,186 @@
 );%0A    %7D
+%0A%0A    @SuppressWarnings(%22unchecked%22)%0A    private %3CT%3E JsonSerializer%3CT%3E getSerializer(T event)%0A    %7B%0A        return (JsonSerializer%3CT%3E) serializers.get(event.getClass());%0A    %7D
 %0A%7D%0A

Fix unchecked warning for JsonSerializer