/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 *
 *   SPDX-License-Identifier: Apache-2.0
 */

package org.apache.jena.riot.lang;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.IOException;

import org.junit.jupiter.api.Test;

import org.apache.jena.graph.Graph;
import org.apache.jena.graph.Triple;
import org.apache.jena.query.Dataset;
import org.apache.jena.query.DatasetFactory;
import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.Property;
import org.apache.jena.rdf.model.Resource;
import org.apache.jena.rdf.model.ResourceFactory;
import org.apache.jena.riot.Lang;
import org.apache.jena.riot.RDFParser;
import org.apache.jena.riot.system.ErrorHandlerFactory;
import org.apache.jena.sparql.core.DatasetGraph;
import org.apache.jena.sparql.sse.SSE;
import org.apache.jena.sparql.util.Context;
import org.apache.jena.vocabulary.RDF;

public class TestLangJsonLD {

    @Test
    public final void readFromString() throws IOException {
        String jsonld = someSchemaDotOrgJsonld();
        Dataset ds = jsonld2dataset(jsonld, null, Lang.JSONLD);
        assertJohnDoeIsOK(ds.getDefaultModel());
    }

    @Test
    public final void readContext() {
        DatasetGraph dsg = RDFParser.source("testing/RIOT/jsonld11/doc-1.jsonld11").toDatasetGraph();
        assertFalse(dsg.isEmpty());
        // "@vocab" : "http://example.org/vocab" -- not a prefix - does not end in "/" "#" or ":"
        assertEquals(0, dsg.prefixes().size());
    }

    @Test
    public final void readContextArrayPrefixes_1() {
        DatasetGraph dsg = RDFParser.source("testing/RIOT/jsonld11/doc-2.jsonld11").toDatasetGraph();
        assertTrue(dsg.prefixes().containsPrefix("foaf"));
        assertEquals(1, dsg.prefixes().size());
    }

    @Test
    public final void readContextArrayPrefixes_2() {
        DatasetGraph dsg = RDFParser.source("testing/RIOT/jsonld11/doc-3.jsonld11").toDatasetGraph();
        assertTrue(dsg.prefixes().containsPrefix("foaf"));
        assertEquals(3, dsg.prefixes().size());
        assertTrue(dsg.prefixes().containsPrefix("foaf"));
        assertTrue(dsg.prefixes().containsPrefix("foo"));
        assertTrue(dsg.prefixes().containsPrefix(""));
        assertFalse(dsg.prefixes().containsPrefix("bar"));
    }

    @Test
    public void testJsonLdBase() {
        // GH-1451
        //    {
        //        "@id": "./relative",
        //        "@type": "RelType",
        //        "http://example/p": { "@id" : "#frag" }
        //     }

        String jsonld = """
                { "@id": "./relative", "@type": "RelType", "http://example/p": { "@id" : "#frag" } }
                """;
        Graph g = RDFParser.fromString(jsonld, Lang.JSONLD).base("http://base/abc").toGraph();
        assertNotNull(g);
        assertFalse(g.isEmpty());
        Triple t = SSE.parseTriple("( <http://base/relative> <http://example/p> <http://base/abc#frag> )");
        assertTrue(g.contains(t));
    }

    /**
     * Reading some jsonld String, using a Context
     * @return a new Dataset
     * @throws IOException
     */
    private Dataset jsonld2dataset(String jsonld, Context jenaCtx, Lang lang) throws IOException {
        Dataset ds = DatasetFactory.create();
        RDFParser.create()
            .fromString(jsonld)
            .errorHandler(ErrorHandlerFactory.errorHandlerNoLogging)
            .lang(lang)
            // Only needed for JSON-LD 1.0 (jsonld-java) usage.
            // [JSONLD 1.0] Remove
            .context(jenaCtx)
            .parse(ds.asDatasetGraph());
        return ds;
    }

    /**
     * Example data
     */
    private String someSchemaDotOrgJsonld() {
        return String.format("{\"@id\": \"_:b0\", \"@type\": \"Person\", \"name\": \"John Doe\", %s }", schemaOrgContext());
    }

    private String schemaOrgContext() {
        return "\"@context\": \"https://schema.org/\"";
    }

    // a subset of schema.org that can be used as @context for jsonld
    private String schemaOrgResolvedContext() {
        return "{\"name\":{\"@id\":\"https://schema.org/name\"},\"Person\": {\"@id\": \"http://schema.org/Person\"}}";
    }

    private static Resource person1 = ResourceFactory.createResource("http://schema.org/Person");
    private static Resource person2 = ResourceFactory.createResource("https://schema.org/Person");
    private static Property name1 = ResourceFactory.createProperty("http://schema.org/name");
    private static Property name2 = ResourceFactory.createProperty("https://schema.org/name");

    /**
     * Checking that the data loaded from someSchemaDorOrgJsonld into a model, is OK
     */
    private void assertJohnDoeIsOK(Model m) {
        assertTrue(m.contains(null, RDF.type, person1) || m.contains(null, RDF.type, person2));
        assertTrue(m.contains(null, name1, "John Doe") || m.contains(null, name2, "John Doe"));
    }
}
