<%
  # partial inputs:
  comment ||= raise "required Comment missing" # the Comment to render
  force_open ||= false # this comment is allowed to collapse
  show_tree_lines ||= false # render collapse button (misnamed?)
  show_story ||= false # show "on: story title" end of byline
  was_merged ||= false # show merged icon start of byline
  is_unread ||= false # show (unread) in byline
%>
<input id="comment_folder_<%= comment.short_id %>"
  class="comment_folder_button" type="checkbox"
  <%= force_open ||
      @user.try(:is_moderator?) ||
      (comment.score > Comment::COLLAPSE_SCORE && !comment.current_flagged?) ? "" : "checked" %>>

<div id="c_<%= comment.short_id %>"
  data-shortid="<%= comment.short_id if comment.persisted? %>"
  class="comment
    <%= comment.current_upvoted? ? "upvoted" : "" %>
    <%= comment.current_flagged? ? "flagged" : "" %>
    <%= comment.score < Comment::SCORE_RANGE_TO_HIDE.first ? "bad" : "" %>
  ">

  <% if show_tree_lines %>
    <label for="comment_folder_<%= comment.short_id %>"
      class="comment_folder"></label>
  <% end %>

  <% if !comment.is_gone? %>
    <% can_flag = @user && @user.can_flag?(comment) %>
    <% score_display = comment.score_for_user(@user) %>
    <div class="voters">
      <% if @user %>
        <a class="upvoter"></a>
      <% else %>
        <%= link_to "", login_path, :class => "upvoter" %>
      <% end %>
      <div class="score"><%= score_display %></div>
    </div>
    <div class="comment_parent_tree_line
      <%= can_flag ? "can_flag" : "" %>
      <%= score_display != "&nbsp;" ? "score_shown" : "" %>
      <%= comment.reply_count.to_i == 0 ? "no_children" : "" %>"></div>
  <% end %>

  <div class="details">
    <div class="byline">
      <a name="c_<%= comment.short_id %>"></a>

      <% if show_tree_lines %>
        <label for="comment_folder_<%= comment.short_id %>"
          class="comment_folder comment_folder_inline"></label>
      <% else %>
        <label for="comment_folder_<%= comment.short_id %>"
          class="comment_folder comment_folder_inline force_inline"></label>
      <% end %>

      <% if was_merged %>
        <span class="merge"></span>
      <% end %>

      <% if (@user && @user.show_avatars?) || !@user %>
        <%= link_to avatar_img(comment.user, 16), comment.user %>
      <% end %>

      <%= styled_user_link comment.user, comment %>

      <% if comment.hat %>
        <%= comment.hat.to_html_label %>
      <% end %>

      <% if comment.previewing %>
        previewed
        just now
      <% else %>
        <% if comment.has_been_edited? %>
          edited
        <% elsif comment.is_from_email? %>
          e-mailed
        <% end %>
        <%= time_ago_in_words_label(comment.has_been_edited? ? comment.updated_at : comment.created_at) %>
        <%= raw "<span class='comment_unread'>(unread)</span>" if is_unread %>
      <% end %>

      <% if !comment.previewing %>
        |
        <a href="<%= comment.path %>">link</a>

        <% if comment.is_editable_by_user?(@user) %>
          |
          <a tabindex="0" class="comment_editor">edit</a>
        <% end %>

        <% if comment.is_gone? && comment.is_undeletable_by_user?(@user) %>
          |
          <a tabindex="0" class="comment_undeletor" href="#c_<%= comment.short_id %>">undelete</a>
        <% elsif !comment.is_gone? && comment.is_deletable_by_user?(@user) %>
          |
          <% if @user && @user.is_moderator? && @user.id != comment.user_id %>
            <a tabindex="0" class="comment_moderator" href="#c_<%= comment.short_id %>">delete</a>
          <% else %>
            <a tabindex="0" class="comment_deletor" href="#c_<%= comment.short_id %>">delete</a>
          <% end %>
        <% elsif !comment.is_gone? && comment.is_disownable_by_user?(@user) %>
          |
          <a tabindex="0" class="comment-disowner" href="#">disown</a>
        <% end %>

        <% if can_flag && !comment.current_flagged? %>
          <span class="dropdown_parent">
            | <a class="flagger">flag</a>
          </span>
        <% elsif comment.current_flagged? %>
          | <a class="flagger">unflag</a>
        <% else %>
          <span class="flagger flagger_stub"></span>
        <% end %>

        <% if @user && !comment.is_gone? && comment.story.accepting_comments? %>
            |
            <a tabindex="0" class="comment_replier" unselectable="on" href="<%= reply_comment_path(comment) %>">reply</a>
        <% end %>

        <% if show_story %>
          | on:
          <a href="<%= comment.story.comments_path %>"><%= comment.story.title %></a>
        <% end %>

        <span class="reason">
          <% if comment.flags > 0 &&
          comment.show_score_to_user?(@user) &&
          (comment.user_id == @user.try(:id) || @user.try("is_moderator?")) %>
            <% if (s = comment.vote_summary_for_user(@user).downcase).present? %>
              | <%= s %>
            <% end %>
          <% elsif comment.current_flagged? %>
            | -1
            <%= Vote::ALL_COMMENT_REASONS[comment.current_vote[:reason]].downcase %>
          <% end %>
        </span>
      <% end %>
    </div>
    <div role="heading" aria-level="3" class="comment_text">
      <% if comment.is_gone? %>
        <p>
        <span class="na">
          [<%= comment.gone_text %><% if @user && @user.is_moderator? %> Visible to moderator:<% end %>]
        </span>
        </p>
        <% if @user && @user.is_moderator? %>
          <%= raw comment.markeddown_comment %>
        <% end %>
      <% else %>
        <%= raw comment.markeddown_comment %>
      <% end %>
    </div>
  </div>
</div>
