package eu.dnetlib.services.hcm;

import java.io.IOException;
import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.UUID;
import java.util.stream.Collectors;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.maven.model.Model;
import org.apache.maven.model.Parent;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.AbstractEnvironment;
import org.springframework.core.env.Environment;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.PropertySource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.io.support.ResourcePatternUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.google.common.collect.Maps;

import eu.dnetlib.conf.DnetGenericApplicationProperties;
import eu.dnetlib.enabling.annotations.DnetService;
import eu.dnetlib.enabling.annotations.DnetServiceType;
import eu.dnetlib.miscutils.datetime.DateUtils;
import eu.dnetlib.miscutils.datetime.HumanTime;
import eu.dnetlib.miscutils.streams.DnetStreamSupport;
import eu.dnetlib.services.BaseService;

@RestController
@RequestMapping(value = "/hcm", method = RequestMethod.GET)
@DnetService(DnetServiceType.hcm)
public class HCM extends BaseService {

	private static final Log log = LogFactory.getLog(HCM.class);

	@Autowired
	private Environment env;

	@Autowired
	private DnetGenericApplicationProperties containerConfiguration;

	@Autowired
	private ResourceLoader resourceLoader;

	@RequestMapping(value = "properties", method = RequestMethod.GET)
	public Map<String, Object> listProperties() {

		final Iterator<PropertySource<?>> iter = ((AbstractEnvironment) env).getPropertySources().iterator();
		return DnetStreamSupport.stream(iter)
				.filter(ps -> ps instanceof MapPropertySource)
				.map(ps -> (MapPropertySource) ps)
				.collect(Collectors.toMap(MapPropertySource::getName, MapPropertySource::getSource));

	}

	@RequestMapping(value = "info", method = RequestMethod.GET)
	public Map<String, Object> info() {
		final RuntimeMXBean mxbean = ManagementFactory.getRuntimeMXBean();
		final Map<String, Object> genInfo = Maps.newLinkedHashMap();
		genInfo.put("Hostname", containerConfiguration.getHost());
		genInfo.put("Port", containerConfiguration.getPort());
		genInfo.put("Uptime", HumanTime.exactly(mxbean.getUptime()));
		genInfo.put("Start Time", DateUtils.calculate_ISO8601(mxbean.getStartTime()));
		return genInfo;
	}

	@RequestMapping(value = "jvm", method = RequestMethod.GET)
	public Map<String, String> jvmInfo() {
		final RuntimeMXBean mxbean = ManagementFactory.getRuntimeMXBean();
		final Map<String, String> jvmInfo = Maps.newLinkedHashMap();
		jvmInfo.put("JVM Name", mxbean.getVmName());
		jvmInfo.put("JVM Vendor", mxbean.getVmVendor());
		jvmInfo.put("JVM Version", mxbean.getVmVersion());
		jvmInfo.put("JVM Spec Name", mxbean.getSpecName());
		jvmInfo.put("JVM Spec Vendor", mxbean.getSpecVendor());
		jvmInfo.put("JVM Spec Version", mxbean.getSpecVersion());
		jvmInfo.put("Running JVM Name", mxbean.getName());
		jvmInfo.put("Management Spec Version", mxbean.getManagementSpecVersion());
		return jvmInfo;
	}

	@RequestMapping(value = "libs", method = RequestMethod.GET)
	public Map<String, String> libInfo() {
		final RuntimeMXBean mxbean = ManagementFactory.getRuntimeMXBean();
		final Map<String, String> libInfo = Maps.newLinkedHashMap();
		libInfo.put("Classpath", mxbean.getClassPath().replaceAll(":", " : "));
		libInfo.put("Boot ClassPath", mxbean.getBootClassPath().replaceAll(":", " : "));
		libInfo.put("Input arguments", mxbean.getInputArguments().toString());
		libInfo.put("Library Path", mxbean.getLibraryPath().replaceAll(":", " : "));
		return libInfo;
	}

	@RequestMapping(value = "maven", method = RequestMethod.GET)
	private Map<String, Map<String, Map<String, List<String>>>> mavenModules() throws IOException {
		final Map<String, Map<String, Map<String, List<String>>>> modules = new TreeMap<>();

		final MavenXpp3Reader reader = new MavenXpp3Reader();
		for (final Resource res : ResourcePatternUtils.getResourcePatternResolver(resourceLoader).getResources("classpath*:/META-INF/**/pom.xml")) {
			try {
				final Model model = reader.read(res.getInputStream());

				final String name = model.getArtifactId();

				String groupId = model.getGroupId();
				for (Parent parent = model.getParent(); (groupId == null) && (model.getParent() != null); parent = model.getParent()) {
					groupId = parent.getGroupId();
				}

				String version = model.getVersion();
				for (Parent parent = model.getParent(); (version == null) && (model.getParent() != null); parent = model.getParent()) {
					version = parent.getVersion();
				}

				if (!modules.containsKey(groupId)) {
					modules.put(groupId, new TreeMap<>());
				}
				if (!modules.get(groupId).containsKey(name)) {
					modules.get(groupId).put(name, new TreeMap<>());
				}
				if (!modules.get(groupId).get(name).containsKey(version)) {
					modules.get(groupId).get(name).put(version, new ArrayList<>());
				}

				modules.get(groupId).get(name).get(version).add(res.getURI().toString());
			} catch (final Exception e) {
				log.warn("Error evaluating pom: " + res.getURI());
				log.debug("-- ERROR --", e);
			}
		}

		return modules;
	}

	@Override
	public List<Element> geXmlProfileSections() {
		final Element elem = DocumentHelper.createElement("HCM_SESSION_ID");
		elem.addAttribute("value", "session-" + UUID.randomUUID());
		return Arrays.asList(elem);
	}

}
