# encoding: utf-8

"""
Custom element classes that correspond to the document part, e.g.
<w:document>.
"""

from ..ns import qn
from ..table import CT_Tbl
from ..xmlchemy import BaseOxmlElement, ZeroOrOne, ZeroOrMore


class CT_Document(BaseOxmlElement):
    """
    ``<w:document>`` element, the root element of a document.xml file.
    """
    body = ZeroOrOne('w:body')

    @property
    def sectPr_lst(self):
        """
        Return a list containing a reference to each ``<w:sectPr>`` element
        in the document, in the order encountered.
        """
        return self.xpath('.//w:sectPr')


class CT_Body(BaseOxmlElement):
    """
    ``<w:body>``, the container element for the main document story in
    ``document.xml``.
    """
    p = ZeroOrMore('w:p', successors=('w:sectPr',))
    tbl = ZeroOrMore('w:tbl', successors=('w:sectPr',))

    def add_section_break(self):
        """
        Return the current ``<w:sectPr>`` element after adding a clone of it
        in a new ``<w:p>`` element appended to the block content elements.
        Note that the "current" ``<w:sectPr>`` will always be the sentinel
        sectPr in this case since we're always working at the end of the
        block content.
        """
        raise NotImplementedError

    def _insert_p(self, p):
        return self._append_blocklevelelt(p)

    def _insert_tbl(self, tbl):
        return self._append_blocklevelelt(tbl)

    def _new_tbl(self):
        return CT_Tbl.new()

    def clear_content(self):
        """
        Remove all content child elements from this <w:body> element. Leave
        the <w:sectPr> element if it is present.
        """
        if self._sentinel_sectPr is not None:
            content_elms = self[:-1]
        else:
            content_elms = self[:]
        for content_elm in content_elms:
            self.remove(content_elm)

    def _append_blocklevelelt(self, block_level_elt):
        """
        Return *block_level_elt* after appending it to end of
        EG_BlockLevelElts sequence.
        """
        sentinel_sectPr = self._sentinel_sectPr
        if sentinel_sectPr is not None:
            sentinel_sectPr.addprevious(block_level_elt)
        else:
            self.append(block_level_elt)
        return block_level_elt

    @property
    def _sentinel_sectPr(self):
        """
        Return ``<w:sectPr>`` element appearing as last child, or None if not
        found. Note that the ``<w:sectPr>`` element can also occur earlier in
        the body; here we're only interested in one occuring as the last
        child.
        """
        if len(self) == 0:
            sentinel_sectPr = None
        elif self[-1].tag != qn('w:sectPr'):
            sentinel_sectPr = None
        else:
            sentinel_sectPr = self[-1]
        return sentinel_sectPr

@@ -105,28 +105,8 @@
 %22%22%0A%0A
-from ..ns import qn%0A
 from
@@ -829,16 +829,66 @@
 ctPr',))
+%0A    sectPr = ZeroOrOne('w:sectPr', successors=())
 %0A%0A    de
@@ -1693,34 +1693,24 @@
     if self.
-_sentinel_
 sectPr is no
@@ -2062,33 +2062,24 @@
 %22%22%22%0A        
-sentinel_
 sectPr = sel
@@ -2080,26 +2080,16 @@
  = self.
-_sentinel_
 sectPr%0A 
@@ -2098,25 +2098,16 @@
      if 
-sentinel_
 sectPr i
@@ -2126,33 +2126,24 @@
             
-sentinel_
 sectPr.addpr
@@ -2256,545 +2256,4 @@
 elt%0A
-%0A    @property%0A    def _sentinel_sectPr(self):%0A        %22%22%22%0A        Return %60%60%3Cw:sectPr%3E%60%60 element appearing as last child, or None if not%0A        found. Note that the %60%60%3Cw:sectPr%3E%60%60 element can also occur earlier in%0A        the body; here we're only interested in one occuring as the last%0A        child.%0A        %22%22%22%0A        if len(self) == 0:%0A            sentinel_sectPr = None%0A        elif self%5B-1%5D.tag != qn('w:sectPr'):%0A            sentinel_sectPr = None%0A        else:%0A            sentinel_sectPr = self%5B-1%5D%0A        return sentinel_sectPr%0A

add CT_Body.sectPr