# -*- coding: utf-8 -*-

from . import namespaces as ns
from . import xsd


XSD_NAMESPACE = ns.xsd


class Enumeration(xsd.ComplexType):
    value = xsd.Attribute(xsd.String)

    @classmethod
    def create(cls, value):
        enum = Enumeration()
        enum.value = value
        return enum


class Pattern(xsd.ComplexType):
    NAMESPACE = ns.xsd
    value = xsd.Attribute(xsd.String)


class RestrictionValue(xsd.ComplexType):
    NAMESPACE = ns.xsd
    value = xsd.Attribute(xsd.String)


class Restriction(xsd.ComplexType):
    NAMESPACE = ns.xsd
    base = xsd.Attribute(xsd.String)
    enumerations = xsd.ListElement(Enumeration, 'enumeration')
    pattern = xsd.Element(Pattern)
    minInclusive = xsd.Element(RestrictionValue)
    minExclusive = xsd.Element(RestrictionValue)
    maxExclusive = xsd.Element(RestrictionValue)
    maxInclusive = xsd.Element(RestrictionValue)
    fractionDigits = xsd.Element(RestrictionValue)
    totalDigits = xsd.Element(RestrictionValue)

    def to_python(self):
        enum_values = map(lambda e: '\'%s\'' % e.value, self.enumerations)
        return '[%s]' % ','.join(enum_values)


class List(xsd.ComplexType):
    NAMESPACE = ns.xsd
    pass


class SimpleType(xsd.ComplexType):
    NAMESPACE = ns.xsd
    name = xsd.Attribute(xsd.String, use=xsd.Use.OPTIONAL)
    restriction = xsd.Element(Restriction, minOccurs=0)
    list = xsd.Element(List, minOccurs=0)


class Element(xsd.ComplexType):
    NAMESPACE = ns.xsd
    name = xsd.Attribute(xsd.String, use=xsd.Use.OPTIONAL)
    type = xsd.Attribute(xsd.String, use=xsd.Use.OPTIONAL)
    ref = xsd.Attribute(xsd.String, use=xsd.Use.OPTIONAL)
    minOccurs = xsd.Attribute(xsd.Integer, use=xsd.Use.OPTIONAL)
    maxOccurs = xsd.Attribute(xsd.MaxOccurs, use=xsd.Use.OPTIONAL)
    nillable = xsd.Attribute(xsd.Boolean, use=xsd.Use.OPTIONAL)
    simpleType = xsd.Element(SimpleType, minOccurs=0)
    complexType = xsd.Element('soapfish.xsdspec.XSDComplexType')


class Sequence(xsd.ComplexType):
    NAMESPACE = ns.xsd
    elements = xsd.ListElement(Element, 'element')


class Attribute(xsd.ComplexType):
    NAMESPACE = ns.xsd
    name = xsd.Attribute(xsd.String)
    ref = xsd.Attribute(xsd.String)
    type = xsd.Attribute(xsd.String)
    use = xsd.Attribute(xsd.String)


class AttributeGroup(xsd.ComplexType):
    NAMESPACE = ns.xsd
    name = xsd.Attribute(xsd.String)
    attributes = xsd.ListElement(Attribute, 'attribute')


class AttributeGroupReference(xsd.ComplexType):
    NAMESPACE = ns.xsd
    ref = xsd.Attribute(xsd.String)

    def to_python(self):
        typename = get_type(self.ref)
        data = {'name': typename.lower(), 'type': typename}
        return '''    %(name)s = xsd.Ref(%(type)s)\n''' % data


class Extension(xsd.ComplexType):
    NAMESPACE = ns.xsd
    base = xsd.Attribute(xsd.String)
    sequence = xsd.Element(Sequence)
    attributes = xsd.ListElement(Attribute, 'attribute')
    attributeGroups = xsd.ListElement(AttributeGroupReference, 'attributeGroup')


class ComplexContent(xsd.ComplexType):
    NAMESPACE = ns.xsd
    mixed = xsd.Attribute(xsd.Boolean)
    extension = xsd.Element(Extension)
    restriction = xsd.Element(Extension)


class XSDComplexType(xsd.ComplexType):
    NAMESPACE = ns.xsd
    name = xsd.Attribute(xsd.String, use=xsd.Use.OPTIONAL)
    sequence = xsd.Element(Sequence)
    choice = xsd.Element(Sequence)
    all = xsd.Element(Sequence)
    complexContent = xsd.Element(ComplexContent)
    attributes = xsd.ListElement(Attribute, 'attribute')
    attributeGroups = xsd.ListElement(AttributeGroupReference, 'attributeGroup')


class Group(xsd.ComplexType):
    NAMESPACE = ns.xsd
    name = xsd.Attribute(xsd.String)
    sequence = xsd.Element(Sequence)


class Import(xsd.ComplexType):
    schemaLocation = xsd.Attribute(xsd.String, use=xsd.Use.OPTIONAL)
    namespace = xsd.Attribute(xsd.String)


class Schema(xsd.ComplexType):
    NAMESPACE = ns.xsd
    targetNamespace = xsd.Attribute(xsd.String)
    elementFormDefault = xsd.Attribute(
        xsd.String(enumeration=['qualified', 'unqualified']),
        use=xsd.Use.OPTIONAL, default='unqualified',
    )
    imports = xsd.ListElement(Import, 'import')
    simpleTypes = xsd.ListElement(SimpleType, 'simpleType')
    groups = xsd.ListElement(Group, 'group')
    attributeGroups = xsd.ListElement(AttributeGroup, 'attributeGroup')
    complexTypes = xsd.ListElement(XSDComplexType, 'complexType')
    elements = xsd.ListElement(Element, 'element')


SCHEMA = xsd.Schema(
    targetNamespace=ns.xsd,
    elementFormDefault=xsd.ElementFormDefault.QUALIFIED,
    simpleTypes=[],
    attributeGroups=[],
    groups=[],
    complexTypes=[Enumeration, Pattern, RestrictionValue, Restriction, List,
                  SimpleType, Element, Sequence, Attribute, AttributeGroup,
                  AttributeGroupReference, Extension, ComplexContent,
                  XSDComplexType, Group, Schema],
    elements={},
)

@@ -482,32 +482,107 @@
 te(xsd.String)%0A%0A
+    def __repr__(self):%0A        return 'RestrictionValue%3C%25r%3E' %25 self.value%0A
 %0Aclass Restricti

add __repr__ method for RestrictionValue