package eu.eudat.models.rda.mapper;

import com.fasterxml.jackson.databind.JsonNode;
import eu.eudat.logic.utilities.helpers.MyStringUtils;
import eu.eudat.models.rda.TechnicalResource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;
import java.util.stream.Collectors;

public class TechnicalResourceRDAMapper {
	private static final Logger logger = LoggerFactory.getLogger(TechnicalResourceRDAMapper.class);

	public static List<TechnicalResource> toRDAList(List<JsonNode> nodes) {
		Map<String, TechnicalResource> rdaMap = new HashMap<>();

		for (JsonNode node: nodes) {
			String rdaProperty = node.get("rdaProperty").asText();
			String rdaValue = node.get("value").asText();
			if(rdaValue == null || rdaValue.isEmpty()){
				continue;
			}
			TechnicalResource rda = getRelative(rdaMap, node.get("numbering").asText());
			if (!rdaMap.containsValue(rda)) {
				rdaMap.put(node.get("numbering").asText(), rda);
			}
			for (ExportPropertyName exportPropertyName : ExportPropertyName.values()) {
				if (rdaProperty.contains(exportPropertyName.getName())) {
					switch (exportPropertyName) {
						case NAME:
							rda.setName(rdaValue);
							rda.setAdditionalProperty(ImportPropertyName.NAME.getName(), node.get("id").asText());
							break;
						case DESCRIPTION:
							rda.setDescription(rdaValue);
							rda.setAdditionalProperty(ImportPropertyName.DESCRIPTION.getName(), node.get("id").asText());
							break;
					}
				}
			}
		}

		return rdaMap.values().stream()
				.filter(tr -> tr.getName() != null)
				.collect(Collectors.toList());
	}

	public static Map<String, String> toProperties(List<TechnicalResource> rdas) {
		Map<String, String> properties = new HashMap<>();

		rdas.forEach(rda -> rda.getAdditionalProperties().entrySet().forEach(entry -> {
			try {
				ImportPropertyName importPropertyName = ImportPropertyName.fromString(entry.getKey());
				switch(importPropertyName) {
					case DESCRIPTION:
						properties.put(entry.getValue().toString(), rda.getDescription());
						break;
					case NAME:
						properties.put(entry.getValue().toString(), rda.getName());
						break;
				}
			} catch (Exception e) {
				logger.error(e.getMessage(), e);
			}
		}));

		return properties;
	}

	public static TechnicalResource toRDA(JsonNode node) {
		TechnicalResource rda = new TechnicalResource();
		String rdaProperty = node.get("rdaProperty").asText();
		String value = node.get("value").asText();

		if (rdaProperty.contains("description")) {
			rda.setDescription(value);
		}
		if (rdaProperty.contains("name")) {
			rda.setName(value);
		}
		
		if (rda.getName() == null) {
			throw new IllegalArgumentException("Technical Resources Name is missing");
		}

		return rda;
	}

	private static TechnicalResource getRelative(Map<String, TechnicalResource> rdaMap, String numbering) {
		return rdaMap.entrySet().stream().filter(entry -> MyStringUtils.getFirstDifference(entry.getKey(), numbering) > 0)
					 .max(Comparator.comparingInt(entry -> MyStringUtils.getFirstDifference(entry.getKey(), numbering))).map(Map.Entry::getValue).orElse(new TechnicalResource());
	}

	private enum ExportPropertyName {
		NAME("name"),
		DESCRIPTION("description");

		private String name;

		ExportPropertyName(String name) {
			this.name = name;
		}

		public String getName() {
			return name;
		}
	}

	private enum ImportPropertyName {
		NAME("nameId"),
		DESCRIPTION("descriptionId");

		private String name;

		ImportPropertyName(String name) {
			this.name = name;
		}

		public String getName() {
			return name;
		}

		public static ImportPropertyName fromString(String name) throws Exception {
			for (ImportPropertyName importPropertyName: ImportPropertyName.values()) {
				if (importPropertyName.getName().equals(name)) {
					return importPropertyName;
				}
			}
			throw new Exception("Property name not available");
		}
	}
}
