package cloudade.server.auth.mongo.info;

import java.io.IOException;
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 {
				host = Runtime.getRuntime().exec("hostname").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);
	}

}

@@ -50,27 +50,92 @@
 java.io.
-IOException
+BufferedReader;%0Aimport java.io.IOException;%0Aimport java.io.InputStreamReader
 ;%0Aimport
@@ -1493,20 +1493,26 @@
 y %7B%0A%09%09%09%09
-host
+Process p 
  = Runti
@@ -1543,16 +1543,324 @@
 stname%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
 .toStrin

fix bug to call hostname