"""Generated directive and node classes for {{ machine_name }}."""
from __future__ import annotations

from docutils import nodes
from sphinx.util.docutils import directives

from byteb4rb1e.sphinxcontrib.h5p.core import register_h5p_node
from byteb4rb1e.sphinxcontrib.h5p.core.directives import H5PDirective
from byteb4rb1e.sphinxcontrib.h5p.core.nodes import h5p_content

from . import params


# -- Select validators -------------------------------------------------
{% for v in select_validators %}

def {{ v.func_name }}(argument):
    """Validate the :{{ v.option_name }}: option."""
    return directives.choice(argument, (
{% for val in v.allowed_values %}
        '{{ val }}',
{% endfor %}
    ))
{% endfor %}
{% for child in structural_children %}
{% for v in child.select_validators %}

def {{ v.func_name }}(argument):
    """Validate the :{{ v.option_name }}: option."""
    return directives.choice(argument, (
{% for val in v.allowed_values %}
        '{{ val }}',
{% endfor %}
    ))
{% endfor %}
{% endfor %}


# -- Node classes ------------------------------------------------------

class {{ node_class }}(h5p_content):
    """Content node for the ``.. {{ directive_name }}::`` directive."""

    allowed_children = (
{% for child in structural_children %}
        '{{ child.node_class }}',
{% endfor %}
{% for child in library_children %}
        '{{ child.node_class }}',
{% endfor %}
    )
{% for child in structural_children %}


class {{ child.node_class }}(h5p_content):
    """Child node for the ``.. {{ child.directive_name }}::`` directive."""

    allowed_children = ()
{% endfor %}
{% for child in library_children %}


class {{ child.node_class }}(h5p_content):
    """Child node for the ``.. {{ child.directive_name }}::`` directive."""

    allowed_children = ()
{% endfor %}


# -- Directive classes -------------------------------------------------

class {{ directive_class }}(H5PDirective):
    """Directive for ``.. {{ directive_name }}::``."""

    node_class = {{ node_class }}
    h5p_library = '{{ library }}'
{% if has_body %}
    has_content = True
{% endif %}
    option_spec = {
{% for opt in option_spec %}
        '{{ opt.name }}': {{ opt.validator }},
{% endfor %}
    }

    def build_params(self):
        return params.build_params(self.node)
{% for child in structural_children %}


class {{ child.directive_class }}(H5PDirective):
    """Directive for ``.. {{ child.directive_name }}::``."""

    node_class = {{ child.node_class }}
    h5p_library = '{{ library }}'
{% if child.has_body %}
    has_content = True
{% endif %}
    option_spec = {
{% for opt in child.option_spec %}
        '{{ opt.name }}': {{ opt.validator }},
{% endfor %}
    }
{% endfor %}
{% for child in library_children %}


class {{ child.directive_class }}(H5PDirective):
    """Directive for ``.. {{ child.directive_name }}::``."""

    node_class = {{ child.node_class }}
    h5p_library = '{{ library }}'
    option_spec = {}
{% endfor %}


# -- Registration ------------------------------------------------------

def register(app):
    """Register all nodes and directives with Sphinx."""
    register_h5p_node(app, {{ node_class }})
    app.add_directive('{{ directive_name }}', {{ directive_class }})
{% for child in structural_children %}
    register_h5p_node(app, {{ child.node_class }})
    app.add_directive('{{ child.directive_name }}', {{ child.directive_class }})
{% endfor %}
{% for child in library_children %}
    register_h5p_node(app, {{ child.node_class }})
    app.add_directive('{{ child.directive_name }}', {{ child.directive_class }})
{% endfor %}
