package org.zwobble.mammoth.xml;

import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableList;

import java.util.List;
import java.util.Map;
import java.util.Optional;

import static com.google.common.collect.Iterables.*;
import static org.zwobble.mammoth.util.MammothMaps.map;

public class XmlElement implements XmlNode, XmlElementLike {
    public XmlElement(String name) {
        this(name, map());
    }
    
    public XmlElement(String name, Map<String, String> attributes) {
        this(name, attributes, ImmutableList.of());
    }
    
    private final String name;
    private final Map<String, String> attributes;
    private final List<XmlNode> children;
    
    public XmlElement(String name, Map<String, String> attributes, List<XmlNode> children) {
        this.name = name;
        this.attributes = attributes;
        this.children = children;
    }
    
    public String getName() {
        return name;
    }

    public Map<String, String> getAttributes() {
        return attributes;
    }

    public String getAttribute(String name) {
        // TODO: throw more informative error message
        return getAttributeOrNone(name).get();
    }

    @Override
    public Optional<String> getAttributeOrNone(String name) {
        return Optional.ofNullable(attributes.get(name));
    }

    public List<XmlNode> getChildren() {
        return children;
    }

    @Override
    public String innerText() {
        return Joiner.on("").join(transform(children, XmlNode::innerText));
    }

    @Override
    public <T> T accept(XmlNodeVisitor<T> visitor) {
        return visitor.visit(this);
    }

    public Iterable<XmlNode> children() {
        return children;
    }

    @Override
    public String toString() {
        return "XmlElement(name=" + name + ", attributes=" + attributes
               + ", children=" + children + ")";
    }

    public XmlElementList findChildren(String name) {
        Iterable<XmlElement> filtered = findChildrenIterable(name);
        return new XmlElementList(ImmutableList.copyOf(filtered));
    }

    public XmlElement findChild(String name) {
        return findChildrenIterable(name).iterator().next();
    }

    @Override
    public XmlElementLike findChildOrEmpty(String name) {
        return getFirst(findChildrenIterable(name), NullXmlElement.INSTANCE);
    }

    private Iterable<XmlElement> findChildrenIterable(String name) {
        return filter(
            filter(children, XmlElement.class),
            child -> child.getName().equals(name));
    }
}

@@ -243,64 +243,8 @@
 s.*;
-%0Aimport static org.zwobble.mammoth.util.MammothMaps.map;
 %0A%0Apu
@@ -306,215 +306,8 @@
 e %7B%0A
-    public XmlElement(String name) %7B%0A        this(name, map());%0A    %7D%0A    %0A    public XmlElement(String name, Map%3CString, String%3E attributes) %7B%0A        this(name, attributes, ImmutableList.of());%0A    %7D%0A    %0A
     

Remove extra constructors from XmlElement