<%! 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, show_comments=False)">
  <div class="thread" _id="${thread['id']}">
    ${render_content(thread, "thread", show_comments=show_comments)}
    % if show_comments:
      ${render_comments(thread.get('children', []))}
    % endif
  </div>
</%def>

<%def name="render_comment(comment)">
  % if comment['endorsed']:
  <div class="comment endorsed" _id="${comment['id']}">
  % else:
  <div class="comment" _id="${comment['id']}">
  % endif
    ${render_content(comment, "comment")}
    <div class="comments">
      ${render_comments(comment.get('children', []))}
    </div>
  </div>
</%def>

<%def name="render_comments(comments)">
  <div class="comments">
    % for comment in comments:
      ${render_comment(comment)}
    % 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" id="content-body-${content['id']}">${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":
    <a class="thread-title" name="${content['id']}" href="javascript:void(0)">${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.forum_form_discussion', args=[course_id, content['commentable_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)}
    <ul class="discussion-actions">
      <li>${render_link("discussion-link discussion-reply discussion-reply-" + type, "Reply")}</li>
      <li><div class="follow-wrapper"></div></li>
      <li>${render_link("discussion-link discussion-edit", "Edit")}</li>
      <li>
      % if type == "comment" and request.user.is_staff:
        % if content['endorsed']:
          <input type="checkbox" checked="checked" class="discussion-link discussion-endorse" id="discussion-endorse-${content['id']}">
        % else:
          <input type="checkbox" class="discussion-link discussion-endorse" id="discussion-endorse-${content['id']}">
        % endif
        <label class="discussion-link" for="discussion-endorse-${content['id']}">Endorsed</label>
      % endif
      </li>
    </ul>
  </div>
</%def>

<%def name="render_info(content)">
  <div class="comment-time">
    ${time_ago_in_words(parse(content['updated_at']))} ago by
    % if content['anonymous']:
      anonymous
    % else:
      user No.${content['user_id']}
    % endif
  </div>
  <div class="comment-count">
    % if content.get('comments_count', -1) >= 0:
      <a href="javascript:void(0)" class="discussion-show-comments"> 
      % if content.get('comments_count', -1) >= 2:
        Show ${content['comments_count']} comments
      % else:
        Show ${content['comments_count']} comment
      % endif
      </a>
    % endif
  </div>
</%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", "&#9650;")}
    <div class="discussion-votes-point">${content['votes']['point']}</div>
    ${render_link("discussion-vote discussion-vote-down", "&#9660;")}
  </div>
</%def>
