package org.zwobble.mammoth.tests.docx;

import org.junit.jupiter.api.Test;
import org.zwobble.mammoth.internal.documents.NumberingStyle;
import org.zwobble.mammoth.internal.docx.Numbering;
import org.zwobble.mammoth.internal.docx.Styles;
import org.zwobble.mammoth.internal.xml.XmlElement;

import java.util.Optional;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.zwobble.mammoth.internal.docx.NumberingXml.readNumberingXmlElement;
import static org.zwobble.mammoth.internal.util.Lists.list;
import static org.zwobble.mammoth.internal.util.Maps.map;
import static org.zwobble.mammoth.internal.xml.XmlNodes.element;

public class NumberingXmlTests {
    private final static XmlElement SAMPLE_NUMBERING_XML = element("w:numbering", list(
        element("w:abstractNum", map("w:abstractNumId", "42"), list(
            element("w:lvl", map("w:ilvl", "0"), list(
                element("w:numFmt", map("w:val", "bullet")))),
            element("w:lvl", map("w:ilvl", "1"), list(
                element("w:numFmt", map("w:val", "decimal")))))),
        element("w:num", map("w:numId", "47"), list(
            element("w:abstractNumId", map("w:val", "42"))))));

    @Test
    public void findLevelReturnsNoneIfNumWithIdCannotBeFound() {
        Numbering numbering = readNumbering(element("w:numbering"));
        assertEquals(Optional.empty(), numbering.findLevel("47", "0"));
    }

    @Test
    public void levelIncludesLevelIndex() {
        Numbering numbering = readNumbering(SAMPLE_NUMBERING_XML);
        assertEquals("0", numbering.findLevel("47", "0").get().getLevelIndex());
        assertEquals("1", numbering.findLevel("47", "1").get().getLevelIndex());
    }

    @Test
    public void listIsNotOrderedIfFormattedAsBullet() {
        Numbering numbering = readNumbering(SAMPLE_NUMBERING_XML);
        assertEquals(false, numbering.findLevel("47", "0").get().isOrdered());
    }

    @Test
    public void listIsOrderedIfFormattedAsDecimal() {
        Numbering numbering = readNumbering(SAMPLE_NUMBERING_XML);
        assertEquals(true, numbering.findLevel("47", "1").get().isOrdered());
    }

    @Test
    public void whenAbstractNumHasNumStyleLinkThenStyleIsUsedToFindNum() {
        Numbering numbering = readNumberingXmlElement(
            element("w:numbering", list(
                element("w:abstractNum", map("w:abstractNumId", "100"), list(
                    element("w:lvl", map("w:ilvl", "0"), list(
                        element("w:numFmt", map("w:val", "decimal"))
                    ))
                )),
                element("w:abstractNum", map("w:abstractNumId", "101"), list(
                    element("w:numStyleLink", map("w:val", "List1"))
                )),
                element("w:num", map("w:numId", "200"), list(
                    element("w:abstractNumId", map("w:val", "100"))
                )),
                element("w:num", map("w:numId", "201"), list(
                    element("w:abstractNumId", map("w:val", "101"))
                ))
            )),
            new Styles(
                map(),
                map(),
                map(),
                map("List1", new NumberingStyle(Optional.of("200")))
            )
        );
        assertEquals(true, numbering.findLevel("201", "0").get().isOrdered());
    }

    // See: 17.9.23 pStyle (Paragraph Style's Associated Numbering Level) in ECMA-376, 4th Edition
    @Test
    public void numberingLevelCanBeFoundByParagraphStyleId() {
        Numbering numbering = readNumbering(
            element("w:numbering", list(
                element("w:abstractNum", map("w:abstractNumId", "42"), list(
                    element("w:lvl", map("w:ilvl", "0"), list(
                        element("w:numFmt", map("w:val", "bullet"))
                    ))
                )),
                element("w:abstractNum", map("w:abstractNumId", "43"), list(
                    element("w:lvl", map("w:ilvl", "0"), list(
                        element("w:pStyle", map("w:val", "List")),
                        element("w:numFmt", map("w:val", "decimal"))
                    ))
                ))
            ))
        );
        assertEquals(true, numbering.findLevelByParagraphStyleId("List").get().isOrdered());
        assertEquals(false, numbering.findLevelByParagraphStyleId("Paragraph").isPresent());
    }


    private Numbering readNumbering(XmlElement element) {
        return readNumberingXmlElement(element, Styles.EMPTY);
    }
}

@@ -2131,32 +2131,448 @@
 ered());%0A    %7D%0A%0A
+    @Test%0A    public void numReferencingNonExistentAbstractNumIsIgnored() %7B%0A        XmlElement element = element(%22w:numbering%22, list(%0A            element(%22w:num%22, map(%22w:numId%22, %2247%22), list(%0A                element(%22w:abstractNumId%22, map(%22w:val%22, %2242%22))%0A            ))%0A        ));%0A%0A        Numbering numbering = readNumbering(element);%0A%0A        assertEquals(Optional.empty(), numbering.findLevel(%2247%22, %220%22));%0A    %7D%0A%0A
     @Test%0A    pu

Test handling of w:num with invalid w:abstractNumId