package leigh.conflux;

import leigh.mecha.nodegraph.Node;

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

public class Model {
    private final ArrayList<Node> nodes = new ArrayList<>();

    /**
     * Return an array of all the nodes contained within this model. Returns an empty list if there are no
     * nodes in the model.
     */
    public List<Node> getNodes() {
        return nodes;
    }

    /**
     * Return a {@link List} of all the nodes in this model matching the supplied class. Returns an empty list
     * if there are no matching nodes.
     */
    public List<Node> getNodes(Class clazz) {
        ArrayList<Node> classNodes = new ArrayList<>();
        for (Node n : nodes) {
            if (clazz.equals(n.getClass())) classNodes.add(n);
        }
        return classNodes;
    }

    /**
     * Return the first {@link Node} matching the given class. If multiple nodes match the given class, it is not
     * specified which is returned. Returns <code>null</code> if there is no matching node.
     */
    public Node getNode(Class clazz) {
        for (Node n : nodes) {
            if (clazz.equals(n.getClass())) return n;
        }
        return null;
    }

    public Node getNode(String id) {
        for (Node node : nodes) {
            if (id.equals(node.getId())) return node;
        }
        return null;
    }
}
