/*
 * Copyright (c) "Neo4j"
 * Neo4j Sweden AB [http://neo4j.com]
 *
 * This file is part of Neo4j.
 *
 * Licensed 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
 *
 *    http://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.
 */
package apoc.path;

import apoc.util.collection.Iterables;
import java.util.Iterator;
import java.util.List;
import org.neo4j.graphalgo.impl.util.PathImpl;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Path;
import org.neo4j.graphdb.Relationship;
import org.neo4j.procedure.Description;
import org.neo4j.procedure.Name;
import org.neo4j.procedure.UserFunction;

/**
 * @author mh
 * @since 19.02.18
 */
public class Paths {

    @UserFunction("apoc.path.create")
    @Description("Returns a `PATH` from the given start `NODE` and `LIST<RELATIONSHIP>`.")
    public Path create(
            @Name(value = "startNode", description = "The starting node of the new path.") Node startNode,
            @Name(
                            value = "rels",
                            defaultValue = "[]",
                            description = "The list of relationships to create the path from.")
                    List<Relationship> rels) {
        if (startNode == null) return null;
        PathImpl.Builder builder = new PathImpl.Builder(startNode);
        if (rels != null) {
            for (Relationship rel : rels) {
                if (rel != null) {
                    builder = builder.push(rel);
                }
            }
        }
        return builder.build();
    }

    @UserFunction("apoc.path.slice")
    @Description("Returns a new `PATH` of the given length, taken from the given `PATH` at the given offset.")
    public Path slice(
            @Name(value = "path", description = "The path to be sliced.") Path path,
            @Name(value = "offset", defaultValue = "0", description = "The offset to begin the new path from.")
                    long offset,
            @Name(value = "length", defaultValue = "-1", description = "The length of the new path.") long length) {
        if (path == null) return null;
        if (offset < 0) offset = 0;
        if (length == -1) length = path.length() - offset;
        if (offset == 0 && length >= path.length()) return path;

        Iterator<Node> nodes = path.nodes().iterator();
        Iterator<Relationship> rels = path.relationships().iterator();
        for (long i = 0; i < offset && nodes.hasNext() && rels.hasNext(); i++) {
            nodes.next();
            rels.next();
        }
        if (!nodes.hasNext()) return null;

        PathImpl.Builder builder = new PathImpl.Builder(nodes.next());
        for (long i = 0; i < length && rels.hasNext(); i++) {
            builder = builder.push(rels.next());
        }
        return builder.build();
    }

    @UserFunction("apoc.path.combine")
    @Description("Combines the two given `PATH` values into one `PATH`.")
    public Path combine(
            @Name(value = "path1", description = "The first path to combine with the second path.") Path first,
            @Name(value = "path2", description = "The second path to combine with the first path.") Path second) {
        if (first == null) return second;
        if (second == null) return first;

        if (!first.endNode().equals(second.startNode()))
            throw new IllegalArgumentException(
                    "Paths don't connect on their end and start-nodes " + first + " with " + second);

        PathImpl.Builder builder = new PathImpl.Builder(first.startNode());
        for (Relationship rel : first.relationships()) builder = builder.push(rel);
        for (Relationship rel : second.relationships()) builder = builder.push(rel);
        return builder.build();
    }

    @UserFunction("apoc.path.elements")
    @Description("Converts the given `PATH` into a `LIST<NODE | RELATIONSHIP>`.")
    public List<Object> elements(
            @Name(value = "path", description = "The path to be converted into a list of nodes and relationships.")
                    Path path) {
        if (path == null) return null;
        return Iterables.asList((Iterable) path);
    }
}
