package cloudade.server.auth.mongo.info;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.management.ManagementFactory;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.List;

import org.joda.time.LocalDate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.Banner;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import cloudade.server.auth.mongo.info.domain.ServerInfo;
import cloudade.server.auth.mongo.info.repositories.ServerInfoRepository;

@Component
public class ServerInfoService {

	private ServerInfoRepository serverInfoRepository;

	@Autowired
	private ApplicationContext context;

	@Autowired
	public ServerInfoService(final ServerInfoRepository serverInfoRepository) {
		this.serverInfoRepository = serverInfoRepository;
	}

	@Value("${server.port}")
	private int port;
	
	@Value("${spring.application.name}")
	private String appName;

	public ServerInfo saveServerInfo() throws UnknownHostException{
		return saveServerInfo(getServerInfo());
	}
	
	public ServerInfo saveServerInfo(ServerInfo serverInfo) throws UnknownHostException{

		String jvmName = ManagementFactory.getRuntimeMXBean().getName();
		String host;
		try {
			host = InetAddress.getLocalHost().getHostName();
		} catch (UnknownHostException e) {
			try {
				Process p  = Runtime.getRuntime().exec("hostname");
				BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
			    String line = null;
			    StringBuffer sBuf = new StringBuffer();
			    while((line = br.readLine()) != null){
			    	sBuf.append(line);
			    	System.out.println(sBuf.toString());
			    }
			    host = sBuf.toString();
			} catch (IOException e1) {
				host = InetAddress.getLocalHost().toString();
			}
		}
		
		String springBootVersion = Banner.class.getPackage().getImplementationVersion();
		String appVersion = getClass().getPackage().getImplementationVersion();
		
		if (serverInfo != null) {
			serverInfo.setSpringBootVersion(springBootVersion);
			serverInfo.setAppVersion(appVersion);
			serverInfo.setPid(jvmName.split("@")[0]);
			serverInfo.setLastStartedAt( new LocalDate() );
			
			serverInfoRepository.save(serverInfo);
			return serverInfo;
		} else {
			serverInfo = new ServerInfo(null, host, port + "",
					jvmName.split("@")[0], appName, springBootVersion, appVersion, new LocalDate(), new LocalDate());
			serverInfoRepository.save(serverInfo);
			return serverInfo;
		}

	}

	public ServerInfo getServerInfo() throws UnknownHostException {
		return serverInfoRepository.findByUniqueKeys(InetAddress.getLocalHost().getHostName(), "" + port);

	}
	
	public List<ServerInfo> getServerInfoByAppName() {
		 return serverInfoRepository.findByAppName(appName);
	}

}

@@ -1036,17 +1036,16 @@
 t port;%0A
-%09
 %0A%09@Value
@@ -1140,35 +1140,26 @@
 fo() throws 
-UnknownHost
+IO
 Exception%7B%0A%09
@@ -1198,25 +1198,24 @@
 Info());%0A%09%7D%0A
-%09
 %0A%09public Ser
@@ -1263,35 +1263,26 @@
 nfo) throws 
-UnknownHost
+IO
 Exception%7B%0A%0A
@@ -1365,582 +1365,26 @@
 host
-;%0A%09%09try %7B%0A%09%09%09host = InetAddress.getLocalHost().getHostName();%0A%09%09%7D catch (UnknownHostException e) %7B%0A%09%09%09try %7B%0A%09%09%09%09Process p  = Runtime.getRuntime().exec(%22hostname%22);%0A%09%09%09%09BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));%0A%09%09%09    String line = null;%0A%09%09%09    StringBuffer sBuf = new StringBuffer();%0A%09%09%09    while((line = br.readLine()) != null)%7B%0A%09%09%09    %09sBuf.append(line);%0A%09%09%09    %09System.out.println(sBuf.toString());%0A%09%09%09    %7D%0A%09%09%09    host = sBuf.toString();%0A%09%09%09%7D catch (IOException e1) %7B%0A%09%09%09%09host = InetAddress.getLocalHost().toString();%0A%09%09%09%7D%0A%09%09%7D%0A%09%09
+ = getHostname();%0A
 %0A%09%09S
@@ -1537,18 +1537,16 @@
 sion();%0A
-%09%09
 %0A%09%09if (s
@@ -1758,19 +1758,16 @@
 te() );%0A
-%09%09%09
 %0A%09%09%09serv
@@ -2106,27 +2106,18 @@
  throws 
-UnknownHost
+IO
 Exceptio
@@ -2171,43 +2171,16 @@
 eys(
-InetAddress.getLocalHost().getHostN
+getHostn
 ame(
@@ -2198,17 +2198,16 @@
 t);%0A%0A%09%7D%0A
-%09
 %0A%09public
@@ -2253,17 +2253,16 @@
 e() %7B%0A%09%09
- 
 return s
@@ -2309,10 +2309,544 @@
 e);%0A%09%7D%0A%0A
+%09private String getHostname() throws IOException %7B%0A%09%09String host;%0A%09%09try %7B%0A%09%09%09host = InetAddress.getLocalHost().getHostName();%0A%09%09%7D catch (UnknownHostException e) %7B%0A%0A%09%09%09Process p  = Runtime.getRuntime().exec(%22hostname%22);%0A%09%09%09BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));%0A%09%09%09String line = null;%0A%09%09%09StringBuffer sBuf = new StringBuffer();%0A%09%09%09while((line = br.readLine()) != null)%7B%0A%09%09%09%09sBuf.append(line);%0A%09%09%09%09System.out.println(sBuf.toString());%0A%09%09%09%7D%0A%09%09%09host = sBuf.toString();%0A%0A%09%09%7D%0A%0A%09%09return host;%0A%09%7D%0A%0A
 %7D%0A

fix bug again