package uk.me.graphe.shared.messages;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import uk.me.graphe.shared.jsonwrapper.JSONException;
import uk.me.graphe.shared.jsonwrapper.JSONObject;
import uk.me.graphe.shared.messages.factories.AddEdgeFactory;
import uk.me.graphe.shared.messages.factories.AddNodeFactory;
import uk.me.graphe.shared.messages.factories.ChatFactory;
import uk.me.graphe.shared.messages.factories.CompositeFactory;
import uk.me.graphe.shared.messages.factories.ConversionFactory;
import uk.me.graphe.shared.messages.factories.DeleteEdgeFactory;
import uk.me.graphe.shared.messages.factories.DeleteNodeFactory;
import uk.me.graphe.shared.messages.factories.HeartbeatFactory;
import uk.me.graphe.shared.messages.factories.MakeGraphFactory;
import uk.me.graphe.shared.messages.factories.MoveNodeFactory;
import uk.me.graphe.shared.messages.factories.NoSuchGraphFactory;
import uk.me.graphe.shared.messages.factories.OpenGraphFactory;
import uk.me.graphe.shared.messages.factories.RequestGraphFactory;
import uk.me.graphe.shared.messages.factories.StateIdFactory;



public abstract class MessageFactory {
    private static Map<String, ConversionFactory> sOpFactoryMap = null;

    static {
        // TODO: get the strings from the classes instead of hardcode them
        sOpFactoryMap = new HashMap<String, ConversionFactory>();
        sOpFactoryMap.put("addEdge", new AddEdgeFactory());
        sOpFactoryMap.put("delEdge", new DeleteEdgeFactory());
        sOpFactoryMap.put("addNode", new AddNodeFactory());
        sOpFactoryMap.put("delNode", new DeleteNodeFactory());
        sOpFactoryMap.put("movNode", new MoveNodeFactory());
        sOpFactoryMap.put("heartbeat", new HeartbeatFactory());
        sOpFactoryMap.put("makeGraph", new MakeGraphFactory());
        sOpFactoryMap.put("openGraph", new OpenGraphFactory());
        sOpFactoryMap.put("updateStateId", new StateIdFactory());
        sOpFactoryMap.put("requestGraph", new RequestGraphFactory());
        sOpFactoryMap.put("noSuchGraph", new NoSuchGraphFactory());
        sOpFactoryMap.put("composite", new CompositeFactory());
        sOpFactoryMap.put("chat", new ChatFactory());
    }

    public static List<Message> makeOperationsFromJson(List<JSONObject> jsos)
            throws JSONException {

        List<Message> result = new ArrayList<Message>();

        for (JSONObject o : jsos) {
            String messageVal = o.getString("message");
            Message op = MessageFactory.make(messageVal, o);
            result.add(op);
        }

        return result;
    }

    private static Message make(String messageVal, JSONObject o) {
        if (sOpFactoryMap.containsKey(messageVal)) {
            return sOpFactoryMap.get(messageVal).make(o);
        } else {
            throw new Error("message factory: got a message I couldn't decode");
        }
    }

}

@@ -1130,18 +1130,16 @@
 ctory;%0A%0A
-%0A%0A
 public a
@@ -1270,71 +1270,54 @@
  // 
-TODO: get the strings from the classes instead of hardcode them
+insert all the operations into the factory map
 %0A   
@@ -2203,24 +2203,321 @@
 ());%0A    %7D%0A%0A
+    /**%0A     * makes a list of messages from a list of json objects%0A     * %0A     * @param jsos%0A     *            the json objects to turn into messages%0A     * @return the json objects converted to messages%0A     * @throws JSONException%0A     *             if we can't convert for any reason%0A     */%0A
     public s
@@ -2581,28 +2581,16 @@
 t%3E jsos)
-%0A           
  throws 
@@ -2664,16 +2664,116 @@
 ge%3E();%0A%0A
+        // for all the messages that we get in, turn them into type safe message%0A        // objects%0A
         
@@ -2982,24 +2982,333 @@
 ult;%0A    %7D%0A%0A
+    /**%0A     * attempts to turn a json object into a type safe message, using messageval%0A     * as the type of message to construct%0A     * %0A     * @param messageVal the value of the message field%0A     * @param o the passed json object%0A     * @return a message object if the type of message is correct%0A     */%0A
     private 
@@ -3358,24 +3358,25 @@
 Object o) %7B%0A
+%0A
         if (

add some comments to message factory