{# freepost
 # http://freepo.st
 # 
 # Copyright © 2014-2015 zPlus
 # 
 # This file is part of freepost.
 # freepost is free software: you can redistribute it and/or modify
 # it under the terms of the GNU Affero General Public License as
 # published by the Free Software Foundation, either version 3 of the
 # License, or (at your option) any later version.
 # 
 # freepost is distributed in the hope that it will be useful,
 # but WITHOUT ANY WARRANTY; without even the implied warranty of
 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 # GNU Affero General Public License for more details.
 # 
 # You should have received a copy of the GNU Affero General Public License
 # along with freepost. If not, see <http://www.gnu.org/licenses/>.
 #}

{# This template is used to print a post's comment tree recursively #}

{% set parentId = (parent is null ? 'root' : parent.id) %}

{# Since comments are written recursively, this check is the condition to exit
 # the loop. The last comment will call this template with itself as parent,
 # but no children by definition.
 #}
 
{% if comments[parentId] is defined %}

    {% for aComment in comments[parentId] %}
        
        {# The user vote for this comment #}
        {% set userVote = aComment.votes|length == 0 ? 0 : aComment.votes[0].vote %}
        
        <div data-hashid="{{ aComment.hashId }}"  data-uservote="{{ userVote }}" class="comment" style="padding-left: {{ depth }}px">
            {# Here I should put the user picture #}
            <div class="userPicture">
                <img alt="" title="" src="{{ aComment.user|userPicture }}" />
            </div>

            {# Text of the comment #}
            <div class="content">
                <div class="userName {{ aComment.user.hashId == op.hashId ? 'op' }}">
                    {{ aComment.user.username }}
                </div>
                
                <div class="datetime">
                    {% set difference = date(aComment.created).diff(currentDate) %}
                    {% if difference.days > 0 %}
                        {{ difference.days }} days ago
                    {% else %}
                        {% if difference.h > 0 %}
                            {{ difference.h }} hours ago
                        {% else %}
                            {{ difference.i }} minutes ago
                        {% endif %}
                    {% endif %}
                </div>
                
                <div class="text">
                    {{ aComment.text|raw }}
                </div>
                
                {# Here a textarea will be created after clicking "Reply" #}
                <div class="editTextarea"></div>
                
                {# Here a textarea will be created after clicking "Reply" #}
                <div class="replyTextarea"></div>
                
                <div class="menu">
                    {% image '@AppBundle/Resources/public/images/upvote-slim.png' %}
                        <img class="upvote {{ userVote == 1 ? 'selected' }}" src="{{ asset_url }}" alt="" title="Upvote" />
                    {% endimage %}
                    {% image '@AppBundle/Resources/public/images/downvote-slim.png' %}
                        <img class="downvote {{ userVote == -1 ? 'selected' }}" src="{{ asset_url }}" alt="" title="Downvote" />
                    {% endimage %}
                    
                    <span class="points">
                        {{ aComment.vote }}
                    </span>

                    <ul>
                        
                        {# Edit comment #}
                        
                        {% if app.user and aComment.user.id == app.user.id %}
                            <li class="edit">Edit</li>
                            <li class="editCancel hidden">Cancel</li>
                            <li class="editSave hidden">Save</li>
                        {% endif %}
                        
                        {# Reply to this comment #}
                        
                        {% if app.user %}
                            <li class="reply">Reply</li>
                            <li class="cancel hidden">Cancel</li>
                            <li class="submit hidden">Submit</li>
                        {% endif %}
                        
                        {# Loading icon... #}
                        
                        {% if app.user %}
                            <li class="loading hidden">
                                {% image '@AppBundle/Resources/public/images/loading-bar.gif' %}
                                    <img src="{{ asset_url }}" alt="" />
                                {% endimage %}
                            </li>
                        {% endif %}
                        
                        {# More options #}
                        
                        <li class="moreButtons hidden">
                        
                            {# Link to this comment #}
                            <span class="link">
                                <a class="none" target="_self" href="{{ path('freepost_post_comment', {'communityName': post.community.name, 'postHashId': post.hashId, 'postTitle': post.title, 'commentHashId': aComment.hashId}) }}">
                                    Link
                                </a>
                            </span>
                            
                            {# Link to this comment's parent #}
                            {% if parent is not null %}
                                <span class="link">
                                    <a class="none" target="_self" href="{{ path('freepost_post_comment', {'communityName': post.community.name, 'postHashId': post.hashId, 'postTitle': post.title, 'commentHashId': parent.hashId}) }}">
                                        Parent
                                    </a>
                                </span>
                            {% endif %}
                            
                        </li>
                        
                        {% image '@AppBundle/Resources/public/images/more.png' %}
                            <img title="More" class="more" src="{{ asset_url }}" alt="" />
                        {% endimage %}
                    </ul>
                </div>
                
                
            </div>
        </div>
        
        {% include 'AppBundle:Default:Post/comment.html.twig' with {'parent': aComment, 'depth': depth+32} %}
    {% endfor %}

{% endif %}
