package leigh.mecha.nodegraph;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * The configuration for a graph node. This class only contains the schema (meta-information) for the node. The
 * actual values are stored in the node itself.
 *
 * @author Alex Leigh
 */
public class NodeConfig {
    private final String label;
    private final String name;

    private final ArrayList<NodeAttribute> attrs = new ArrayList<>();

    public NodeConfig(String name, String label) {
        this.label = label;
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public String getLabel() {
        return label;
    }

    public void addAttr(NodeAttribute attr) {
        attrs.add(attr);
    }

    public List<NodeAttribute> getAttrs() {
        return Collections.unmodifiableList(attrs);
    }

    public NodeAttribute getAttr(String name) {
        for (NodeAttribute attr : attrs) {
            if (name.equals(attr.getName())) return attr;
        }
        return null;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        NodeConfig that = (NodeConfig) o;
        return Objects.equals(label, that.label) &&
                Objects.equals(name, that.name) &&
                Objects.equals(attrs, that.attrs);
    }

    @Override
    public int hashCode() {
        return Objects.hash(label, name, attrs);
    }

    @Override
    public String toString() {
        return "NodeConfig{" +
                "label='" + label + '\'' +
                ", name='" + name + '\'' +
                ", attrs=" + attrs +
                '}';
    }
}
