package org.petapico.npop.fingerprint;

import static org.nanopub.SimpleTimestampPattern.isCreationTimeProperty;

import java.util.ArrayList;
import java.util.List;

import net.trustyuri.TrustyUriUtils;
import net.trustyuri.rdf.RdfHasher;
import net.trustyuri.rdf.RdfPreprocessor;

import org.nanopub.Nanopub;
import org.nanopub.NanopubUtils;
import org.openrdf.model.Resource;
import org.openrdf.model.Statement;
import org.openrdf.model.URI;
import org.openrdf.model.Value;
import org.openrdf.model.impl.ContextStatementImpl;
import org.openrdf.model.impl.URIImpl;

public class WikipathwaysFingerprints implements FingerprintHandler {

	@Override
	public String getFingerprint(Nanopub np) {
		String artifactCode = TrustyUriUtils.getArtifactCode(np.getUri().toString());
		if (artifactCode == null) {
			throw new RuntimeException("Not a trusty URI: " + np.getUri());
		}
		List<Statement> statements = getNormalizedStatements(np);
		statements = RdfPreprocessor.run(statements, artifactCode);
		String fingerprint = RdfHasher.makeArtifactCode(statements);
		return fingerprint.substring(2);
	}

	private List<Statement> getNormalizedStatements(Nanopub np) {
		List<Statement> statements = NanopubUtils.getStatements(np);
		List<Statement> n = new ArrayList<>();
		for (Statement st : statements) {
			boolean isInAssertion = st.getContext().equals(np.getAssertionUri());
			boolean isInProvenance = st.getContext().equals(np.getProvenanceUri());
			boolean isInPubinfo = st.getContext().equals(np.getPubinfoUri());
			if (!isInProvenance && !isInAssertion && !isInPubinfo) continue;
			URI graphURI;
			if (isInAssertion) {
				graphURI = assertionUriPlaceholder;
			} else if (isInProvenance) {
				graphURI = provUriPlaceholder;
			} else {
				graphURI = pubinfoUriPlaceholder;
			}
			Resource subj = st.getSubject();
			URI pred = st.getPredicate();
			Value obj = st.getObject();
			if (isInPubinfo && subj.equals(np.getUri()) && isCreationTimeProperty(pred)) {
				continue;
			}
			n.add(new ContextStatementImpl((Resource) transform(subj), (URI) transform(pred), transform(obj), graphURI));
		}
		return n;
	}

	private Value transform(Value v) {
		if (v instanceof URI) {
			String s = ((URI) v).stringValue();
			if (s.startsWith("http://www.wikipathways.org/instance/WP")) {
				s.replaceFirst("^(http://www.wikipathways.org/instance/WP[0-9]*)_r[0-9]*$", "$1");
				System.err.println(s);
				return new URIImpl(s);
			}
		}
		return v;
	}

}

@@ -524,47 +524,8 @@
 mpl;
-%0Aimport org.openrdf.model.impl.URIImpl;
 %0A%0Apu
@@ -1983,73 +1983,23 @@
 mpl(
-(Resource) transform(
 subj
-)
 , 
-(URI) transform(
 pred
-)
 , 
-transform(
 obj
-)
 , gr
@@ -2032,339 +2032,6 @@
 %09%7D%0A%0A
-%09private Value transform(Value v) %7B%0A%09%09if (v instanceof URI) %7B%0A%09%09%09String s = ((URI) v).stringValue();%0A%09%09%09if (s.startsWith(%22http://www.wikipathways.org/instance/WP%22)) %7B%0A%09%09%09%09s.replaceFirst(%22%5E(http://www.wikipathways.org/instance/WP%5B0-9%5D*)_r%5B0-9%5D*$%22, %22$1%22);%0A%09%09%09%09System.err.println(s);%0A%09%09%09%09return new URIImpl(s);%0A%09%09%09%7D%0A%09%09%7D%0A%09%09return v;%0A%09%7D%0A%0A
 %7D%0A

Remove code that had no effect in WikiPathways fingerprinting