<%= render 'rostra/shared/sidebar' %>

<div id="page_content">

  <div class="question" id="<%= dom_id(@question) %>">
    <%= render partial: 'rostra/shared/votes', locals: { resource: @question } %>
    <%= image_tag avatar_url(@question.user), class: 'avatar' %>
    <cite>
      Asked by <%= link_to_profile(@question.user) %>
      <%= link_to 'edit', edit_question_path(@question) if can? :manage, @question %>
    </cite>
    <div class="text"><%= simple_format(@question.details) %></div>
    <div class="timestamp"><%= time_ago_in_words(@question.updated_at) %> ago</div>
    <%= tag_list(@question) %>
  </div>

  <% unless @answers.empty? %>
    <h3 id="answer_count"><%= pluralize(@answers.size, 'Answer') %></h3>
    <div id="answers">
      <% @answers.each do |answer| %>
        <div class="answer" id="<%= dom_id(answer) %>">
          <%= render partial: 'rostra/shared/votes', locals: { resource: answer } %>
          <%= image_tag avatar_url(answer.user), class: 'avatar' %>
          <cite>
            <%= link_to_profile(answer.user) %> says:
            <%= link_to('edit', edit_question_answer_path(@question, answer)) if can? :manage, answer %>
          </cite>
          <div class="text"><%= simple_format(answer.text) %></div>
          <div class="timestamp"><%= time_ago_in_words(answer.updated_at) %> ago</div>

          <% if answer.comments.any? %>
            <div class="comments">
              <% answer.comments.each do |comment| %>
                <div class="comment text">
                  <div class="editable" data-ajax_url="<%= question_comment_path(@question, comment) %>"><%= comment.comment %></div>
                  <div class="timestamp">
                    <%= link_to_profile(comment.user) %> – <%= time_ago_in_words(comment.updated_at) %> ago
                    <% if can? :manage, comment %>
                      <%= link_to('edit', question_comment_path(@question, comment), class: 'edit_comment') %>
                      <%= link_to('delete', question_comment_path(@question, comment), method: :delete, confirm: 'Are you sure?') %>
                    <% end %>
                  </div>
                </div>
              <% end %>
            </div>
          <% end %>


          <% if can? :manage, Comment %>
            <%= link_to("comment on this answer", '#', class: 'leave_comment') %>
          <% else %>
            <%= link_to("login to leave comment", main_app_login_path) %>
          <% end %>


          <% if can? :manage, Comment  %>
            <%= simple_form_for [@question, answer.comments.build], validate: true do |f| %>
              <% # TODO: Should not need hidden fields. Why doesn't build apply the foreign keys? %>
              <%= f.hidden_field :user_id, value: rostra_user.id %>
              <%= f.hidden_field :commentable_id, value: answer.id %>
              <%= f.hidden_field :commentable_type, value: "Rostra::Answer" %>
              <%= f.input :comment, label: '' %>
              <%= f.submit 'post comment' %>
            <% end %>
          <% end %>

        </div>
      <% end %>
    </div>
  <% end %>

  <% if user_signed_in? %>
    <%= render 'rostra/answers/form' %>
  <% else %>
    <%= link_to "Login to answer this question", main_app_login_path, class: 'button' %>
  <% end %>
</div><!-- page_content -->