import numpy as np            # pylint: disable=unused-import
import typing                 # pylint: disable=unused-import
from nomad.metainfo import (  # pylint: disable=unused-import
    MSection, MCategory, Category, Package, Quantity, Section, SubSection, SectionProxy,
    Reference
)
from nomad.metainfo.legacy import LegacyDefinition
{% for dependency in pkg.dependencies %}
{{ fromat_package_import(dependency) }}
{%- endfor %}

m_package = Package(
    name='{{ pkg.name }}',
    description='{{ pkg.description }}',
    a_legacy=LegacyDefinition(name='{{pkg.a_legacy.name}}'))
{% for category in order_categories(pkg.category_definitions) %}

class {{ category.name }}(MCategory):
    {% if category.description is not none -%}
    '''
    {{ format_description(category.description, indent=1) }}
    '''
    {%- endif %}

    m_def = Category(
        {% if category.categories | length > 0 -%}
        categories=[{{ format_definition_refs(pkg, category.categories) }}],
        {% endif -%}
        a_legacy=LegacyDefinition(name='{{category.a_legacy.name}}'))
{% endfor -%}

{% for section in pkg.section_definitions %}

class {{ section.name }}({%- if section.extends_base_section -%}{{ format_definition_refs(pkg, section.base_sections)}}{%- else -%}MSection{%- endif -%}):
    {% if section.description is not none -%}
    '''
    {{ format_description(section.description, indent=1) }}
    '''
    {% endif %}
    m_def = Section(validate=False{%- if section.extends_base_section -%}, extends_base_section=True{%- endif -%}, a_legacy=LegacyDefinition(name='{{section.a_legacy.name}}'))
    {% for quantity in section.quantities %}
    {{ quantity.name }} = Quantity(
        type={{ format_type(pkg, quantity.type) }},
        shape={{ quantity.shape }}
        {%- if quantity.unit is not none -%},
        unit={{ format_unit(quantity.unit) }}
        {%- endif -%}
        {%- if quantity.description is not none -%},
        description='''
        {{ format_description(quantity.description, indent=2) }}
        '''{%- endif -%}
        {%- if quantity.categories | length > 0 -%},
        categories=[{{ format_definition_refs(pkg, quantity.categories) }}]
        {%- endif -%},
        a_legacy=LegacyDefinition(name='{{quantity.a_legacy.name}}'))
    {% endfor -%}

    {%- for sub_section in section.sub_sections %}
    {{ sub_section.name }} = SubSection(
        sub_section=SectionProxy('{{ sub_section.sub_section.name }}'),
        repeats={{ sub_section.repeats }}
        {%- if sub_section.description is not none -%},
        description='''
        {{ format_description(sub_section.description, indent=2) }}
        '''{%- endif -%},
        a_legacy=LegacyDefinition(name='{{sub_section.a_legacy.name}}'))
    {% endfor -%}
{%- endfor %}

m_package.__init_metainfo__()

