package com.aphyr.riemann.client;

import com.aphyr.riemann.Proto.Attribute;
import com.aphyr.riemann.Proto.Event;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.io.IOException;

public class EventDSL {
    public final AbstractRiemannClient client;
    public final Event.Builder builder;

    public EventDSL(AbstractRiemannClient client) {
        this.client = client;
        this.builder = Event.newBuilder();
        try {
            this.builder.setHost(java.net.InetAddress.getLocalHost().getHostName());
        } catch (java.net.UnknownHostException e) {
            // If we can't get the local host, a null host is perfectly
            // acceptable.  Caller will know soon enough. :)
        }
    }

    public EventDSL host(String host) {
        if (null == host) {
            builder.clearHost();
        } else {
            builder.setHost(host);
        }
        return this;
    }

    public EventDSL service(String service) {
        if (null == service) {
            builder.clearService();
        } else {
            builder.setService(service);
        }
        return this;
    }

    public EventDSL state(String state) {
        if (null == state) {
            builder.clearState();
        } else {
            builder.setState(state);
        }
        return this;
    }

    public EventDSL description(String description) {
        if (null == description) {
            builder.clearDescription();
        } else {
            builder.setDescription(description);
        }
        return this;
    }

    public EventDSL time(Null n) {
        builder.clearMetricF();
        return this;
    }

    public EventDSL time(float time) {
        builder.setTime((long) time);
        return this;
    }

    public EventDSL time(double time) {
        builder.setTime((long) time);
        return this;
    }
    public EventDSL time(long time) {
        builder.setTime(time);
        return this;
    }

    public EventDSL metric(Null n) {
        builder.clearMetricF();
        builder.clearMetricD();
        builder.clearMetricSint64();
        return this;
    }

    public EventDSL metric(byte metric) {
      builder.setMetricSint64((long) metric);
      builder.setMetricF((float) metric);
      return this;
    }

    public EventDSL metric(short metric) {
      builder.setMetricSint64((long) metric);
        builder.setMetricF((float) metric);
      return this;
    }

    public EventDSL metric(int metric) {
        builder.setMetricSint64((long) metric);
        builder.setMetricF((float) metric);
        return this;
    }

    public EventDSL metric(long metric) {
        builder.setMetricSint64(metric);
        builder.setMetricF((float) metric);
        return this;
    }

    public EventDSL metric(float metric) {
      builder.setMetricF(metric);
      return this;
    }

    public EventDSL metric(double metric) {
        builder.setMetricD(metric);
        builder.setMetricF((float) metric);
        return this;
    }

    public EventDSL tag(String tag) {
        builder.addTags(tag);
        return this;
    }

    public EventDSL tags(List<String> tags) {
        builder.addAllTags(tags);
        return this;
    }

    public EventDSL tags(String... tags) {
        builder.addAllTags(Arrays.asList(tags));
        return this;
    }

    public EventDSL ttl(Null n) {
        builder.clearTtl();
        return this;
    }

    public EventDSL ttl(float ttl) {
        builder.setTtl(ttl);
        return this;
    }

    public EventDSL attribute(String name, String value) {
        Attribute.Builder attribBuilder = Attribute.newBuilder();
        attribBuilder.setName(name);
        attribBuilder.setValue(value);
        builder.addAttributes(attribBuilder);
        return this;
    }

    public EventDSL attributes(Map<String, String> attributes) {
        for (Map.Entry<String, String> entry : attributes.entrySet()) {
            Attribute.Builder attribBuilder = Attribute.newBuilder();
            attribBuilder.setName(entry.getKey());
            attribBuilder.setValue(entry.getValue());
            builder.addAttributes(attribBuilder);
        }
        return this;
    }

    public Boolean sendWithAck() throws IOException, ServerError, MsgTooLargeException {
        return client.sendEventsWithAck(builder.build());
    }

    public void send() {
        client.sendEvents(builder.build());
    }
}

@@ -179,16 +179,42 @@
 il.Map;%0A
+import java.util.HashMap;%0A
 import j
@@ -344,16 +344,97 @@
 builder;
+%0A    public final Map%3CString, String%3E attributes = new HashMap%3CString, String%3E();
 %0A%0A    pu
@@ -3706,35 +3706,33 @@
 ue) %7B%0A      
-  A
+a
 ttribute
 .Builder att
@@ -3723,181 +3723,156 @@
 bute
-.Builder attribBuilder = Attribute.newBuilder();%0A        attribBuilder.setName(name);%0A        attribBuilder.setValue(value);%0A        builder.addAttributes(attribBuilder
+s.put(name, value);%0A      return this;%0A    %7D%0A%0A    public EventDSL attributes(Map%3CString, String%3E attributes) %7B%0A      attributes.putAll(attributes
 );%0A
-  
     
@@ -3901,71 +3901,164 @@
     
-public EventDSL attributes(Map%3CString, String%3E attributes
+// Returns the compiled Protobuf event for this DSL. Merges in the custom%0A    // attributes map. Can only be called safely once.%0A    public Event build(
 ) %7B%0A
-  
     
@@ -4131,20 +4131,16 @@
         
-    
 Attribut
@@ -4189,36 +4189,32 @@
 lder();%0A        
-  
-  
 attribBuilder.se
@@ -4232,28 +4232,24 @@
 .getKey());%0A
-    
         attr
@@ -4286,36 +4286,32 @@
 lue());%0A        
-    
 builder.addAttri
@@ -4332,18 +4332,16 @@
 ilder);%0A
-  
       %7D%0A
@@ -4342,37 +4342,46 @@
     %7D%0A      
-  
 return 
-this
+builder.build()
 ;%0A    %7D%0A%0A   
@@ -4506,24 +4506,16 @@
 WithAck(
-builder.
 build())
@@ -4574,24 +4574,16 @@
 dEvents(
-builder.
 build())

Store a temporary map of attributes.