package com.ociweb.gl.example.parallel;

import com.ociweb.gl.api.GreenCommandChannel;
import com.ociweb.gl.api.GreenRuntime;
import com.ociweb.gl.api.HTTPResponseService;
import com.ociweb.gl.api.MsgCommandChannel;
import com.ociweb.gl.api.PubSubListener;
import com.ociweb.gl.api.Writable;
import com.ociweb.json.encode.JSONRenderer;
import com.ociweb.pronghorn.network.config.HTTPContentTypeDefaults;
import com.ociweb.pronghorn.pipe.ChannelReader;
import com.ociweb.pronghorn.pipe.ChannelWriter;

public class RestResponder implements PubSubListener{

	private final HTTPResponseService cmd;
	private final GreenCommandChannel newCommandChannel;
	private final boolean useChunked;
	
	
    private static final JSONRenderer<ChannelReader> jsonRenderer = new JSONRenderer<ChannelReader>()
            .beginObject()
            .integer("value", o->o.readPackedInt())
            .string("other", (o,t)->t.append("text"))
            .endObject();
	
	private ChannelReader payloadW;	
	private Writable w = new Writable() {
		@Override
		public void write(ChannelWriter writer) {
			jsonRenderer.render(writer, payloadW);
		}
	};

	public RestResponder(GreenRuntime runtime, boolean chunked) {
		newCommandChannel = runtime.newCommandChannel();
		cmd = newCommandChannel.newHTTPResponseService(1<<12,250);
		useChunked = chunked;
	}
	
	@Override
	public boolean message(CharSequence topic, ChannelReader payload) {
		payloadW = payload;
		
		if (useChunked) {
			if (cmd.hasRoomFor(2)) {
						
				long connectionId = payload.readPackedLong();
				long sequenceCode = payload.readPackedLong();
				
				cmd.publishHTTPResponse(
						connectionId, 
						sequenceCode, 
						200, true, HTTPContentTypeDefaults.JSON, w);
							
				//TODO: another issue, The end of the continuation MUST be non zero length!!.
				cmd.publishHTTPResponseContinuation(
						connectionId, 
						sequenceCode, 
						false, (w)->{w.append("hello");});
				
				   
				return true;
			} else {
				return false;
			}
		
		} else {
			return cmd.publishHTTPResponse(
					payload.readPackedLong(), 
					payload.readPackedLong(), 
					200, false, HTTPContentTypeDefaults.JSON, w);
		}
	}

}

@@ -988,16 +988,22 @@
 private 
+final 
 Writable
@@ -1024,16 +1024,16 @@
 ble() %7B%0A
-
 %09%09@Overr
@@ -1298,17 +1298,17 @@
 ice(1%3C%3C1
-2
+4
 ,250);%0A%09
@@ -1446,16 +1446,17 @@
 %09%0A%09%09if (
+!
 useChunk
@@ -1461,16 +1461,180 @@
 nked) %7B%0A
+%09%09%09return cmd.publishHTTPResponse(%0A%09%09%09%09%09payload.readPackedLong(), %0A%09%09%09%09%09payload.readPackedLong(), %0A%09%09%09%09%09200, false, HTTPContentTypeDefaults.JSON, w);%0A%09%09%0A%09%09%7D else %7B%0A
 %09%09%09if (c
@@ -1653,27 +1653,25 @@
 r(2)) %7B%0A%09%09%09%09
-%09%09
 %0A
+
 %09%09%09%09long con
@@ -1889,20 +1889,17 @@
 w);%0A%09%09%09%09
-%09%09%09
 %0A
+
 %09%09%09%09//TO
@@ -2105,19 +2105,16 @@
 %09%09%09%0A%09%09%09%09
-   
 %0A%09%09%09%09ret
@@ -2157,177 +2157,13 @@
 se;%0A
+
 %09%09%09%7D%0A
-%09%09%0A%09%09%7D else %7B%0A%09%09%09return cmd.publishHTTPResponse(%0A%09%09%09%09%09payload.readPackedLong(), %0A%09%09%09%09%09payload.readPackedLong(), %0A%09%09%09%09%09200, false, HTTPContentTypeDefaults.JSON, w);%0A
 %09%09%7D%0A

put postitive case first in test