package org.zwobble.mammoth.docx;

import com.google.common.collect.ImmutableList;
import org.zwobble.mammoth.documents.*;
import org.zwobble.mammoth.xml.XmlElement;
import org.zwobble.mammoth.xml.XmlElementLike;
import org.zwobble.mammoth.xml.XmlNode;

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

import static com.google.common.collect.Iterables.*;
import static org.zwobble.mammoth.util.MammothLists.list;

public class BodyXmlReader {
    private final Styles styles;
    private final Numbering numbering;

    public BodyXmlReader(Styles styles, Numbering numbering) {
        this.styles = styles;
        this.numbering = numbering;
    }

    public List<DocumentElement> readElement(XmlElement element) {
        switch (element.getName()) {
            case "w:t":
                return list(new Text(element.innerText()));
            case "w:r":
                return list(new Run(readElements(element.children())));
            case "w:p":
                return list(readParagraph(element));

            case "w:pPr":
                return list();

            default:
                // TODO: emit warning
                return list();
        }
    }

    public List<DocumentElement> readElements(Iterable<XmlNode> nodes) {
        return ImmutableList.copyOf(
            concat(
                transform(
                    filter(nodes, XmlElement.class),
                    this::readElement)));
    }

    private Paragraph readParagraph(XmlElement element) {
        XmlElementLike properties = element.findChildOrEmpty("w:pPr");
        return new Paragraph(
            readParagraphStyle(properties),
            readNumbering(properties),
            readElements(element.children()));
    }

    private Optional<Style> readParagraphStyle(XmlElementLike properties) {
        return properties.findChildOrEmpty("w:pStyle")
            .getAttributeOrNone("w:val")
            .map(styleId -> styles.findParagraphStyleById(styleId)
                .orElse(new Style(styleId, Optional.empty())));
    }

    private Optional<NumberingLevel> readNumbering(XmlElementLike properties) {
        XmlElementLike numberingProperties = properties.findChildOrEmpty("w:numPr");
        Optional<String> numId = numberingProperties.findChildOrEmpty("w:numId").getAttributeOrNone("w:val");
        Optional<String> levelIndex = numberingProperties.findChildOrEmpty("w:ilvl").getAttributeOrNone("w:val");
        if (numId.isPresent() && levelIndex.isPresent()) {
            return numbering.findLevel(numId.get(), levelIndex.get());
        } else {
            return Optional.empty();
        }
    }
}

@@ -1821,16 +1821,24 @@
  return 
+readVal(
 properti
@@ -1843,76 +1843,19 @@
 ties
-.findChildOrEmpty(%22w:pStyle%22)%0A            .getAttributeOrNone(%22w:val
+, %22w:pStyle
 %22)%0A 
@@ -2189,16 +2189,24 @@
 numId = 
+readVal(
 numberin
@@ -2220,62 +2220,18 @@
 ties
-.findChildOrEmpty(%22w:numId%22).getAttributeOrNone(%22w:val
+, %22w:numId
 %22);%0A
@@ -2268,16 +2268,24 @@
 Index = 
+readVal(
 numberin
@@ -2299,60 +2299,16 @@
 ties
-.findChildOrEmpty(%22w:ilvl%22).getAttributeOrNone(%22w:va
+, %22w:ilv
 l%22);
@@ -2488,24 +2488,24 @@
 al.empty();%0A
-
         %7D%0A  
@@ -2507,11 +2507,169 @@
  %7D%0A    %7D
+%0A%0A    private Optional%3CString%3E readVal(XmlElementLike element, String name) %7B%0A        return element.findChildOrEmpty(name).getAttributeOrNone(%22w:val%22);%0A    %7D
 %0A%7D%0A

Extract common code for reading w:val