/*
 * Copyright 2016 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.cloud.function.web;

import java.util.function.Function;

import org.reactivestreams.Publisher;

import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.function.registry.FileSystemFunctionRegistry;
import org.springframework.cloud.function.registry.FunctionRegistry;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.server.reactive.HttpHandler;
import org.springframework.http.server.reactive.ReactorHttpHandlerAdapter;
import org.springframework.util.StringUtils;
import org.springframework.web.reactive.function.RequestPredicates;
import org.springframework.web.reactive.function.RouterFunction;
import org.springframework.web.reactive.function.RouterFunctions;
import org.springframework.web.reactive.function.ServerRequest;
import org.springframework.web.reactive.function.ServerResponse;

import static org.springframework.http.codec.BodyExtractors.toFlux;
import static org.springframework.http.codec.BodyInserters.fromPublisher;

import reactor.core.publisher.Flux;
import reactor.ipc.netty.NettyContext;
import reactor.ipc.netty.http.server.HttpServer;

/**
 * @author Mark Fisher
 */
@Configuration
@EnableConfigurationProperties({ FunctionConfigurationProperties.class,
		WebConfigurationProperties.class })
public class RestConfiguration {

	@Autowired
	private FunctionConfigurationProperties functionProperties;

	@Autowired
	private WebConfigurationProperties webProperties;

	@Bean
	public FunctionRegistry registry() {
		return new FileSystemFunctionRegistry();
	}

	@Bean
	public HttpHandler httpHandler(FunctionRegistry registry) {
		String name = functionProperties.getName();
		Function<Flux<String>, Flux<String>> function = (name.indexOf(',') == -1)
				? registry.lookupFunction(name)
				: registry.composeFunction(
						StringUtils.commaDelimitedListToStringArray(name));
		FunctionInvokingHandler handler = new FunctionInvokingHandler(function);
		RouterFunction<Publisher<String>> route = RouterFunctions.route(
				RequestPredicates.POST(webProperties.getPath())
						.and(RequestPredicates.contentType(MediaType.TEXT_PLAIN)),
				handler::handleText);
		return RouterFunctions.toHttpHandler(route);
	}

	@Bean
	public LifecycleAwareHttpServer httpServer(HttpHandler handler) {
		return new LifecycleAwareHttpServer(handler, webProperties.getPort());
	}

	private static class FunctionInvokingHandler {

		private final Function<Flux<String>, Flux<String>> function;

		private FunctionInvokingHandler(Function<Flux<String>, Flux<String>> function) {
			this.function = function;
		}

		private ServerResponse<Publisher<String>> handleText(ServerRequest request) {
			Flux<String> input = request.body(toFlux(String.class));
			Publisher<String> output = this.function.apply(input);
			return ServerResponse.ok().body(fromPublisher(output, String.class));
		}
	}

	private static class LifecycleAwareHttpServer
			implements InitializingBean, DisposableBean {

		private final HttpHandler handler;

		private final int port;

		private volatile NettyContext server;

		private LifecycleAwareHttpServer(HttpHandler handler, int port) {
			this.handler = handler;
			this.port = port;
		}

		@Override
		public void afterPropertiesSet() {
			ReactorHttpHandlerAdapter adapter = new ReactorHttpHandlerAdapter(
					this.handler);
			final HttpServer server = HttpServer.create("localhost", port);
			Thread thread = new Thread(new Runnable() {

				@Override
				public void run() {
					LifecycleAwareHttpServer.this.server = server.newHandler(adapter)
							.block();
					while (LifecycleAwareHttpServer.this.server != null) {
						try {
							Thread.sleep(100L);
						}
						catch (InterruptedException e) {
							Thread.currentThread().interrupt();
						}
					}
				}
			});
			thread.setDaemon(false);
			thread.start();
		}

		@Override
		public void destroy() {
			if (this.server != null) {
				this.server.dispose();
				this.server = null;
			}
		}
	}
}

@@ -1525,16 +1525,23 @@
 unction.
+server.
 RequestP
@@ -1592,32 +1592,39 @@
 active.function.
+server.
 RouterFunction;%0A
@@ -1672,16 +1672,23 @@
 unction.
+server.
 RouterFu
@@ -1737,32 +1737,39 @@
 active.function.
+server.
 ServerRequest;%0Ai
@@ -1816,16 +1816,23 @@
 unction.
+server.
 ServerRe
@@ -1870,34 +1870,45 @@
 ngframework.
-http.codec
+web.reactive.function
 .BodyExtract
@@ -1957,18 +1957,29 @@
 ork.
-http.codec
+web.reactive.function
 .Bod
@@ -2037,24 +2037,60 @@
 isher.Flux;%0A
+import reactor.core.publisher.Mono;%0A
 import react
@@ -2994,33 +2994,30 @@
 unction%3C
-Publisher%3CString%3E
+ServerResponse
 %3E route 
@@ -3626,16 +3626,21 @@
 private 
+Mono%3C
 ServerRe
@@ -3649,26 +3649,8 @@
 onse
-%3CPublisher%3CString%3E
 %3E ha

Fix for another API change