/**
 * Copyright (C) 2015 The Gravitee team (http://gravitee.io)
 *
 * 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 io.gravitee.reporter.elastic.config;

import io.gravitee.reporter.elastic.model.HostAddress;
import io.gravitee.reporter.elastic.model.Protocol;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;

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

/**
 * Elasticsearch client reporter configuration.
 *  
 * @author Loic DASSONVILLE (loic.dassonville at gmail.com)
 *
 */
public class ElasticConfiguration {
	
	private static final String PORT_SEPARATOR = ":";

	@Autowired
	private Environment environment;
	
	/**
	 *  Client communication protocol. 
	 */
	@Value("${reporters.elastic.protocol:TRANSPORT}")
	private Protocol protocol;
	
	/**
	 * Cluster name. Used only for node protocol
	 */
	@Value("${reporters.elastic.cluster.name:elasticsearch}")
	private String clusterName;
	
	/**
	 * Prefix index name. 
	 */
	@Value("${reporters.elastic.index.name:gravitee}")
	private String indexName;
	
	/**
	 * Request actions max by bulk 
	 */
	@Value("${reporters.elastic.bulk.actions:5000}")
	private Integer bulkActions;
	
	/**
	 * Bulk flush interval in seconds
	 */
	@Value("${reporters.elastic.bulk.flush_interval:5}")
	private Long flushInterval;
	
	/**
	 * Accepted concurrent request
	 */
	@Value("${reporters.elastic.bulk.concurrent_requests:5}")
	private Integer concurrentRequests;

	/**
	 * Elasticsearch hosts
	 */
	private List<HostAddress> hostsAddresses;
	
	private List<String> hostsUrls;

	public Protocol getProtocol() {
		return protocol;
	}

	public String getClusterName() {
		return clusterName;
	}

	public List<HostAddress> getHostsAddresses() {
		if(hostsAddresses == null){
			hostsAddresses = initializeHostsAddresses();
		}
		return hostsAddresses;
	}
	
	public List<String> getHostsUrls() {
		if(hostsUrls == null){
			hostsUrls = initializeHostsUrls();
		}
		return hostsUrls;
	}

	public Integer getBulkActions() {
		return bulkActions;
	}

	public Long getFlushInterval() {
		return flushInterval;
	}

	public Integer getConcurrentRequests() {
		return concurrentRequests;
	}

	public String getIndexName() {
		return indexName;
	}

	private List<HostAddress> initializeHostsAddresses(){
		String key = String.format("reporter.elastic.hosts[%s]", 0);
		List<HostAddress> res = new ArrayList<>();
		
		while (environment.containsProperty(key)) {
			String serializedHost = environment.getProperty(key);
			
			if (serializedHost.contains(PORT_SEPARATOR)) {
				String[] hostParts = serializedHost.split(PORT_SEPARATOR);
				
				String hostname = hostParts[0].toLowerCase();
				Integer port = Integer.parseInt(hostParts[1].trim());
				
				res.add(new HostAddress(hostname, port));
			} else {
				res.add(new HostAddress(serializedHost.trim(), protocol.getDefaultPort()));
			}
			
			key = String.format("reporter.elastic.hosts[%s]", res.size());
		}
		
		// Use default host if required
		if(res.isEmpty()){
			res.add(new HostAddress("localhost", protocol.getDefaultPort()));
		}
		return res;
	}
	
	public List<String> initializeHostsUrls() {
		String key = String.format("reporter.elastic.hosts[%s]", 0);
		List<String> res = new ArrayList<>();
		
		while (environment.containsProperty(key)) {
			res.add(environment.getProperty(key));
			key = String.format("reporter.elastic.hosts[%s]", res.size());
		}
		
		// Use default host if required
		if(res.isEmpty()){
			res.add("http://localhost:9200/");
		}
		return res;
	}
}

@@ -1120,16 +1120,94 @@
 .com)%0A *
+ @author David BRASSELY (brasseld at gmail.com)%0A * @author GraviteeSource Team
 %0A */%0Apub
@@ -1561,21 +1561,16 @@
 .cluster
-.name
 :elastic
@@ -1682,13 +1682,8 @@
 ndex
-.name
 :gra
@@ -2194,43 +2194,8 @@
 ses;
-%0A%09%0A%09private List%3CString%3E hostsUrls;
 %0A%0A%09p
@@ -2469,138 +2469,8 @@
 ;%0A%09%7D
-%0A%09%0A%09public List%3CString%3E getHostsUrls() %7B%0A%09%09if(hostsUrls == null)%7B%0A%09%09%09hostsUrls = initializeHostsUrls();%0A%09%09%7D%0A%09%09return hostsUrls;%0A%09%7D
 %0A%0A%09p
@@ -2806,32 +2806,33 @@
 format(%22reporter
+s
 .elastic.hosts%5B%25
@@ -3397,32 +3397,33 @@
 format(%22reporter
+s
 .elastic.hosts%5B%25
@@ -3596,434 +3596,6 @@
 %0A%09%7D%0A
-%09%0A%09public List%3CString%3E initializeHostsUrls() %7B%0A%09%09String key = String.format(%22reporter.elastic.hosts%5B%25s%5D%22, 0);%0A%09%09List%3CString%3E res = new ArrayList%3C%3E();%0A%09%09%0A%09%09while (environment.containsProperty(key)) %7B%0A%09%09%09res.add(environment.getProperty(key));%0A%09%09%09key = String.format(%22reporter.elastic.hosts%5B%25s%5D%22, res.size());%0A%09%09%7D%0A%09%09%0A%09%09// Use default host if required%0A%09%09if(res.isEmpty())%7B%0A%09%09%09res.add(%22http://localhost:9200/%22);%0A%09%09%7D%0A%09%09return res;%0A%09%7D%0A
 %7D%0A

Read configuration properties prefixed with reporters.elastic