/*
 * Copyright 2013-2015 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.kubernetes.discovery;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.netflix.client.config.IClientConfig;
import com.netflix.loadbalancer.AbstractServerList;

import io.fabric8.kubernetes.api.model.EndpointAddress;
import io.fabric8.kubernetes.api.model.EndpointPort;
import io.fabric8.kubernetes.api.model.EndpointSubset;
import io.fabric8.kubernetes.api.model.Endpoints;
import io.fabric8.kubernetes.api.model.EndpointsList;
import io.fabric8.kubernetes.client.KubernetesClient;

/**
 * @author Spencer Gibb
 */
public class KubernetesServerList extends AbstractServerList<KubernetesServer> {

	private final KubernetesClient kubernetes;
	private final KubernetesDiscoveryProperties properties;

	private String serviceId;

	public KubernetesServerList(KubernetesClient kubernetes, KubernetesDiscoveryProperties properties) {
		this.kubernetes = kubernetes;
		this.properties = properties;
	}

	@Override
	public void initWithNiwsConfig(IClientConfig clientConfig) {
		this.serviceId = clientConfig.getClientName();
	}

	@Override
	public List<KubernetesServer> getInitialListOfServers() {
		return getServers();
	}

	@Override
	public List<KubernetesServer> getUpdatedListOfServers() {
		return getServers();
	}

	private List<KubernetesServer> getServers() {
		if (this.kubernetes == null) {
			return Collections.emptyList();
		}
		EndpointsList endpointsList = this.kubernetes.endpoints().list();

		if (endpointsList == null || endpointsList.getItems().isEmpty()) {
			return Collections.emptyList();
		}

		List<KubernetesServer> servers = new ArrayList<>();

		for (Endpoints endpoints : endpointsList.getItems()) {
			String name = endpoints.getMetadata().getName();
			if (this.serviceId.equals(name)) { //TODO: filter on query?
				EndpointSubset subset = endpoints.getSubsets().iterator().next();
				EndpointAddress address = subset.getAddresses().iterator().next();
				EndpointPort port = subset.getPorts().iterator().next();
				String uid = endpoints.getMetadata().getUid();
				servers.add(new KubernetesServer(address.getIp(), port.getPort(), name, uid));
			}
		}
		return servers;
	}

	@Override
	public String toString() {
		final StringBuffer sb = new StringBuffer("KubernetesServerList{");
		sb.append("serviceId='").append(this.serviceId).append('\'');
		sb.append('}');
		return sb.toString();
	}
}

@@ -2095,16 +2095,59 @@
 s().
+withField(%22metadata.name%22, this.serviceId).
 list();%0A
 %0A%09%09i
@@ -2142,17 +2142,16 @@
 list();%0A
-%0A
 %09%09if (en
@@ -2422,72 +2422,64 @@
 %0A%09%09%09
-if (this.serviceId.equals(name)) %7B //TODO: filter on query?%0A%09%09%09%09
+String uid = endpoints.getMetadata().getUid();%0A%0A%09%09%09for (
 Endp
@@ -2496,17 +2496,17 @@
  subset 
-=
+:
  endpoin
@@ -2520,36 +2520,83 @@
 ubsets()
-.iterator().next();%0A
+) %7B%0A%09%09%09%09for (int i = 0; i %3C subset.getAddresses().size(); i++ ) %7B%0A%09
 %09%09%09%09Endp
@@ -2639,35 +2639,25 @@
 esses().
-iterator().next(
+get(i
 );%0A
+%09
 %09%09%09%09Endp
@@ -2694,78 +2694,17 @@
 s().
-iterator().next();%0A%09%09%09%09String uid = endpoints.getMetadata().getUid(
+get(i
 );%0A
+%09
 %09%09%09%09
@@ -2782,16 +2782,22 @@
  uid));%0A
+%09%09%09%09%7D%0A
 %09%09%09%7D%0A%09%09%7D
@@ -3036,10 +3036,11 @@
 g();%0A%09%7D%0A
-
 %7D%0A
+%0A

update server list to query by metadata.name