[%# 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.
  #%]

<div id="comment-actions">
  [% IF user.id && user.settings.comment_box_position.value == 'after_comments' %]
    <button type="button" id="add-comment-btn" class="secondary" aria-label="Add Comment">Add Comment &darr;</button>
  [% ELSE %]
    <button type="button" id="bottom-btn" class="secondary" aria-label="Go to Page Bottom">Bottom &darr;</button>
  [% END %]
  <div class="dropdown">
    <button type="button" id="comment-tags-btn" aria-haspopup="true" aria-label="Tags"
      aria-expanded="false" aria-controls="comment-tags-menu" class="dropdown-button minor">Tags &#9662;</button>
    <ul id="comment-tags-menu" role="menu" tabindex="0" class="dropdown-content left" style="display:none">
      <li role="presentation">
        <a role="menuitem" tabindex="-1" data-comment-tag="">Reset</a>
      </li>
    </ul>
  </div>
  <div class="dropdown">
    <button type="button" id="view-menu-btn" aria-haspopup="true" aria-label="Timeline"
      aria-expanded="false" aria-controls="view-menu" class="dropdown-button minor">Timeline &#9662;</button>
    <ul id="view-menu" role="menu" tabindex="0" class="dropdown-content left" style="display:none">
      <li role="presentation">
        <a id="view-reset" role="menuitem" tabindex="-1">Reset</a>
      </li>
      <li role="separator"></li>
      <li role="presentation">
        <a id="view-collapse-all" role="menuitem" tabindex="-1">Collapse All</a>
      </li>
      <li role="presentation">
        <a id="view-expand-all" role="menuitem" tabindex="-1">Expand All</a>
      </li>
      <li role="presentation">
        <a id="view-comments-only" role="menuitem" tabindex="-1">Comments Only</a>
      </li>
      [% IF treeherder_user_ids.size %]
        <li role="separator"></li>
        <li role="presentation">
          <a id="view-toggle-treeherder" role="menuitem" data-userids="[[% treeherder_user_ids.join(',') FILTER none %]]">Show Treeherder Comments</a>
        </li>
      [% END %]
    </ul>
   </div>
</div>

[%
  PROCESS bug/time.html.tmpl;

  FOREACH change_set IN bug.activity_stream;
    NEXT IF change_set.cc_only;
    extra_class = change_set.comment.collapsed ? " ca-" _ change_set.comment.author.id : "";
    '<div class="change-set' _ extra_class _ '" id="' _ change_set.id _ '">';

    extra_class = "";
    IF change_set.user_id == bug.assigned_to.id;
      extra_class = "assignee";
    ELSIF change_set.user_id == bug.reporter.id;
      extra_class = "reporter";
    END;

    IF change_set.comment;
      PROCESS comment_header comment=change_set.comment;
    ELSE;
      PROCESS activity_header activities=change_set.activity id=change_set.id;
    END;

    IF change_set.comment != "";
      PROCESS comment_body comment=change_set.comment;
    END;
    FOREACH activity IN change_set.activity;
      PROCESS activity_body activity=activity change_set=change_set;
    END;

    '</div>';
  END;
%]

[% IF user.can_tag_comments %]
  <div id="ctag" style="display:none">
    <input id="ctag-add" size="10" placeholder="add tag"
      maxlength="[% constants.MAX_COMMENT_TAG_LENGTH FILTER html %]">
    <button type="button" id="ctag-save" class="minor">Save</button>
    <button type="button" id="ctag-cancel" class="minor">Cancel</button>
    <a href="https://wiki.mozilla.org/BMO/comment_tagging" target="_blank" rel="noopener noreferrer" title="About Comment Tagging">Help</a>
  </div>
  <div id="ctag-error" style="display:none">
    <a href="#" class="close-btn" data-for="ctag-error" aria-label="Dismiss">×</a>
    <span id="ctag-error-message"></span>
  </div>
[% END %]

[% BLOCK comment_header %]
  <div class="comment" data-id="[% comment.id FILTER none %]" data-no="[% comment.count FILTER none %]"
       data-tags="[% comment.tags.join(' ') FILTER html %]">
    [%# normal comment header %]
    <table class="layout-table change-head [% extra_class FILTER none %]" id="ch-[% comment.count FILTER none %]"
      [% IF comment.collapsed +%] style="display:none"[% END %] role="presentation">
      <tr>
        <td rowspan="2" class="change-gravatar">
          [% INCLUDE bug_modal/user.html.tmpl
              u = comment.author
              gravatar_size = 32
              gravatar_only = 1
          %]
        </td>

        <td class="change-author">
          [% INCLUDE bug_modal/user.html.tmpl
              u = comment.author
          %]
          [% IF extra_class %]
            <span class="user-role">[% extra_class.ucfirst FILTER none %]</span>
          [% END %]
          [% Hook.process('user', 'bug/comments.html.tmpl') %]
        </td>

        <td rowspan="2" class="comment-actions"><div role="group">
          [% IF user.is_insider && bug.check_can_change_field('longdesc', 0, 1) %]
            [% IF comment.is_private %]
              <div class="comment-private edit-hide bz_private">
                Private
              </div>
            [% END %]
            <div class="comment-private edit-show" style="display:none">
              <input type="hidden" value="1" name="defined_isprivate_[% comment.id FILTER none %]">
              <input type="checkbox" name="isprivate_[% comment.id FILTER none %]"
                  id="is-private-[% comment.id FILTER none %]"
                  class="is-private" value="1" [%= "checked" IF comment.is_private %]>
              <label for="is-private-[% comment.id FILTER none %]" aria-label="Mark as Private">Private</label>
            </div>
          [% END %]
          [% Hook.process('comment_action', 'bug_modal/activity_stream.html.tmpl') %]
          [% IF user.id %]
            [% IF user.can_tag_comments %]
              <button type="button" class="tag-btn minor iconic" aria-label="Tag"
                      data-id="[% comment.id FILTER none %]" data-no="[% comment.count FILTER none %]">
                <span class="icon" aria-hidden="true"></span>
              </button>
            [% END %]
            <button type="button" class="reply-btn minor iconic" aria-label="Reply" [% 'disabled' IF !comment.body %]
                    data-id="[% comment.id FILTER none %]" data-no="[% comment.count FILTER none %]"
                    data-reply-name="[% comment.author.name || comment.author.nick FILTER html %]">
              <span class="icon" aria-hidden="true"></span>
            </button>
          [% END %]
          <button type="button" class="change-spinner minor iconic" id="cs-[% comment.count FILTER none %]"
                  aria-label="Collapse" aria-expanded="true"
                  data-strings='{ "collapse_label": "Collapse", "expand_label": "Expanded" }'>
            <span class="icon" aria-hidden="true"></span>
          </button>
        </div></td>
      </tr>

      <tr id="cr-[% comment.count FILTER none %]" [%= IF comment.collapsed %]style="display:none"[% END %]>
        <td>
          <h3 class="change-name">
            <a href="[% basepath FILTER none %]show_bug.cgi?id=[% bug.bug_id FILTER none %]#c[% comment.count FILTER none %]">
              [% comment.count == 0 ? "Description" : "Comment " _ comment.count ~%]
            </a>
          </h3>
          &bull;
          <div class="change-time">
            [% INCLUDE bug_modal/rel_time.html.tmpl ts=comment.creation_ts %]
          </div>
          [% Hook.process('comment_meta', 'bug_modal/activity_stream.html.tmpl') %]
        </td>
      </tr>

      <tr id="ctag-[% comment.count FILTER none %]">
        <td colspan="2" class="comment-tags">
          [% FOREACH tag IN comment.tags ~%]
            <span class="comment-tag">
              [%~ '<a role="button" aria-label="Remove" class="remove">×</a>' IF user.can_tag_comments %][% tag FILTER html ~%]
            </span>
          [%~ END %]
        </td>
      </tr>
    </table>

    [%# default-collapsed comment header %]
    [% IF comment.collapsed %]
      <table class="layout-table change-head default-collapsed" id="cc-[% comment.count FILTER none %]"
             role="presentation">
      <tr>
        <td class="comment-collapse-reason"
            [% IF user.setting("ui_use_absolute_time") == "on" %]
            title="[% comment.author.nick FILTER html %] [[% comment.creation_ts FILTER time("%Y-%m-%d %H:%M %Z") FILTER html %]]">
            [% ELSE %]
            title="[% comment.author.nick FILTER html %] [[% comment.creation_ts FILTER time_duration FILTER html %]]">
            [% END %]
            Comment hidden ([% comment.collapsed_reason FILTER html %])
        </td>
        <td class="comment-actions">
          <button type="button" class="change-spinner minor iconic" id="ccs-[% comment.count FILTER none %]"
                  aria-label="[% comment.collapsed ? 'Expand' : 'Collapse' %]"
                  aria-expanded="[% comment.collapsed ? 'false' : 'true' %]"
                  data-strings='{ "collapse_label": "Collapse", "expand_label": "Expanded" }'>
            <span class="icon" aria-hidden="true"></span>
          </button>
        </td>
      </tr>
      </table>
    [% END %]
  </div>
[% END %]

[% BLOCK activity_header %]
  [% action = activities.0 %]
  <div class="change" id="a[% id FILTER none %]">
    <table class="layout-table change-head [% extra_class FILTER none %]" role="presentation">
      <tr>
        <td rowspan="2" class="change-gravatar">
          [% INCLUDE bug_modal/user.html.tmpl
              u = action.who
              gravatar_size = 32
              gravatar_only = 1
          %]
        </td>
        <td class="change-author">
          [% INCLUDE bug_modal/user.html.tmpl
              u = action.who
          %]
          [% IF extra_class %]
            <span class="user-role">[% extra_class.ucfirst FILTER none %]</span>
          [% END %]
          [% Hook.process('user', 'bug/changes.html.tmpl') %]
        </td>
        <td rowspan="2" class="comment-actions"><div role="group">
          <button type="button" class="change-spinner minor iconic" id="as-[% id FILTER none %]"
                  aria-label="Collapse" aria-expanded="true"
                  data-strings='{ "collapse_label": "Collapse", "expand_label": "Expanded" }'>
            <span class="icon" aria-hidden="true"></span>
          </button>
        </div></td>
      </tr>
      <tr id="ar-[% id FILTER none %]">
        <td>
          <h3 class="change-name">
            <a href="[% basepath FILTER none %]show_bug.cgi?id=[% bug.bug_id FILTER none %]#[% id FILTER none %]">Updated</a>
          </h3>
          &bull;
          <div class="change-time">
            [% INCLUDE bug_modal/rel_time.html.tmpl ts=action.when %]
          </div>
        </td>
      </tr>
    </table>
  </div>
[% END %]

[% BLOCK comment_body %]
  [% IF comment.type == constants.CMT_ATTACHMENT_CREATED %]
    [% att = comment.attachment; link = 'attachment.cgi?id=' _ att.id %]
    <div id="att-[% att.id FILTER none %]"
         class="attachment[% " patch" IF att.ispatch; " obsolete" IF att.isobsolete; " deleted" IF !att.datasize %]"
         data-id="[% att.id FILTER none %]" itemscope itemtype="http://schema.org/MediaObject"
         [% IF comment.collapsed +%] style="display:none"[% END ~%]>
      <meta itemprop="name" content="[% att.filename FILTER html %]">
      <meta itemprop="contentSize" content="[% att.datasize FILTER none %]">
      <meta itemprop="encodingFormat" content="[% att.mimetype FILTER html %]">
      <div class="label">
        Attached [% IF att.is_image %]image
          [% ELSIF att.is_audio %]audio[% ELSIF att.is_video %]video
          [% ELSIF att.ispatch %]patch[% ELSE %]file[% END %]
        [% IF att.ispatch %]
          <meta itemprop="contentUrl" content="[% link FILTER html %]">
          <a class="link" href="[% link FILTER html %]&amp;action=diff">
        [% ELSE %]
          <a class="link[% " lightbox" IF att.is_image %]" href="[% link FILTER html %]" itemprop="contentUrl">
        [% END %]
        <span id="att-[% att.id FILTER none %]-description" itemprop="description">[% att.description FILTER html %]</span></a>
        [% " (obsolete)" IF att.isobsolete; " (deleted)" IF !att.datasize %]
        — <a href="[% link FILTER html %]&amp;action=edit" itemprop="url">Details</a>
        [% IF att.ispatch && Param('splinter_base') %]
          — <a href="[% Bugzilla.splinter_review_url(bug.bug_id, att.id) FILTER none %]">Splinter Review</a>
        [% END %]
      </div>
    </div>
  [% END %]
  [% IF comment.is_markdown AND Param('use_markdown') %]
    [% comment_tag = 'div' %]
  [% ELSE %]
    [% comment_tag = 'pre' %]
  [% END %]

  [%# Description (Comment 0) can be empty %]
  [% IF comment.body || comment.count == 0 %]
    <[% comment_tag FILTER none %]
        class="comment-text [%= "markdown-body" IF comment.is_markdown %] [%= "bz_private" IF comment.is_private %]
               [% "empty" IF comment.body == "" %]"
        id="ct-[% comment.count FILTER none %]" data-comment-id="[% comment.id FILTER none %]"
        [% IF comment.is_markdown +%] data-ismarkdown="true" [% END ~%]
        [% IF comment.collapsed +%] style="display:none"[% END ~%]>
      [%~ IF comment.body != "" ~%]
        [%~ comment.body_full({ exclude_attachment => 1 }) FILTER renderMarkdown(bug, comment) ~%]
      [%~ ELSE ~%]
        <em>No description provided.</em>
      [%~ END ~%]
    </[% comment_tag FILTER none %]>
  [% END %]
[% END %]

[%
  BLOCK activity_body;
    RETURN IF activity.cc_only && !change_set.cc_only;
    style = "";
    IF change_set.comment && change_set.comment.collapsed;
      style = ' style="display:none"';
    END;
    '<div class="activity"' _ style _ '>';

    FOREACH change IN activity.changes;
      NEXT IF change.fieldname == 'cc';
      '<div class="change">';

      IF change.attachid;
        %]
        <a href="[% basepath FILTER none %]attachment.cgi?id=[% change.attachid FILTER none %]&amp;action=edit"
           title="[% change.attach.description FILTER html %]"
           class="[% "bz_obsolete" IF change.attach.isobsoletee %]"
        >Attachment #[% change.attachid FILTER none %]</a> -
        [%+
      END;

      IF change.buglist;
        IF change.fieldname == 'has_dupe';
          label = "Duplicate of this " _ terms.bug;
        ELSIF change.fieldname == 'dupe_of';
          label = "Duplicate of " _ terms.bug;
        ELSE;
          label = field_descs.${change.fieldname};
        END;
        IF change.added != '';
          label _ ": " FILTER html;
          PROCESS add_change value=change.added;
        END;
        IF change.removed != '';
          IF change.added != '';
            "<br>";
          END;
          "No longer ";
          label FILTER lcfirst;
          ": ";
          PROCESS add_change value=change.removed;
        END;

      ELSE;
        IF change.fieldname == 'longdescs.isprivate';
          # reference the comment that was made private/public in the field label
          %]
          <a href="#c[% change.comment.count FILTER none %]">
            Comment [% change.comment.count FILTER none %]</a> is private:
          [%+
        ELSIF change.fieldname == 'reporter_accessible';
          "";
        ELSIF change.fieldname.match('^cf_(status|tracking)_');
          %]
          <a href="[% basepath FILTER none %]buglist.cgi?f1=[% change.fieldname FILTER uri %]&amp;o1=isnotempty">
            [%~ field_descs.${change.fieldname} FILTER html %]</a>:
          [%+
        ELSE;
          field_descs.${change.fieldname} _ ": " FILTER html;
        END;

        IF change.removed != '';
          IF change.added == '';
            '<span class="activity-deleted">';
          END;
          PROCESS add_change value=change.removed;
          IF change.added == '';
            '</span>';
          ELSE;
            ' &rarr; ';
          END;
        END;
        IF change.fieldname == 'cf_user_story';
          %](updated)[%
        ELSE;
          PROCESS add_change value=change.added;
        END;
      END;

      '</div>';
    END;
    '</div>';
  END;

  BLOCK add_change;
    SWITCH change.fieldname;

      CASE [ 'estimated_time', 'remaining_time', 'work_time' ];
        PROCESS formattimeunit time_unit=value;

      CASE 'bug_file_loc';
        %]
        <a href="[% value FILTER html %]" target="_blank" rel="noreferrer"
           class="bug-url" data-safe="[% is_safe_url(value) ? 1 : 0 %]"
        >[% value FILTER truncate(40) FILTER html %]</a>
        [%

      CASE 'see_also';
        FOREACH see_also IN value;
          IF see_also.bug_id;
            see_also.bug_id FILTER bug_link(see_also.bug_id, use_alias => 1);
          ELSE;
            %]
            <a href="[% see_also.url FILTER html %]" target="_blank" rel="noreferrer">[% see_also.url FILTER html %]</a>
            [%
          END;
          ", " UNLESS loop.last;
        END;

      CASE [ 'assigned_to', 'reporter', 'qa_contact', 'cc', 'bug_mentor', 'flagtypes.name' ];
        value FILTER email;

      CASE 'reporter_accessible';
        IF value == 'true';
          "Accessible to reporter";
        ELSIF value == 'false';
          "Not accessible to reporter";
        END;

      CASE 'keywords';
        FOREACH keyword IN value.split(', ');
          %]
            <a href="[% basepath FILTER none %]buglist.cgi?keywords=[% keyword FILTER uri %]&amp;resolution=---">
              [%~ keyword FILTER html %]</a>[% ", " UNLESS loop.last %]
          [%+
        END;

      CASE;
        IF change.fieldtype == constants.FIELD_TYPE_DATETIME;
          INCLUDE bug_modal/rel_time.html.tmpl ts=value;

        ELSIF change.buglist;
          value FILTER bug_list_link(use_alias => 1);

        ELSIF change.fieldname.match('^cf_(status|tracking)_') && value != '---';
          %]
          <a href="[% basepath FILTER none %]buglist.cgi?f1=[% change.fieldname FILTER uri %]&amp;o1=equals&amp;v1=
            [%~ value FILTER uri %]">[% value FILTER html %]</a>
          [%+
        ELSE;
          value FILTER truncate(256, '…') FILTER html;

        END;
    END;
  END;
%]
