[%# This Source Code Form is subject to the terms of the Mozilla Public
  # License, v. 2.0. If a copy of the MPL was not distributed with this
  # file, You can obtain one at http://mozilla.org/MPL/2.0/.
  #
  # This Source Code Form is "Incompatible With Secondary Licenses", as
  # defined by the Mozilla Public License, v. 2.0.
  #%]

[%
  needinfo_flagtype   = "";
  needinfo_flags      = [];

  FOREACH type = bug.flag_types;
    IF type.name == 'needinfo';
      needinfo_flagtype = type;
      FOREACH flag = type.flags;
        IF flag.status == '?';
          needinfo_flags.push(flag);
        END;
      END;
      LAST IF needinfo_flagtype;
    END;
  END;

  available_mentors = bug.mentors( exclude_needinfo_blocked => 1 );

  BLOCK needinfo_comment_div;
    match = needinfo_flags.last.creation_date.match('^(\d{4})\.(\d{2})\.(\d{2})(.+)$');
    date  = "$match.0-$match.1-$match.2$match.3";
    FOREACH comment IN bug.comments;
      NEXT IF comment.is_private AND NOT (user.is_insider || user.id == comment.author.id);
      IF comment.creation_ts == date;
        GET "c$comment.count";
        LAST;
      END;
    END;
  END;

  RETURN UNLESS needinfo_flagtype;
%]

<script [% script_nonce FILTER none%]>
[% IF needinfo_flags.size > 0 %]
$(function() {
  [%# Displays NEEDINFO tag in bug header %]
  var summary_container = document.getElementById('static_bug_status');
  if (summary_container) {
    var needinfo_comment_div = '[% INCLUDE needinfo_comment_div FILTER js %]';
    if (document.getElementById('inline-history-ext')) {
      needinfo_comment_div = inline_history.getNeedinfoDiv();
    }

    if (needinfo_comment_div) {
      var a = document.createElement('a');
      a.id = 'needinfo-lnk';
      a.href = "#" + needinfo_comment_div;
      a.appendChild(document.createTextNode('NEEDINFO'));
      summary_container.appendChild(document.createTextNode('['));
      summary_container.appendChild(a);
      summary_container.appendChild(document.createTextNode(']'));
    }
    else {
      summary_container.appendChild(document.createTextNode('[NEEDINFO]'));
    }
  }
});
[% END %]

[% IF needinfo_flags.size == 0 || needinfo_flagtype.is_multiplicable %]
  function needinfo_init() {
    $('#needinfo')
      .on('change', new_needinfo_changed)
    $('.needinfo-override')
      .on('change', existing_needinfo_changed)
      .change();
    $('#needinfo_role, #needinfo_redirect_role')
      .on('change', needinfo_role_changed);
    needinfo_visibility('needinfo');

    [% FOREACH flag = needinfo_flags %]
      $('#requestee-[% flag.id FILTER none %]')
        .blur(function() {
          $('#needinfo_override_[% flag.id FILTER none %]')
            .prop('checked', $(this).val() === '[% flag.requestee.login FILTER js %]')
        });
    [% END %]
  }

  function needinfo_visibility() {
    var role = $('#needinfo_role').val();
    if (role == 'other') {
      $('#needinfo_from_container').removeClass('bz_default_hidden');
      $('#needinfo_from').prop('disabled', false);
      $('#needinfo_role_identity').text('');
    } else {
      $('#needinfo_from_container').addClass('bz_default_hidden');
      $('#needinfo_from').prop('disabled', true);
      var identity = $("#needinfo_role :selected").data("identity");
      $('#needinfo_role_identity').text(identity);
    }
  }

  function new_needinfo_changed(event) {
    if ($('#needinfo').is(':checked') && $('#needinfo_role').val() === 'other') {
      $('#needinfo_from').prop('required', true);
      $('#needinfo_from').focus().select();
    }
    else {
      $('#needinfo_from').prop('required', false);
    }
  }

  function existing_needinfo_changed(event) {
    var $target = $(event.target);
    var is_redirect = !$target.is(':checked') && $target.data('is-self');
    if (is_redirect) {
      $('#needinfo_from_label').hide();
      $('#needinfo_type').show();
      $('#needinfo_type').val('redirect_to');
    } else {
      $('#needinfo_from_label').show();
      $('#needinfo_type').hide();
      $('#needinfo_type').val('needinfo_from');
    }
  }

  function needinfo_role_changed() {
    $('#needinfo').prop('checked', true);
    needinfo_visibility();
    $('#needinfo').change();
  }

  $(function() {
    needinfo_init();
    $(".needinfo_from_changed").on("change", function (event) {
        $("#needinfo").prop("checked", $("#needinfo_from").val() !== '');
    });
  });
[% END %]
</script>

<div id="needinfo_container">
  <table>
  [% FOREACH flag = needinfo_flags %]
    <tr>
      [% IF !flag.requestee || flag.requestee.id == user.id%]
        [%# needinfo from anyone or the current user %]
        <td align="center">
          <input type="checkbox" id="needinfo_override_[% flag.id FILTER html %]"
            name="needinfo_override_[% flag.id FILTER html %]" value="1" class="needinfo-override"
            [% 'data-is-self="1"' IF flag.requestee %]
            [% " checked" IF flag.requestee.id == user.id || user.in_group("canconfirm") %]>
        </td>
        <td>
          <label for="needinfo_override_[% flag.id FILTER html %]">
            Clear the needinfo request for <em>[% flag.requestee ? flag.requestee.login : "anyone" FILTER html %]</em>.
          </label>
        </td>
      [% ELSIF user.in_group("canconfirm") || flag.setter_id == user.id %]
        [%# needinfo targeted at someone else, but the user can clear %]
        <td align="center">
          <input type="checkbox" id="needinfo_override_[% flag.id FILTER html %]"
                  name="needinfo_override_[% flag.id FILTER html %]" value="1">
        </td>
        <td>
          <label for="needinfo_override_[% flag.id FILTER html %]">
            I am providing the requested information for <em>[% flag.requestee.login FILTER html %]</em>
            (clears the needinfo request).
          </label>
        </td>
      [% ELSE %]
        [%# current user does not have permissions to clear needinfo %]
        <td>&nbsp;</td>
        <td>
          Needinfo requested from <em>[% flag.requestee.login FILTER html %]</em>.
        </td>
      [% END %]
    </tr>
  [% END %]
  [% IF needinfo_flags.size == 0 || needinfo_flagtype.is_multiplicable %]
    <tr>
      <td class="needinfo-cb-td">
        <input type="checkbox" name="needinfo" id="needinfo" value="1">
      </td>
      <td>
        <label for="needinfo" id="needinfo_from_label">Request information from</label>
        <select name="needinfo_type" id="needinfo_type" style="display:none;" aria-label="Request info actions">
          <option value="needinfo_from" selected="true">Need more information from</option>
          <option value="redirect_to">Redirect needinfo request to</option>
        </select>
        <select name="needinfo_role" id="needinfo_role" aria-label="Request info from">
          <option value="other">other</option>
          [% IF NOT bug.reporter.needinfo_blocked %]
            <option
                data-identity="[% bug.reporter.name_or_login FILTER html %]"
                value="reporter">reporter</option>
          [% END %]
          [% IF NOT bug.assigned_to.needinfo_blocked %]
            <option
                data-identity="[% bug.assigned_to.name_or_login FILTER html %]"
                value="assigned_to">assignee</option>
          [% END %]
          [% IF Param('useqacontact') && bug.qa_contact.login != "" && !bug.qa_contact.needinfo_blocked %]
            <option
                data-identity="[% bug.qa_contact.name_or_login FILTER html %]"
                value="qa_contact">qa contact</option>
          [% END %]
          [% IF !is_redirect && NOT user.needinfo_blocked %]
            <option
                data-identity="[% user.name_or_login FILTER html %]"
                value="user">myself</option>
          [% END %]
          [% FOREACH mentor = available_mentors %]
            <option data-identity="[% mentor.name_or_login FILTER html %][% IF available_mentors.size > 1 %][% END %]"
                value="[% mentor.login FILTER html %]">
                [% available_mentors.size == 1 ? "mentor" : mentor.login FILTER html %][% " (mentor)" IF available_mentors.size > 1 %]
            </option>
          [% END %]
          [% IF bug.component_obj.triage_owner_id %]
             <option
                data-identity="[% bug.component_obj.triage_owner.name_or_login FILTER html %]"
                value="triage_owner">triage owner</option>
           [% END %]
        </select>
        <span id="needinfo_from_container">
          [% INCLUDE global/userselect.html.tmpl
              id          => "needinfo_from"
              name        => "needinfo_from"
              value       => ""
              size        => 30
              multiple    => 5
              classes     => ["needinfo_from_changed"]
              field_title => "Enter one or more comma separated users to request more information from"
              request_type => "needinfo"
          %]
        </span>
        <span id="needinfo_role_identity"></span>
      </td>
    </tr>
  [% END %]
  </table>
</div>
