<%
# Copyright (C) 2011 - present Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas 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, version 3 of the License.
#
# Canvas 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 this program. If not, see <http://www.gnu.org/licenses/>.
%>

<% multi_answer ||= nil %>
<% answer = multi_answer %>
<% question ||= {} %>
<div class="answer">
  <% if question_type.entry_type == "radio" || question_type.entry_type == "select" || question_type.entry_type == "checkbox" %>
    <% answer_text = (hash_get(answer, :html).present? ? user_content(hash_get(answer, :html)) : hash_get(answer, :text)) || '' %>
    <% answer_label = html_to_text(answer_text + ' ' + (hash_get(question, 'question_text') || '')) %>
    <label class="answer_row">
      <span class="answer_input">
        <% if user_answer %>
          <%= "*" if hash_get(user_answer, :answer_id) == hash_get(answer, :id) %>
          <%= "+" if hash_get(answer, :weight) == 100 %>
        <% elsif question_type.entry_type == "checkbox" %>
          <input type="hidden" name="question_<%= hash_get(question, :id) %>_answer_<%= hash_get(answer, :id) %>" value="0"/>
          <input type="checkbox" <%= "checked" if @stored_params["question_#{hash_get(question, :id)}_answer_#{hash_get(answer, :id)}"].to_s == "1" %> class="question_input" name="question_<%= hash_get(question, :id) %>_answer_<%= hash_get(answer, :id) %>" value="1" id="question_<%= hash_get(question, :id) %>_answer_<%= hash_get(answer, :id) %>" />
        <% else %>
          <input type="radio" <%= "checked" if @stored_params["question_#{hash_get(question, :id)}"].to_s == hash_get(answer, :id).to_s %> class="question_input" name="question_<%= hash_get(question, :id) %>" value="<%= hash_get(answer, :id) %>" id="question_<%= hash_get(question, :id) %>_answer_<%= hash_get(answer, :id) %>" />
        <% end %>
      </span>
      <div class="answer_label">
        <%= answer_text %>
      </div>
    </label>
  <% elsif question_type.entry_type == "matching" %>
    <% html_answer = hash_get(answer, :left_html) || hash_get(answer, :html) %>
    <% text_answer = hash_get(answer, :left) || hash_get(answer, :text) || '' %>
    <% answer_text = html_answer.present? ? user_content(html_answer) : text_answer %>
    <% answer_label = html_to_text(answer_text + ' ' + (hash_get(question, 'question_text') || '')) %>
    <div class="pull-left" style="width: 40%; margin-top: 10px;">
      <% if answer %>
        <label for="question_<%= hash_get(question, :id) %>_answer_<%= hash_get(answer, :id) %>" aria-label="<%= answer_label %>"><%= answer_text %></label>
        <% if !user_answer %>
          <br/>
          <div style="margin-<%= direction('left') %>: 20px;">
          </div>
        <% end %>
      <% end %>
    </div>
    <div class="pull-left" style="width: 40%; margin-top: 10px;">
      <% if user_answer %>
        <% if hash_get(user_answer, "answer_#{hash_get(answer, :id)}").to_i == hash_get(answer, :match_id).to_i %>
          <%= (hash_get(answer, :right) || (hash_get(question, :matches, []).find{|a| hash_get(a, :match_id).to_i == hash_get(answer, :match_id).to_i}[:text] rescue '')) %>
        <% else %>
          <%= hash_get(answer, :match_id) %>
          <%= hash_get(user_answer, "answer_#{hash_get(answer, :id)}") %>
          You put: <%= (hash_get(question, :answers, []).find{|a| hash_get(a, :match_id) == hash_get(user_answer, "answer_#{hash_get(answer, :id)}")}) %><br/>
          It was really: <%= (hash_get(answer, :right) || (hash_get(question, :matches, []).find{|a| hash_get(a, :match_id).to_i == hash_get(answer, :match_id).to_i}[:text] rescue '')) %>
        <% end %>
      <% else %>
        <select name="question_<%= hash_get(question, :id) %>_answer_<%= hash_get(answer, :id) %>" id="question_<%= hash_get(question, :id) %>_answer_<%= hash_get(answer, :id) %>" class="question_input" style="max-width: 90%;" aria-label="<%= answer_label %>">
          <option value="" <%= "selected" unless @stored_params["question_#{hash_get(question, :id)}_answer_#{hash_get(answer, :id)}"] %>>[ <%= t :choose_answer, "Choose" %> ]</option>
          <% right_answers.uniq{|a| hash_get(a, :right) || (hash_get(question, :matches, []).find{|aa| hash_get(aa, :match_id).to_i == hash_get(a, :match_id).to_i}[:text] rescue '') }.each do |a| %>
            <option value="<%= hash_get(a, :match_id) %>" <%= "selected" if @stored_params["question_#{hash_get(question, :id)}_answer_#{hash_get(answer, :id)}"].to_s == hash_get(a, :match_id).to_s %>><%= (hash_get(a, :right) || (hash_get(question, :matches).find{|aa| hash_get(aa, :match_id).to_i == hash_get(a, :match_id).to_i}[:text] rescue '')) %></option>
          <% end %>
        </select>
      <% end %>
    </div>
    <div class="clear"></div>
  <% end %>
</div>
<% if user_answer && (hash_get(user_answer, :answer_id) == hash_get(answer, :id) || (question_type.type == "matching" && hash_get(user_answer, "answer_#{hash_get(answer, :id)}").to_i != hash_get(answer, :match_id).to_i)) %>
<div>
  <%= hash_get(answer, :comments) %>
</div>
<% end %>
