/*
 * Copyright 2013-2017 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.gateway.route;

import java.net.URI;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.springframework.cloud.gateway.filter.factory.WebFilterFactories;
import org.springframework.web.reactive.function.server.RequestPredicate;
import org.springframework.web.server.WebFilter;

import reactor.core.publisher.Flux;

/**
 * @author Spencer Gibb
 */
public class Routes {

	public static LocatorBuilder locator() {
		return new LocatorBuilder();
	}

	public static class LocatorBuilder {

		private List<Route> routes = new ArrayList<>();

		public RouteBuilder route(String id) {
			return new RouteBuilder(this).id(id);
		}

		private void add(Route route) {
			this.routes.add(route);
		}

		public RouteLocator build() {
			return () -> Flux.fromIterable(this.routes);
		}

	}

	public static class RouteBuilder {
		private final Route.Builder builder = Route.builder();
		private final LocatorBuilder locatorBuilder;

		private RouteBuilder(LocatorBuilder locatorBuilder) {
			this.locatorBuilder = locatorBuilder;
		}

		public RouteBuilder id(String id) {
			this.builder.id(id);
			return this;
		}

		public PredicateBuilder uri(String uri) {
			this.builder.uri(uri);
			return predicateBuilder();
		}

		public PredicateBuilder uri(URI uri) {
			this.builder.uri(uri);
			return predicateBuilder();
		}

		private PredicateBuilder predicateBuilder() {
			return new PredicateBuilder(this.builder, this.locatorBuilder);
		}

	}

	public static class PredicateBuilder {

		private final Route.Builder routeBuilder;
		private LocatorBuilder locatorBuilder;

		private PredicateBuilder(Route.Builder routeBuilder, LocatorBuilder locatorBuilder) {
			this.routeBuilder = routeBuilder;
			this.locatorBuilder = locatorBuilder;
		}

		/* TODO: has and, or & negate of RequestPredicate with terminal filters()?
		public RequestPredicateBuilder host(String pattern) {
			RequestPredicate requestPredicate = GatewayRequestPredicates.host(pattern);
		}*/

		public WebFilterBuilder predicate(RequestPredicate predicate) {
			this.routeBuilder.requestPredicate(predicate);
			return webFilterBuilder();
		}

		private WebFilterBuilder webFilterBuilder() {
			return new WebFilterBuilder(this.routeBuilder, this.locatorBuilder);
		}

	}

	public static class WebFilterBuilder {
		private Route.Builder builder;
		private LocatorBuilder locatorBuilder;

		public WebFilterBuilder(Route.Builder routeBuilder, LocatorBuilder locatorBuilder) {
			this.builder = routeBuilder;
			this.locatorBuilder = locatorBuilder;
		}

		public WebFilterBuilder webFilters(List<WebFilter> webFilters) {
			this.builder.webFilters(webFilters);
			return this;
		}

		public WebFilterBuilder add(WebFilter webFilter) {
			this.builder.add(webFilter);
			return this;
		}

		public WebFilterBuilder addAll(Collection<WebFilter> webFilters) {
			this.builder.addAll(webFilters);
			return this;
		}

		public WebFilterBuilder addResponseHeader(String headerName, String headerValue) {
			return add(WebFilterFactories.addResponseHeader(headerName, headerValue));
		}

		public LocatorBuilder and() {
			Route route = this.builder.build();
			this.locatorBuilder.add(route);
			return this.locatorBuilder;
		}
	}

}

@@ -1243,23 +1243,20 @@
 ic Route
-Builder
+Spec
  route(S
@@ -1286,23 +1286,20 @@
 ew Route
-Builder
+Spec
 (this).i
@@ -1491,23 +1491,20 @@
 ss Route
-Builder
+Spec
  %7B%0A%09%09pri
@@ -1618,23 +1618,20 @@
 te Route
-Builder
+Spec
 (Locator
@@ -1716,23 +1716,20 @@
 ic Route
-Builder
+Spec
  id(Stri
@@ -1796,31 +1796,28 @@
 ic Predicate
-Builder
+Spec
  uri(String 
@@ -1902,23 +1902,20 @@
 redicate
-Builder
+Spec
  uri(URI
@@ -2002,23 +2002,20 @@
 redicate
-Builder
+Spec
  predica
@@ -2051,23 +2051,20 @@
 redicate
-Builder
+Spec
 (this.bu
@@ -2131,23 +2131,20 @@
 redicate
-Builder
+Spec
  %7B%0A%0A%09%09pr
@@ -2244,23 +2244,20 @@
 redicate
-Builder
+Spec
 (Route.B
@@ -2630,23 +2630,20 @@
 ebFilter
-Builder
+Spec
  predica
@@ -2771,39 +2771,36 @@
 rivate WebFilter
-Builder
+Spec
  webFilterBuilde
@@ -2828,23 +2828,20 @@
 ebFilter
-Builder
+Spec
 (this.ro
@@ -2913,23 +2913,20 @@
 ebFilter
-Builder
+Spec
  %7B%0A%09%09pri
@@ -3013,23 +3013,20 @@
 ebFilter
-Builder
+Spec
 (Route.B
@@ -3175,23 +3175,20 @@
 ebFilter
-Builder
+Spec
  webFilt
@@ -3296,31 +3296,28 @@
 ic WebFilter
-Builder
+Spec
  add(WebFilt
@@ -3399,31 +3399,28 @@
 ic WebFilter
-Builder
+Spec
  addAll(Coll
@@ -3526,23 +3526,20 @@
 ebFilter
-Builder
+Spec
  addResp

Rename builders to Spec