package uk.me.graphe.server.messages;

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

import org.json.JSONException;
import org.json.JSONObject;

import uk.me.graphe.server.messages.factories.AddEdgeFactory;
import uk.me.graphe.server.messages.factories.AddNodeFactory;
import uk.me.graphe.server.messages.factories.ConversionFactory;
import uk.me.graphe.server.messages.factories.DeleteEdgeFactory;
import uk.me.graphe.server.messages.factories.DeleteNodeFactory;
import uk.me.graphe.server.messages.factories.HeartbeatFactory;
import uk.me.graphe.server.messages.factories.MakeGraphFactory;
import uk.me.graphe.server.messages.factories.MoveNodeFactory;

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());
    }

	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) {
		return sOpFactoryMap.get(messageVal).make(o);
	}
	
	
}

@@ -701,16 +701,82 @@
 Factory;
+%0Aimport uk.me.graphe.server.messages.factories.NoSuchGraphFactory;
 %0A%0Apublic
@@ -880,20 +880,16 @@
 = null;%0A
-    
 %0A    sta
@@ -904,16 +904,17 @@
       //
+ 
 TODO: ge
@@ -1474,20 +1474,355 @@
 ));%0A    
-%7D%0A%0A%09
+    sOpFactoryMap.put(%22openGraph%22, new OpenGraphFactory());%0A        sOpFactoryMap.put(%22updateStateId%22, new StateIdFactory());%0A        sOpFactoryMap.put(%22requestGraph%22, new RequestGraphFactory());%0A        sOpFactoryMap.put(%22noSuchGraph%22, new NoSuchGraphFactory());%0A        sOpFactoryMap.put(%22composite%22, new CompositeFactory());%0A    %7D%0A%0A    
 public s
@@ -1886,16 +1886,28 @@
 t%3E jsos)
+%0A           
  throws 
@@ -1922,21 +1922,25 @@
 ption %7B%0A
-%09%09%0A%09%09
+%0A        
 List%3CMes
@@ -1980,21 +1980,25 @@
 age%3E();%0A
-%09%09%0A%09%09
+%0A        
 for (JSO
@@ -2017,19 +2017,28 @@
 jsos) %7B%0A
-%09%09%09
+            
 String m
@@ -2073,19 +2073,28 @@
 sage%22);%0A
-%09%09%09
+            
 Message 
@@ -2138,11 +2138,20 @@
 o);%0A
-%09%09%09
+            
 resu
@@ -2166,17 +2166,27 @@
 p);%0A
-%09%09%7D%0A%09%09%0A%09%09
+        %7D%0A%0A        
 retu
@@ -2200,13 +2200,19 @@
 lt;%0A
-%09%7D%0A%0A%09
+    %7D%0A%0A    
 priv
@@ -2274,61 +2274,232 @@
 ) %7B%0A
-%09%09return sOpFactoryMap.get(messageVal).make(o);%0A%09%7D%0A%09%0A%09
+        if (sOpFactoryMap.containsKey(messageVal)) %7B%0A            return sOpFactoryMap.get(messageVal).make(o);%0A        %7D else %7B%0A            throw new Error(%22message factory: got a message I couldn't decode%22);%0A        %7D%0A    %7D%0A
 %0A%7D%0A

make messagefactory able to produce new kinds of message