package org.zwobble.mammoth;

import com.google.common.base.Function;
import com.google.common.collect.Iterables;
import org.zwobble.mammoth.internal.DocumentToHtml;
import org.zwobble.mammoth.internal.DocumentToHtmlOptions;
import org.zwobble.mammoth.internal.documents.DocumentElement;
import org.zwobble.mammoth.internal.documents.HasChildren;
import org.zwobble.mammoth.internal.documents.Paragraph;
import org.zwobble.mammoth.internal.documents.Text;
import org.zwobble.mammoth.internal.docx.DocxFile;
import org.zwobble.mammoth.internal.docx.InMemoryDocxFile;
import org.zwobble.mammoth.internal.docx.ZippedDocxFile;
import org.zwobble.mammoth.internal.html.Html;
import org.zwobble.mammoth.internal.results.InternalResult;
import org.zwobble.mammoth.internal.util.Casts;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Path;
import java.util.List;
import java.util.Optional;
import java.util.zip.ZipFile;

import static org.zwobble.mammoth.internal.docx.DocumentReader.readDocument;
import static org.zwobble.mammoth.internal.util.MammothLists.list;

public class DocumentConverter {
    private final DocumentToHtmlOptions options;

    public DocumentConverter() {
        this(DocumentToHtmlOptions.DEFAULT);
    }

    private DocumentConverter(DocumentToHtmlOptions options) {
        this.options = options;
    }

    public DocumentConverter idPrefix(String idPrefix) {
        return new DocumentConverter(options.idPrefix(idPrefix));
    }

    public DocumentConverter preserveEmptyParagraphs() {
        return new DocumentConverter(options.preserveEmptyParagraphs());
    }

    public DocumentConverter addStyleMap(String styleMap) {
        return new DocumentConverter(options.addStyleMap(styleMap));
    }

    public DocumentConverter disableDefaultStyleMap() {
        return new DocumentConverter(options.disableDefaultStyleMap());
    }

    public Result<String> convertToHtml(InputStream stream) throws IOException {
        return withDocxFile(stream, zipFile ->
            convertToHtml(Optional.empty(), zipFile));
    }

    public Result<String> convertToHtml(File file) throws IOException {
        return withDocxFile(file, zipFile ->
            convertToHtml(Optional.of(file.toPath()), zipFile));
    }

    private InternalResult<String> convertToHtml(Optional<Path> path, DocxFile zipFile) {
        return readDocument(path, zipFile)
            .flatMap(nodes -> DocumentToHtml.convertToHtml(nodes, options))
            .map(Html::stripEmpty)
            .map(Html::collapse)
            .map(Html::write);
    }

    public Result<String> extractRawText(File file) throws IOException {
        return withDocxFile(file, zipFile ->
            readDocument(Optional.of(file.toPath()), zipFile)
                .map(DocumentConverter::extractRawTextOfChildren));
    }

    private static <T> T withDocxFile(File file, Function<DocxFile, T> function) throws IOException {
        try (DocxFile zipFile = new ZippedDocxFile(new ZipFile(file))) {
            return function.apply(zipFile);
        }
    }

    private static <T> T withDocxFile(InputStream stream, Function<DocxFile, T> function) throws IOException {
        try (DocxFile zipFile = InMemoryDocxFile.fromStream(stream)) {
            return function.apply(zipFile);
        }
    }

    private static String extractRawTextOfChildren(HasChildren parent) {
        return extractRawText(parent.getChildren());
    }

    private static String extractRawText(List<DocumentElement> nodes) {
        return String.join("", Iterables.transform(nodes, node -> extractRawText(node)));
    }

    private static String extractRawText(DocumentElement node) {
        return Casts.tryCast(Text.class, node)
            .map(Text::getValue)
            .orElseGet(() -> {
                List<DocumentElement> children = Casts.tryCast(HasChildren.class, node)
                    .map(HasChildren::getChildren)
                    .orElse(list());
                String suffix = node instanceof Paragraph ? "\n\n" : "";
                return extractRawText(children) + suffix;
            });
    }
}

@@ -1494,32 +1494,158 @@
 refix));%0A    %7D%0A%0A
+    /**%0A     * By default, empty paragraphs are ignored.%0A     * Call this to preserve empty paragraphs in the output.%0A     */%0A
     public Docum
@@ -1773,274 +1773,825 @@
     
-public DocumentConverter addStyleMap(String styleMap) %7B%0A        return new DocumentConverter(options.addStyleMap(styleMap));%0A    %7D%0A%0A    public DocumentConverter disableDefaultStyleMap() %7B%0A        return new DocumentConverter(options.disableDefaultStyleMap());%0A    %7D%0A
+/**%0A     * Add a style map to specify the mapping of Word styles to HTML.%0A     * The most recently added style map has the greatest precedence.%0A     */%0A    public DocumentConverter addStyleMap(String styleMap) %7B%0A        return new DocumentConverter(options.addStyleMap(styleMap));%0A    %7D%0A%0A    /**%0A     * By default, any added style maps are combined with the default style map.%0A     * Call this to stop using the default style map altogether.%0A     */%0A    public DocumentConverter disableDefaultStyleMap() %7B%0A        return new DocumentConverter(options.disableDefaultStyleMap());%0A    %7D%0A%0A    /**%0A     * Converts %7B@code stream%7D into an HTML string.%0A     * Note that using this method instead of %7B@link #convertToHtml(File file)%7D%0A     * means that relative paths to other files, such as images, cannot be resolved.%0A     */
 %0A   
@@ -2769,32 +2769,98 @@
 pFile));%0A    %7D%0A%0A
+    /**%0A     * Converts %7B@code file%7D into an HTML string.%0A     */%0A
     public Resul
@@ -3339,32 +3339,200 @@
 :write);%0A    %7D%0A%0A
+    /**%0A     * Extract the raw text of the document.%0A     * This will ignore all formatting in the document.%0A     * Each paragraph is followed by two newlines.%0A     */%0A
     public Resul

Add Javadoc matching README.md