package org.nanopub.extra.aida;

import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLDecoder;

import org.nanopub.Nanopub;
import org.nanopub.NanopubPattern;
import org.openrdf.model.Statement;
import org.openrdf.model.URI;
import org.openrdf.model.impl.URIImpl;

public class AidaPattern implements NanopubPattern {

	private static final long serialVersionUID = -164193908921728662L;

	@Override
	public String getName() {
		return "AIDA nanopublication";
	}

	@Override
	public boolean appliesTo(Nanopub nanopub) {
		for (Statement st : nanopub.getAssertion()) {
			if (st.getPredicate().equals(AS_SENTENCE)) return true;
		}
		return false;
	}

	@Override
	public boolean isCorrectlyUsedBy(Nanopub nanopub) {
		URI aidaUri = getAidaUri(nanopub);
		return aidaUri != null && aidaUri.toString().startsWith("http://purl.org/aida/");
	}

	@Override
	public String getDescriptionFor(Nanopub nanopub) {
		if (isCorrectlyUsedBy(nanopub)) {
			URI aidaUri = getAidaUri(nanopub);
			String aidaSentence = aidaUri.toString().substring("http://purl.org/aida/".length());
			try {
				aidaSentence = URLDecoder.decode(aidaSentence, "UTF-8");
			} catch (UnsupportedEncodingException ex) {
				ex.printStackTrace();
			}
			return "AIDA sentence: " + aidaSentence;
		} else {
			return "Not a valid AIDA nanopublication";
		}
	}

	@Override
	public URL getPatternInfoUrl() throws MalformedURLException {
		return new URL("https://github.com/tkuhn/aida");
	}

	public static URI getAidaUri(Nanopub nanopub) {
		URI aidaUri = null;
		boolean error = false;
		for (Statement st : nanopub.getAssertion()) {
			if (!st.getSubject().equals(nanopub.getAssertionUri())) continue;
			if (!st.getPredicate().equals(AS_SENTENCE)) continue;
			if (!(st.getObject() instanceof URI)) {
				error = true;
				break;
			}
			if (aidaUri != null) {
				error = true;
				break;
			}
			aidaUri = (URI) st.getObject();
		}
		if (error) return null;
		return aidaUri;
	}

	public static final URI AS_SENTENCE = new URIImpl("http://purl.org/nanopub/x/asSentence");

}

@@ -873,39 +873,31 @@
 rtsWith(
-%22http://purl.org/aida/%22
+AIDA_URI_PREFIX
 );%0A%09%7D%0A%0A%09
@@ -1001,322 +1001,67 @@
 %0A%09%09%09
-URI aidaUri = getAidaUri(nanopub);%0A%09%09%09String aidaSentence = aidaUri.toString().substring(%22http://purl.org/aida/%22.length());%0A%09%09%09try %7B%0A%09%09%09%09aidaSentence = URLDecoder.decode(aidaSentence, %22UTF-8%22);%0A%09%09%09%7D catch (UnsupportedEncodingException ex) %7B%0A%09%09%09%09ex.printStackTrace();%0A%09%09%09%7D%0A%09%09%09return %22AIDA sentence: %22 + aidaSentence
+return %22AIDA sentence: %22 + getAidaText(getAidaUri(nanopub))
 ;%0A%09%09
@@ -1742,24 +1742,498 @@
 idaUri;%0A%09%7D%0A%0A
+%09public static String getAidaText(URI aidaUri) %7B%0A%09%09if (aidaUri == null) return null;%0A%09%09if (!aidaUri.toString().startsWith(AIDA_URI_PREFIX)) return null;%0A%09%09String aidaSentence = aidaUri.toString().substring(AIDA_URI_PREFIX.length());%0A%09%09try %7B%0A%09%09%09aidaSentence = URLDecoder.decode(aidaSentence, %22UTF-8%22);%0A%09%09%7D catch (UnsupportedEncodingException ex) %7B%0A%09%09%09ex.printStackTrace();%0A%09%09%7D%0A%09%09return aidaSentence;%0A%09%7D%0A%0A%09public static final String AIDA_URI_PREFIX = %22http://purl.org/aida/%22;%0A
 %09public stat

Refactor code for AIDA sentences