package io.iron.ironmq;

import java.io.IOException;

import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;

/**
 * The Queue class represents a specific IronMQ queue bound to a client.
 */
public class Queue {
    final private Client client;
    final private String name;

    Queue(Client client, String name) {
        this.client = client;
        this.name = name;
    }

    /**
    * Retrieves a Message from the queue. If there are no items on the queue, an
    * HTTPException is thrown.
    *
    * @throws HTTPException If the IronMQ service returns a status other than 200 OK.
    * @throws IOException If there is an error accessing the IronMQ server.
    */
    public Message get() throws IOException {
        JSONObject jsonObj = client.get("queues/" + name + "/messages");
        Message msg = new Message();
        msg.setId((String)jsonObj.get("id"));
        msg.setBody(jsonObj.getString("body"));
        if (jsonObj.has("timeout")) {
            msg.setTimeout(jsonObj.getLong("timeout"));
        }
        return msg;
    }

    /**
    * Deletes a Message from the queue.
    *
    * @param id The ID of the message to delete.
    *
    * @throws HTTPException If the IronMQ service returns a status other than 200 OK.
    * @throws IOException If there is an error accessing the IronMQ server.
    */
    public void deleteMessage(String id) throws IOException {
        client.delete("queues/" + name + "/messages/" + id);
    }

    /**
    * Deletes a Message from the queue.
    *
    * @param msg The message to delete.
    *
    * @throws HTTPException If the IronMQ service returns a status other than 200 OK.
    * @throws IOException If there is an error accessing the IronMQ server.
    */
    public void deleteMessage(Message msg) throws IOException {
        deleteMessage(msg.getId());
    }

    /**
    * Pushes a message onto the queue.
    *
    * @param msg The body of the message to push.
    *
    * @throws HTTPException If the IronMQ service returns a status other than 200 OK.
    * @throws IOException If there is an error accessing the IronMQ server.
    */
    public void push(String msg) throws IOException {
        push(msg, 0);
    }

    /**
    * Pushes a message onto the queue.
    *
    * @param msg The body of the message to push.
    * @param timeout The timeout of the message to push.
    *
    * @throws HTTPException If the IronMQ service returns a status other than 200 OK.
    * @throws IOException If there is an error accessing the IronMQ server.
    */
    public void push(String msg, long timeout) throws IOException {
        Message message = new Message();
        message.setBody(msg);
        message.setTimeout(timeout);
        String jsonStr = JSONSerializer.toJSON(message).toString();
        client.post("queues/" + name + "/messages", jsonStr);
    }
}

@@ -47,16 +47,71 @@
 ption;%0A%0A
+import net.sf.json.JSON;%0Aimport net.sf.json.JSONArray;%0A
 import n
@@ -853,16 +853,130 @@
 ages%22);%0A
+        JSONArray array = jsonObj.getJSONArray(%22messages%22);%0A        JSONObject jsonMsg = array.getJSONObject(0);%0A%0A
         
@@ -1026,27 +1026,25 @@
 tId(
-(String)
 json
-Obj
+Msg
 .get
+String
 (%22id
@@ -1068,27 +1068,27 @@
 setBody(json
-Obj
+Msg
 .getString(%22
@@ -1112,19 +1112,19 @@
 if (json
-Obj
+Msg
 .has(%22ti
@@ -1161,27 +1161,27 @@
 Timeout(json
-Obj
+Msg
 .getLong(%22ti
@@ -2883,24 +2883,25 @@
 meout);%0A
+%0A
         
 String j
@@ -2896,22 +2896,20 @@
     
-String
+JSON
  json
-Str
+Msg
  = J
@@ -2941,18 +2941,165 @@
 age)
-.toString(
+;%0A        JSONArray array = new JSONArray();%0A        array.add(jsonMsg);%0A        JSONObject outer = new JSONObject();%0A        outer.element(%22messages%22, array
 );%0A 
@@ -3153,15 +3153,24 @@
 s%22, 
-jsonStr
+outer.toString()
 );%0A 

Use new, batch message style API