{% load mptt_tags %}
{% load comments %}

{% with nodes.last as lastnode %}
    {% recursetree nodes %}
        {% get_latest_version as latest_version %}
        {# "last-node-container" is given to a comments-node-container if it's the last node in each level of nodes. (level 1, each level 2, each level 3, etc.). #}
        {# Note: there may be more than one level-2 group of nodes.  The class is given to the last node in each group. #}  
        {# The if statment first check if node is the lastnode, that would be the last node in level-1. #}
        {# If false, it checks if the node has a parent.  If so, it checks if the next node is the same as it's parents next node (basically checking if we moved up 1 level) #}
        <div class="comments-node-container {% block comments-node-container-classes %}{% endblock %} {% if node == lastnode or node.parent and node.rght|add:1 == node.parent.rght %}last-node-container{% endif %} {% if node.get_children.count > 0 %}node-has-children{% endif %}" data-level="{{ node.level }}">
            {# We skip the root node because it is used as a link to the parent_object (not an actual comment) #}
            {% if not node.is_root_node %}
                {% block comment %}
                    {% include "comments/comment.html" %}
                {% endblock comment %}
            {% endif %}
            {% if node.level < max_depth %}
                <div id='{{ node.id }}-children' class="child-comments{% if not node.is_root_node %} left-comment-indent{% endif %}">
                    {{ children }}
                    {# After the last child on each level we add the form to allow submission of a new comment at the children's level #}
                    {# Note: the root node is NOT a real comment, but the first level of visible comments are still it's children #}
                    {% block new_comment %}
                        <div class="comment-form new-comment {{ node.is_root_node|yesno:'root,non-root' }}">
                            <div class="comment-hidden-fields">
                                <input type="hidden" name="parent_id" value="{{ node.id }}">
                                <input type="hidden" name="message">
                            </div>
                            <div class="comment-visible-fields">
                                <textarea class="{% block post_textarea_class %}{% endblock post_textarea_class %}" name="message_holder"></textarea>
                                {% block new-comment-buttons %}
	                                <button type="button" class="action-trigger" data-action="post-new">{% if node.is_root_node %}Submit New Comment{% else %}Submit Response{% endif %}</button>
	                                {% block extra_buttons %}{% endblock extra_buttons %}
                                {% endblock new-comment-buttons %}
                            </div>
                        </div>
                    {% endblock %}
                </div>
            {% endif %}
        </div>
    {% endrecursetree %}
{% endwith %}

{% block no_comments %}
    {% if nodes.count == 1 and nodes.first.is_root_node %}
        {% block no_comments_message %}<p>No comments yet</p>{% endblock no_comments_message %}
    {% endif %}
{% endblock no_comments %}
