package com.gentics.mesh.dagger.module;

import java.net.MalformedURLException;
import java.net.URL;

import javax.annotation.Nullable;
import javax.inject.Singleton;

import com.gentics.elasticsearch.client.ElasticsearchClient;
import com.gentics.elasticsearch.client.okhttp.ElasticsearchOkClient;
import com.gentics.elasticsearch.client.okhttp.ElasticsearchOkClient.Builder;
import com.gentics.mesh.dagger.SearchProviderType;
import com.gentics.mesh.etc.config.MeshOptions;
import com.gentics.mesh.etc.config.search.ElasticSearchOptions;
import com.gentics.mesh.search.DevNullSearchProvider;
import com.gentics.mesh.search.SearchProvider;
import com.gentics.mesh.search.TrackingSearchProvider;
import com.gentics.mesh.search.impl.ElasticSearchProvider;

import dagger.Module;
import dagger.Provides;
import io.vertx.core.json.JsonObject;

@Module
public class SearchProviderModule {

	@Provides
	@Singleton
	public static SearchProvider searchProvider(@Nullable SearchProviderType type, MeshOptions options, ElasticSearchProvider elasticsearchProvider) {
		if (type == null) {
			if (options.getSearchOptions().getUrl() == null) {
				return new DevNullSearchProvider();
			} else {
				return elasticsearchProvider;
			}
		}
		switch (type) {
		case NULL:
			return new DevNullSearchProvider();
		case TRACKING:
			return new TrackingSearchProvider();
		case ELASTICSEARCH:
		default:
			return elasticsearchProvider;
		}
	}

	@Provides
	public static ElasticsearchClient<JsonObject> searchClient(MeshOptions options) {

		ElasticSearchOptions searchOptions = options.getSearchOptions();
		URL url;
		try {
			url = new URL(searchOptions.getUrl());
		} catch (MalformedURLException e) {
			throw new RuntimeException("Invalid search provider url", e);
		}
		int port = url.getPort();
		String proto = url.getProtocol();
		if ("http".equals(proto) && port == -1) {
			port = 80;
		}
		if ("https".equals(proto) && port == -1) {
			port = 443;
		}

		Builder<JsonObject> builder = new ElasticsearchOkClient.Builder<JsonObject>()
			.setScheme(proto)
			.setHostname(url.getHost())
			.setPort(port)
			.setConverterFunction(JsonObject::new);

		String username = searchOptions.getUsername();
		String password = searchOptions.getPassword();
		builder.setLogin(username, password);
		builder.setCaPath(searchOptions.getCaPath());
		builder.setCertPath(searchOptions.getCertPath());
		builder.setVerifyHostnames(searchOptions.isHostnameVerification());

		return builder.build();
	}

}

@@ -749,16 +749,36 @@
 vider;%0A%0A
+import dagger.Lazy;%0A
 import d
@@ -787,24 +787,24 @@
 ger.Module;%0A
-
 import dagge
@@ -1023,16 +1023,21 @@
 ptions, 
+Lazy%3C
 ElasticS
@@ -1049,16 +1049,17 @@
 Provider
+%3E
  elastic
@@ -1232,24 +1232,30 @@
 archProvider
+.get()
 ;%0A%09%09%09%7D%0A%09%09%7D%0A%09
@@ -1444,16 +1444,22 @@
 Provider
+.get()
 ;%0A%09%09%7D%0A%09%7D
@@ -1471,16 +1471,28 @@
 rovides%0A
+%09@Singleton%0A
 %09public 
@@ -1733,16 +1733,16 @@
 on e) %7B%0A
-
 %09%09%09throw
@@ -1791,16 +1791,49 @@
 ider url
+ %7B%22 + searchOptions.getUrl() + %22%7D
 %22, e);%0A%09

Fix demo data generation