package de.fred4jupiter.fredbet.web.admin;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.util.Locale;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.TimeUnit;

import javax.annotation.PostConstruct;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.endpoint.MetricsEndpoint;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/buildinfo")
public class BuildInfoController {

	private static final Logger LOG = LoggerFactory.getLogger(BuildInfoController.class);

	@Autowired
	private Properties buildProperties;

	@Autowired
	private MetricsEndpoint metricsEndpoint;

	@PostConstruct
	private void postProcessBuildProperties() {
		String buildTimestamp = buildProperties.getProperty("build.timestamp");
		buildTimestamp = buildTimestamp + " +0000";

		ZonedDateTime parsed = ZonedDateTime.parse(buildTimestamp, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm Z"));
		String formattedDateTime = parsed.format(DateTimeFormatter.ofPattern("dd.MM.yyyy HH:mm zZ", Locale.getDefault())
				.withZone(ZoneId.of("Europe/Berlin")));
		buildProperties.put("build.timestamp", formattedDateTime);
	}

	@RequestMapping
	public ModelAndView list() {
		ModelAndView modelAndView = new ModelAndView("buildinfo", "buildInfoMap", buildProperties);
		addDynamicInfoProperties();
		return modelAndView;
	}

	private void addDynamicInfoProperties() {
		addCurrentDateTime();
		addHostName();
		addMetrics();
	}

	private void addMetrics() {
		Map<String, Object> metricsMap = metricsEndpoint.invoke();

		buildProperties.put("system.uptime", formatUptime((Long) metricsMap.get("uptime")));
		buildProperties.put("application.context.uptime", formatUptime((Long) metricsMap.get("instance.uptime")));
	}

	private String formatUptime(Long millis) {
		return String.format("%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(millis),
				TimeUnit.MILLISECONDS.toMinutes(millis), TimeUnit.MILLISECONDS.toSeconds(millis));
	}

	private void addHostName() {
		try {
			InetAddress localHost = InetAddress.getLocalHost();
			String hostName = localHost.getHostName();
			buildProperties.put("hostName", hostName);
		} catch (UnknownHostException e) {
			LOG.error(e.getMessage(), e);
		}
	}

	private void addCurrentDateTime() {
		DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM, FormatStyle.MEDIUM);
		DateTimeFormatter timeZoneFormatter = DateTimeFormatter.ofPattern("VV x");

		final String key = "currentDateTime";
		final String value = formatter.format(LocalDateTime.now()) + ", "
				+ timeZoneFormatter.format(ZonedDateTime.now());

		buildProperties.put(key, value);
	}
}

@@ -278,24 +278,50 @@
 rmatStyle;%0D%0A
+import java.util.Arrays;%0D%0A
 import java.
@@ -660,16 +660,66 @@
 point;%0D%0A
+import org.springframework.core.env.Environment;%0D%0A
 import o
@@ -1164,16 +1164,66 @@
 int;%0D%0A%0D%0A
+%09@Autowired%0D%0A%09private Environment environment;%0D%0A%0D%0A
 %09@PostCo
@@ -1277,16 +1277,312 @@
 es() %7B%0D%0A
+%09%09addBuildTimestamp();%0D%0A%09%09addSpringProfiles();%0D%0A%09%7D%0D%0A%0D%0A%09@RequestMapping%0D%0A%09public ModelAndView list() %7B%0D%0A%09%09ModelAndView modelAndView = new ModelAndView(%22buildinfo%22, %22buildInfoMap%22, buildProperties);%0D%0A%09%09addDynamicInfoProperties();%0D%0A%09%09return modelAndView;%0D%0A%09%7D%0D%0A%0D%0A%09private void addBuildTimestamp() %7B%0D%0A
 %09%09String
@@ -1695,18 +1695,16 @@
 0000%22;%0D%0A
-%0D%0A
 %09%09ZonedD
@@ -2043,137 +2043,35 @@
 %0A%0D%0A%09
-@RequestMapping%0D%0A%09public ModelAndView list() %7B%0D%0A%09%09ModelAndView modelAndView = new ModelAndView(%22buildinfo%22, %22buildInfoMap%22, build
+private void addDynamicInfo
 Prop
@@ -2080,173 +2080,279 @@
 ties
-);
+() %7B
 %0D%0A%09%09add
-DynamicInfoProperties();%0D%0A%09%09return modelAndView;%0D%0A%09%7D%0D%0A%0D%0A%09private void addDynamicInfoProperties() %7B%0D%0A%09%09addCurrentDateTime();%0D%0A%09%09addHostName();%0D%0A%09%09addMetrics(
+CurrentDateTime();%0D%0A%09%09addHostName();%0D%0A%09%09addMetrics();%0D%0A%09%7D%0D%0A%0D%0A%09private void addSpringProfiles() %7B%0D%0A%09%09String%5B%5D activeProfiles = environment.getActiveProfiles();%0D%0A%09%09buildProperties.put(%22Active Profiles%22, activeProfiles != null ? Arrays.asList(activeProfiles) : %22%22
 );%0D%0A
@@ -2733,21 +2733,24 @@
 02d:%2502d
-:%2502d
+ %5BHH:mm%5D
 %22, TimeU
@@ -2831,49 +2831,8 @@
 lis)
-, TimeUnit.MILLISECONDS.toSeconds(millis)
 );%0D%0A

reformat data in build controller; added active profiles