<%! from django.core.urlresolvers import reverse %>
<%! from datehelper import time_ago_in_words %>
<%! from dateutil.parser import parse %>
<%! import urllib %>

<%def name="render_thread(course_id, thread, edit_thread=False, show_comments=False)">
  <div class="thread" _id="${thread['id']}">
    ${render_content(thread, "thread", edit_thread=edit_thread, show_comments=show_comments)}
    % if show_comments:
      ${render_comments(thread['children'])}
    % endif
  </div>
</%def>

<%def name="render_comments(comments)">
  <div class="comments">
    % for comment in comments:
      <div class="comment" _id="${comment['id']}">
        ${render_content(comment, "comment")}
        <div class="comments">
          ${render_comments(comment['children'])}
        </div>
      </div>
    % endfor
  </div>
</%def>

<%def name="render_content(content, type, **kwargs)">
  <div class="discussion-content">
    <div class="discussion-content-wrapper clearfix">
      ${render_vote(content)}
      <div class="discussion-right-wrapper clearfix">
        ${render_title(content, type, **kwargs)}
        <div class="discussion-content-view">
          <div class="content-body ${type}-body">${content['body'] | h}</div>
          <div class="content-raw-body ${type}-raw-body" style="display: none">${content['body'] | h}</div>
          ${render_tags(content, type, **kwargs)}
          ${render_bottom_bar(content, type, **kwargs)}
        </div>
      </div>
    </div>
  </div>
</%def>

<%def name="render_title(content, type, **kwargs)">
  <%
    if type == "thread":
      if kwargs.get('show_comments', False):
        url_for_thread = ""
      else:
        url_for_thread = reverse('django_comment_client.forum.views.single_thread', args=[course_id, content['commentable_id'], content['id']])
  %>
  % if type == "thread":
    <a class="thread-title" name="${content['id']}" href="${url_for_thread}">${content['title'] | h}</a>
  % endif
</%def>

<%def name="render_tags(content, type, **kwargs)">
  <%
    def url_for_tags(tags):
      return reverse('django_comment_client.forum.views.search', args=[course_id]) + '?' + urllib.urlencode({'tags': ",".join(tags)})
  %>
  % if type == "thread":
    <div class="thread-tags">
      % for tag in content['tags']:
        <a class="thread-tag" href="${url_for_tags([tag])}">${tag | h}</a>
      % endfor
    </div>
    <div class="thread-raw-tags" style="display: none">${",".join(content['tags']) | h}</div>
  % endif
</%def>

<%def name="render_bottom_bar(content, type, **kwargs)">
  <div class="info">
    ${render_info(content)}
    ${render_link("discussion-link discussion-reply", "Reply")}
    ${render_link("discussion-link discussion-edit", "Edit")}
  </div>
</%def>

<%def name="render_info(content)">
  ${time_ago_in_words(parse(content['updated_at']))} ago by
  % if content['anonymous']:
    anonymous
  % else:
    user No.${content['user_id']}
  % endif
  % if content.get('comments_count', -1) >= 0:
    , ${content['comments_count']} comment(s)
  % endif
</%def>

<%def name="render_link(cls, html)">
  <a class="${cls}" href="javascript:void(0)">${html}</a>
</%def>

<%def name="render_vote(content)">
  <div class="discussion-votes">
    ${render_link("discussion-vote discussion-vote-up", "&#x2C4;")}
    ${content['votes']['point']}
    ${render_link("discussion-vote discussion-vote-down", "&#x2C5;")}
  </div>
</%def>
