package test.codec.http2;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

import com.firefly.codec.http2.frame.SettingsFrame;
import com.firefly.codec.http2.model.HttpHeader;
import com.firefly.codec.http2.model.HttpURI;
import com.firefly.codec.http2.model.MetaData.Request;
import com.firefly.codec.http2.model.MetaData.Response;
import com.firefly.codec.http2.stream.HTTP2Configuration;
import com.firefly.codec.http2.stream.HTTPConnection;
import com.firefly.codec.http2.stream.HTTPOutputStream;
import com.firefly.server.http2.HTTP2Server;
import com.firefly.server.http2.ServerHTTPHandler;
import com.firefly.utils.io.BufferUtils;
import com.firefly.utils.log.Log;
import com.firefly.utils.log.LogFactory;

public class HTTP2ServerTLSDemo {

	private static Log log = LogFactory.getInstance().getLog("firefly-system");

	public static void main(String[] args) {
//		System.setProperty("javax.net.debug", "all");
			
		final HTTP2Configuration http2Configuration = new HTTP2Configuration();
		http2Configuration.setSecure(true);
		http2Configuration.setFlowControlStrategy("simple");
		http2Configuration.setTcpIdleTimeout(60 * 1000);

		final Map<Integer, Integer> settings = new HashMap<>();
		settings.put(SettingsFrame.HEADER_TABLE_SIZE, http2Configuration.getMaxDynamicTableSize());
		settings.put(SettingsFrame.INITIAL_WINDOW_SIZE, http2Configuration.getInitialStreamSendWindow());

		HTTP2Server server = new HTTP2Server("localhost", 6677, http2Configuration, new ServerHTTPHandler.Adapter() {
			@Override
			public boolean content(ByteBuffer item, Request request, Response response, HTTPOutputStream output,
					HTTPConnection connection) {
				log.info("received data, {}", BufferUtils.toString(item, StandardCharsets.UTF_8));
				return false;
			}

			@Override
			public boolean messageComplete(Request request, Response response, HTTPOutputStream outputStream,
					HTTPConnection connection) {
				log.info("received end frame, {}, {}", request.getURI(), request.getFields());
				HttpURI uri = request.getURI();
				if (uri.getPath().equals("/index")) {
					response.setStatus(200);
					try (HTTPOutputStream output = outputStream) {
						output.writeWithContentLength(
								BufferUtils.toBuffer("receive initial stream successful", StandardCharsets.UTF_8));
					} catch (IOException e) {
						e.printStackTrace();
					}
				} else if (uri.getPath().equals("/data")) {
					response.setStatus(200);
					response.getFields().put(HttpHeader.TRAILER, "foo");
					response.getFields().put("Trailer-Value", "mytest");
					try (HTTPOutputStream output = outputStream) {
//						output.writeWithContentLength(new ByteBuffer[]{BufferUtils.toBuffer("receive data stream successful", StandardCharsets.UTF_8), BufferUtils.toBuffer("thank you", StandardCharsets.UTF_8)});
						output.write(BufferUtils.toBuffer("receive data stream successful\r\n", StandardCharsets.UTF_8));
						output.write(BufferUtils.toBuffer("thank you\r\n", StandardCharsets.UTF_8));
					} catch (IOException e) {
						e.printStackTrace();
					}
				} else if (uri.getPath().equals("/favicon.ico")) {
					response.setStatus(404);
					try (HTTPOutputStream output = outputStream) {
						output.writeWithContentLength(BufferUtils.toBuffer(uri.getPath() + " not found", StandardCharsets.UTF_8));
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
				return true;
			}
		});
		server.start();
	}

}

@@ -961,20 +961,21 @@
 args) %7B%0A
-//
 %09%09
+// 
 System.s
@@ -1012,19 +1012,16 @@
 %22all%22);%0A
-%09%09%09
 %0A%09%09final
@@ -1123,24 +1123,27 @@
 re(true);%0A%09%09
+// 
 http2Configu
@@ -2693,18 +2693,16 @@
 ream) %7B%0A
-//
 %09%09%09%09%09%09ou
@@ -2715,203 +2715,18 @@
 rite
-WithContentLength(new ByteBuffer%5B%5D%7BBufferUtils.toBuffer(%22receive data stream successful%22, StandardCharsets.UTF_8), BufferUtils.toBuffer(%22thank you%22, StandardCharsets.UTF_8)%7D);%0A%09%09%09%09%09%09output.write(
+(%0A%09%09%09%09%09%09%09%09
 Buff
@@ -2968,50 +2968,8 @@
 else
- if (uri.getPath().equals(%22/favicon.ico%22))
  %7B%0A%09
@@ -3041,32 +3041,32 @@
 outputStream) %7B%0A
-
 %09%09%09%09%09%09output.wri
@@ -3085,16 +3085,25 @@
 tLength(
+%0A%09%09%09%09%09%09%09%09
 BufferUt

add chunked encoding for http2