/*Copyright (c) 2015 "hbz"

This file is part of etikett.

etikett is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package helper;

import java.net.URL;
import java.text.Normalizer;
import java.util.Collection;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Properties;

import org.eclipse.rdf4j.model.BNode;
import org.eclipse.rdf4j.model.Literal;
import org.eclipse.rdf4j.model.Statement;
import org.eclipse.rdf4j.model.ValueFactory;
import org.eclipse.rdf4j.model.impl.SimpleValueFactory;
import org.eclipse.rdf4j.rio.RDFFormat;

/**
 * @author Jan Schnasse
 *
 */
@SuppressWarnings("javadoc")
public class GndLabelResolver {

    public static Properties turtleResourceProp = new Properties();
    public static Properties turtleObjectProp = new Properties();

    final public static String id = "http://d-nb.info/gnd/";
    final public static String id2 = "https://d-nb.info/gnd/";

    public GndLabelResolver() {
        turtleResourceProp.setProperty("protocol", "https://");
        turtleResourceProp.setProperty("alternateProtocol", "http://");
        turtleResourceProp.setProperty("domain", "d-nb.info/gnd/");

        turtleObjectProp.setProperty("namespace", "d-nb.info/standards/elementset/gnd#");
        turtleObjectProp.setProperty("preferredName", "preferredName");
        turtleObjectProp.setProperty("preferredNameForTheConferenceOrEvent", "preferredNameForTheConferenceOrEvent");
        turtleObjectProp.setProperty("preferredNameForTheCorporateBody", "preferredNameForTheCorporateBody");
        turtleObjectProp.setProperty("preferredNameForThePerson", "preferredNameForThePerson");
        turtleObjectProp.setProperty("preferredNameForThePlaceOrGeographicName",
                "preferredNameForThePlaceOrGeographicName");
        turtleObjectProp.setProperty("preferredNameForTheSubjectHeading", "preferredNameForTheSubjectHeading");
        turtleObjectProp.setProperty("preferredNameForTheWork", "preferredNameForTheWork");
    }

    /**
     * @param uri
     *            analyes data from the url to find a proper label
     * @return a label
     */
    public static String lookup(String uri, String language) {
        try {
            play.Logger.info("Lookup Label from GND. Language selection is not supported yet! " + uri);

            Collection<Statement> statement = RdfUtils.readRdfToGraph(new URL(uri + "/about/lds"), RDFFormat.RDFXML,
                    "application/rdf+xml");

            Iterator<Statement> sit = statement.iterator();

            while (sit.hasNext()) {
                Statement s = sit.next();
                boolean isLiteral = s.getObject() instanceof Literal;
                if (!(s.getSubject() instanceof BNode)) {
                    if (isLiteral) {
                        ValueFactory v = SimpleValueFactory.getInstance();
                        Statement newS = v.createStatement(s.getSubject(), s.getPredicate(), v.createLiteral(
                                Normalizer.normalize(s.getObject().stringValue(), Normalizer.Form.NFKC)));
                        String label = findLabel(newS, uri);
                        play.Logger.debug("Gefundenes Label: " + label);
                        if (label != null) {
                            play.Logger.info("Found Label: " + label);
                            return label;
                        }
                    }
                }
            }

        } catch (Exception e) {
            play.Logger.error("Failed to find label for " + uri, e);
        }
        return null;
    }

    private static String findLabel(Statement s, String uri) {

        String namespace = turtleResourceProp.getProperty("protocol") + turtleResourceProp.getProperty("namespace");

        Enumeration<Object> keys = turtleObjectProp.keys();
        while (keys.hasMoreElements()) {
            play.Logger.info("huhu");
            String predicate = namespace + turtleObjectProp.getProperty(keys.nextElement().toString());
            play.Logger.debug("Vergleichsstring: " + predicate);
            play.Logger.debug("Vergleichsstring: " + s.getPredicate().stringValue());
            if (predicate.equals(s.getPredicate().stringValue())) {
                return s.getObject().stringValue();
            }
        }

        namespace = turtleResourceProp.getProperty("alternateProtocol") + turtleResourceProp.getProperty("namespace");
        keys = turtleObjectProp.keys();
        while (keys.hasMoreElements()) {
            play.Logger.info("huhu2");
            String predicate = namespace + turtleObjectProp.getProperty(keys.nextElement().toString());
            play.Logger.debug("Vergleichsstring: " + predicate);
            play.Logger.debug("Vergleichsstring: " + s.getPredicate().stringValue());
            if (predicate.equals(s.getPredicate().stringValue())) {
                return s.getObject().stringValue();
            }
        }
        return null;
    }
}

@@ -4317,16 +4317,64 @@
 space%22);
+%0A        play.Logger.info(%22huhu: %22 + namespace);
 %0A%0A      

add debug