# file eulxml/xmlmap/eadmap.py
#
#   Copyright 2010,2011 Emory University Libraries
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.

from __future__ import unicode_literals
from copy import deepcopy

import six

from eulxml import xmlmap

# xmlmap objects for various sections of an ead
# organized from smallest/lowest level to highest level

EAD_NAMESPACE = 'urn:isbn:1-931666-22-9'
XLINK_NAMESPACE = 'http://www.w3.org/1999/xlink'


class _EadBase(xmlmap.XmlObject):
    '''Common EAD namespace declarations, for use by all EAD XmlObject instances.'''
    ROOT_NS = EAD_NAMESPACE
    ROOT_NAME = 'ead'
    ROOT_NAMESPACES = {
        'e': ROOT_NS,
        'xlink': XLINK_NAMESPACE,
        'exist': 'http://exist.sourceforge.net/NS/exist'
    }
    # TODO: if there are any universal EAD attributes, they should be added here

    # NOTE: this is not an EAD field, but simplifies using EAD objects with eXist
    # by making exist match-count totals available at any level
    match_count = xmlmap.IntegerField("count(.//exist:match)")
    'Count of exist matches under the current field - for use with EAD and eXist-db'


class Note(_EadBase):
    """EAD note."""
    ROOT_NAME = 'note'
    content = xmlmap.NodeListField("e:p", xmlmap.XmlObject)   # ?? (to allow formatting)
    "list of paragraphs - `p`"


class Section(_EadBase):
    """Generic EAD section.  Currently only has mappings for head, paragraph, and note."""
    head = xmlmap.NodeField("e:head", xmlmap.XmlObject)
    "heading - `head`"
    content = xmlmap.NodeListField("e:p", xmlmap.XmlObject)   # ?? (to allow formatting)
    "list of paragraphs - `p`"
    note = xmlmap.NodeField("e:note", Note)
    ":class:`Note`"


@six.python_2_unicode_compatible
class Heading(_EadBase):