package org.usergrid.vx.server.operations;

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

import org.usergrid.vx.experimental.IntraOp;
import org.usergrid.vx.experimental.multiprocessor.MultiProcessor;
import org.vertx.java.core.Handler;
import org.vertx.java.core.eventbus.Message;
import org.vertx.java.core.json.JsonArray;
import org.vertx.java.core.json.JsonObject;

public class MultiProcessorHandler implements Handler<Message<JsonObject>> {
  
  MultiProcessor p;
  
  public MultiProcessorHandler(MultiProcessor p){
    this.p=p;
  }
  /*
   * JsonObject params = operation.getObject("op");
        JsonObject theParams = operation.getObject("params");
        operation.putObject("mpparams", theParams);
        operation.putObject("mpres", results.getObject("opsRes"));        
        System.out.println("sendingevent to"+ params.getString("name"));
        vertx.eventBus().send("multiprocessors." + params.getString("name"), operation, this);
   */

  /*
   *         IntraOp op = req.getE().get(i);
        String name = (String) op.getOp().get("name");
        Map params  = (Map) op.getOp().get("params");
        //Processor p = state.processors.get(processorName);
        MultiProcessor p = state.multiProcessors.get(name);

        List<Map> mpResults =  p.multiProcess(res.getOpsRes(), params);
        res.getOpsRes().put(i, mpResults);
      }
   */
  @Override
  public void handle(Message<JsonObject> event) {
    System.out.println(event);
    Integer id = event.body.getInteger("id");
    Map params = event.body.getObject("mpparams").toMap();
    System.out.println(params);
    Map mpres = event.body.getObject("mpres").toMap();
    System.out.println(mpres);
    List<Map> results = p.multiProcess(mpres, params);
    System.out.println(results);
    JsonArray ja = new JsonArray();
    for (Map result: results){
      ja.addObject( new JsonObject(result));
    }
    event.reply(new JsonObject().putArray(id.toString(), ja));
    
  }

}

@@ -58,109 +58,36 @@
 til.
-ArrayList;%0Aimport java.util.List;%0Aimport java.util.Map;%0A%0Aimport org.usergrid.vx.experimental.IntraOp;
+List;%0Aimport java.util.Map;%0A
 %0Aimp
@@ -496,836 +496,8 @@
 %7D%0A  
-/*%0A   * JsonObject params = operation.getObject(%22op%22);%0A        JsonObject theParams = operation.getObject(%22params%22);%0A        operation.putObject(%22mpparams%22, theParams);%0A        operation.putObject(%22mpres%22, results.getObject(%22opsRes%22));        %0A        System.out.println(%22sendingevent to%22+ params.getString(%22name%22));%0A        vertx.eventBus().send(%22multiprocessors.%22 + params.getString(%22name%22), operation, this);%0A   */%0A%0A  /*%0A   *         IntraOp op = req.getE().get(i);%0A        String name = (String) op.getOp().get(%22name%22);%0A        Map params  = (Map) op.getOp().get(%22params%22);%0A        //Processor p = state.processors.get(processorName);%0A        MultiProcessor p = state.multiProcessors.get(name);%0A%0A        List%3CMap%3E mpResults =  p.multiProcess(res.getOpsRes(), params);%0A        res.getOpsRes().put(i, mpResults);%0A      %7D%0A   */
 %0A  @
@@ -559,39 +559,8 @@
 ) %7B%0A
-    System.out.println(event);%0A
     
@@ -664,40 +664,8 @@
 ();%0A
-    System.out.println(params);%0A
     
@@ -719,39 +719,8 @@
 ();%0A
-    System.out.println(mpres);%0A
     
@@ -774,41 +774,8 @@
 s);%0A
-    System.out.println(results);%0A
     
@@ -955,16 +955,10 @@
 ));%0A
-    %0A
   %7D%0A%0A%7D
-%0A

No println