package eu.dnetlib.data.resultSet.cleaner;

import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.Namespace;
import org.dom4j.QName;
import org.dom4j.io.SAXReader;

import com.google.common.base.Function;
import com.google.common.collect.Lists;

public class Cleaner implements Function<String, String> {

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

	private List<XPathCleaningRule> xpathRules = Lists.newArrayList();

	public Cleaner(final List<XPathCleaningRule> xpathRules) {
		this.xpathRules = xpathRules;
	}

	@Override
	public String apply(final String text) {

		try {
			final List<Map<String, String>> errors = new ArrayList<Map<String, String>>();
			final Document doc = new SAXReader().read(new StringReader(text));
			for (final XPathCleaningRule r : xpathRules) {
				errors.addAll(r.applyXpathRule(doc));
			}
			if (errors.size() > 0) {
				markAsInvalid(doc, errors);
			}
			return doc.asXML();
		} catch (final Exception e) {
			log.error("Error evaluating rule", e);
		}
		return "";
	}

	private void markAsInvalid(final Document doc, final List<Map<String, String>> errors) {
		final Element element = (Element) doc.selectSingleNode("//*[local-name()='header']");
		if (element != null) {
			final Element inv = element.addElement(new QName("invalid", new Namespace("dri", "http://www.driver-repository.eu/namespace/dri")));
			for (final Map<String, String> e : errors) {
				final Element err = inv.addElement(new QName("error", new Namespace("dri", "http://www.driver-repository.eu/namespace/dri")));
				for (final Map.Entry<String, String> entry : e.entrySet()) {
					err.addAttribute(entry.getKey(), entry.getValue());
				}
			}
			inv.addAttribute("value", "true");
		}
	}
}
