package de.tourenplaner.algorithms.bbbundle;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.ObjectMapper;
import de.tourenplaner.computecore.StreamJsonWriter;
import de.tourenplaner.graphrep.GraphRep;

import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;

/**
 * This class is used to store and send back
 * subgraphs for client side computation
 */
public final class BBBundleResult implements StreamJsonWriter {

    //private final GraphRep graph;
    // TODO use IntArrayList
    private final ArrayList<BBBundleEdge> upEdges;
    private final ArrayList<BBBundleEdge> downEdges;
    private final int nodeCount;
    private final GraphRep graph;

    public BBBundleResult(GraphRep graph, int nodeCount, ArrayList<BBBundleEdge> upEdges, ArrayList<BBBundleEdge> downEdges) {
        this.upEdges = upEdges;
        this.downEdges = downEdges;
        this.nodeCount= nodeCount;
        this.graph = graph;
    }

    @Override
    public void writeToStream(ObjectMapper mapper, OutputStream stream) throws IOException {
        JsonGenerator gen = mapper.getFactory().createGenerator(stream);
        gen.writeStartObject();
        gen.writeNumberField("nodeCount", nodeCount);
        gen.writeArrayFieldStart("upEdges");
        for (BBBundleEdge e : upEdges) {
            gen.writeStartObject();
            gen.writeNumberField("src", e.srcId);
            gen.writeNumberField("trgt", e.trgtId);
            gen.writeNumberField("cost", e.cost);
            gen.writeArrayFieldStart("draw");
            for (int i = 0; i < e.unpacked.size(); ++i){
                writeDrawEdge(gen, e, i);
            }
            gen.writeEndArray();
            gen.writeEndObject();
        }
        gen.writeEndArray();

        gen.writeArrayFieldStart("downEdges");
        for (BBBundleEdge e : downEdges) {
            gen.writeStartObject();
            gen.writeNumberField("src", e.srcId);
            gen.writeNumberField("trgt", e.trgtId);
            gen.writeNumberField("cost", e.cost);
            gen.writeArrayFieldStart("draw");
            for (int i = 0; i < e.unpacked.size(); ++i){
                writeDrawEdge(gen, e, i);
            }
            gen.writeEndArray();
            gen.writeEndObject();
        }
        gen.writeEndArray();

        gen.writeEndObject();
        gen.flush();
    }

    public final void writeDrawEdge(JsonGenerator gen, BBBundleEdge e, int i) throws IOException {
        int edgeId = e.unpacked.get(i);
        int s = graph.getSource(edgeId);
        int t = graph.getTarget(edgeId);
        // TODO we need to save the real type
        int type = 0;
        gen.writeNumber(graph.getXPos(s));
        gen.writeNumber(graph.getYPos(s));
        gen.writeNumber(graph.getXPos(t));
        gen.writeNumber(graph.getYPos(t));
        gen.writeNumber(type);
    }
}

@@ -679,24 +679,56 @@
  nodeCount;%0A
+    private final int coreSize;%0A
     private 
@@ -865,32 +865,46 @@
 eEdge%3E downEdges
+, int coreSize
 ) %7B%0A        this
@@ -1018,24 +1018,58 @@
 ph = graph;%0A
+        this.coreSize = coreSize;%0A
     %7D%0A%0A    @
@@ -1287,51 +1287,379 @@
     
-gen.writeNumberField(%22nodeCount%22, nodeCount
+// Head%0A        gen.writeObjectFieldStart(%22head%22);%0A        gen.writeNumberField(%22nodeCount%22, nodeCount);%0A        gen.writeNumberField(%22upEdgeCount%22, upEdges.size());%0A        gen.writeNumberField(%22downEdgeCount%22, downEdges.size());%0A        gen.writeNumberField(%22coreSize%22, coreSize);%0A        gen.writeEndObject();%0A        // Edges%0A        gen.writeObjectFieldStart(%22edges%22
 );%0A 
@@ -2732,32 +2732,61 @@
 riteEndArray();%0A
+        gen.writeEndObject();
 %0A        gen.wri

Add upEdgeCount and downEdgeCount as well as coreSize to result