package roart.util;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import com.netflix.appinfo.InstanceInfo;
import com.netflix.appinfo.MyDataCenterInstanceConfig;
import com.netflix.discovery.DefaultEurekaClientConfig;
import com.netflix.discovery.DiscoveryClient;
import com.netflix.discovery.DiscoveryManager;
import com.netflix.discovery.EurekaClient;
import com.netflix.discovery.shared.Application;

public class EurekaUtil {
    
	private static Logger log = LoggerFactory.getLogger(EurekaUtil.class);

    public static EurekaClient eurekaClient = null;
	public static DiscoveryClient discoveryClient = null;
	
	public static EurekaClient initEurekaClient() {
		DiscoveryManager.getInstance().initComponent(
				new MyDataCenterInstanceConfig(),
				new DefaultEurekaClientConfig());
		eurekaClient = DiscoveryManager.getInstance()
				.getEurekaClient();

		if (eurekaClient != null) {
			log.info("euClient " + eurekaClient.getAllKnownRegions());
			List<Application> apps = eurekaClient.getApplications().getRegisteredApplications();
			for (Application app : apps) {
				log.info("currently available app " + app.getName()); 
			}
		}
		discoveryClient = DiscoveryManager.getInstance().getDiscoveryClient();		
		if (discoveryClient != null) {
			List<Application> apps = discoveryClient.getApplications().getRegisteredApplications();
			for (Application app : apps) {
				log.info("currently available app2 " + app.getName()); 
			}
		}
		try {
			// more example code:
			/*
			String vipAddress = "LUCENE";
			InstanceInfo nextServerInfo = DiscoveryManager.getInstance()
					.getEurekaClient()
					.getNextServerFromEureka(vipAddress, false);
			log.info("Found an instance of example service to talk to from eureka: "
					+ nextServerInfo.getVIPAddress() + ":" + nextServerInfo.getPort());

			log.info("healthCheckUrl: " + nextServerInfo.getHealthCheckUrl());
			log.info("override: " + nextServerInfo.getOverriddenStatus());

			log.info("Server Host Name "+ nextServerInfo.getHostName() + " at port " + nextServerInfo.getPort() );		

			log.info("conf " + discoveryClient.getEurekaClientConfig().getEurekaServerPort());
			*/
	} catch (Exception e) {
			log.error("Cannot get an instance of example service to talk to from eureka");
		}
		return eurekaClient;
	}

	public static <T> T sendMe(Class<T> myclass, Object param, String appName, String path) {

		String homePageUrl = null;
		log.info("homePagePre " + appName + " " + path);
		log.info("clis" + eurekaClient + " " + discoveryClient);
		if (discoveryClient != null) {
			List<InstanceInfo> li = discoveryClient.getApplication(appName).getInstances();
			for (InstanceInfo ii : li) {
				log.info("homePage " + ii.getHomePageUrl());
			}
			List<InstanceInfo> li2 = eurekaClient.getApplication(appName).getInstances();
			for (InstanceInfo ii : li2) {
				log.info("homePage2 " + ii.getHomePageUrl());
			}
			if (!li.isEmpty()) {
				homePageUrl = li.get(0).getHomePageUrl();
			}
		}
		MultiValueMap<String, String> headers = new LinkedMultiValueMap<String, String>();
		Map map = new HashMap<String, String>();
		map.put("Content-Type", "application/json");

		headers.setAll(map);

		HttpEntity<?> request = new HttpEntity<>(param, headers);
		String url = homePageUrl;
		RestTemplate rt = new RestTemplate();
		/*
		for (HttpMessageConverter<?> converter : rt.getMessageConverters()) {
			log.info(converter.getClass().getName());
		}
		log.info(rt.getMessageConverters().size());
		 */
		ResponseEntity<T> regr = new RestTemplate().postForEntity(url + path, request, myclass);
		T result = regr.getBody();
		return result;
	}


}

@@ -3997,11 +3997,608 @@
 lt;%0A%09%7D%0A%0A
+        public static %3CT%3E T sendMe(Class%3CT%3E myclass, String url, Object param, String path) %7B%0A            MultiValueMap%3CString, String%3E headers = new LinkedMultiValueMap%3C%3E();%0A            Map map = new HashMap%3CString, String%3E();%0A            map.put(%22Content-Type%22, %22application/json%22);%0A%0A            headers.setAll(map);%0A%0A            HttpEntity%3C?%3E request = new HttpEntity%3C%3E(param, headers);%0A            RestTemplate rt = new RestTemplate();%0A        ResponseEntity%3CT%3E regr = new RestTemplate().postForEntity(url + path, request, myclass);%0A        T result = regr.getBody();%0A        return result;%0A%7D%0A
 %0A%7D%0A

Send to URL, not using Eureka.