template(name="activities")
  .activities.js-sidebar-activities
    //- We should use Template.dynamic here but there is a bug with
    //- blaze-components: https://github.com/peerlibrary/meteor-blaze-components/issues/30
    if $eq mode "board"
      +boardActivities
    else
      +cardActivities

template(name="boardActivities")
  each activityData in currentBoard.activities
    +activity(activity=activityData card=card mode=mode)

template(name="cardActivities")
  each activityData in activities
    +activity(activity=activityData card=card mode=mode)

template(name="editOrDeleteComment")
  = ' - '
  a.js-open-inlined-form {{_ "edit"}}
  = ' - '
  a.js-delete-comment {{_ "delete"}}

template(name="deleteCommentPopup")
  p {{_ "comment-delete"}}
  button.js-confirm.negate.full(type="submit") {{_ 'delete'}}

template(name="commentReactions")
  .reactions
    each reaction in reactions
      span.reaction(class="{{#if isSelected reaction.userIds}}selected{{/if}}" data-codepoint="#{reaction.reactionCodepoint}" title="{{userNames reaction.userIds}}")
        span.reaction-codepoint !{reaction.reactionCodepoint}
        span.reaction-count #{reaction.userIds.length}
    if (currentUser.isBoardMember)
      a.open-comment-reaction-popup(title="{{_ 'addReactionPopup-title'}}")
        i.fa.fa-smile-o
        i.fa.fa-plus

template(name="addReactionPopup")
  .reactions-popup
    each codepoint in codepoints
      span.add-comment-reaction(data-codepoint="#{codepoint}") !{codepoint}

template(name="activity")
  .activity(data-id=activity._id)
    +userAvatar(userId=activity.user._id)
    p.activity-desc
      span.activity-member
        +memberName(user=activity.user)

      //- attachment activity -------------------------------------------------
      if($eq activity.activityType 'deleteAttachment')
        | {{{_ 'activity-delete-attach' cardLink}}}.

      if($eq activity.activityType 'addAttachment')
        | {{{_ 'activity-attached' attachmentLink cardLink}}}.
        if($neq mode 'board')
          if activity.attachment.isImage
            img.attachment-image-preview(src=activity.attachment.url)

      //- board activity ------------------------------------------------------
      if($eq activity.activityType 'createBoard')
        | {{{_ 'activity-created' boardLabelLink}}}.

      if($eq activity.activityType 'importBoard')
        | {{{_ 'activity-imported-board' boardLabelLink sourceLink}}}.

      if($eq activity.activityType 'addBoardMember')
        | {{{_ 'activity-added' memberLink boardLabelLink}}}.

      if($eq activity.activityType 'removeBoardMember')
        | {{{_ 'activity-excluded' memberLink boardLabelLink}}}.

      //- card activity -------------------------------------------------------
      if($eq activity.activityType 'createCard')
        if($eq mode 'card')
          | {{{_ 'activity-added' cardLabelLink (sanitize activity.listName)}}}.
        else
          | {{{_ 'activity-added' cardLabelLink boardLabelLink}}}.

      if($eq activity.activityType 'importCard')
        | {{{_ 'activity-imported' cardLink boardLabelLink sourceLink}}}.

      if($eq activity.activityType 'moveCard')
        | {{{_ 'activity-moved' cardLabelLink (sanitize activity.oldList.title) (sanitize activity.list.title)}}}.

      if($eq activity.activityType 'moveCardBoard')
        | {{{_ 'activity-moved' cardLink (sanitize activity.oldBoardName) (sanitize activity.boardName)}}}.

      if($eq activity.activityType 'archivedCard')
        | {{{_ 'activity-archived' cardLink}}}.

      if($eq activity.activityType 'restoredCard')
        | {{{_ 'activity-sent' cardLink boardLabelLink}}}.

      //- checklist activity --------------------------------------------------
      if($eq activity.activityType 'addChecklist')
        | {{{_ 'activity-checklist-added' cardLink}}}.
        if($eq mode 'card')
          .activity-checklist
            +viewer
              = activity.checklist.title
        else
          a.activity-checklist(href="{{ activity.card.originRelativeUrl }}")
            +viewer
              = activity.checklist.title

      if($eq activity.activityType 'removedChecklist')
        | {{{_ 'activity-checklist-removed' cardLink}}}.

      if($eq activity.activityType 'completeChecklist')
        | {{{_ 'activity-checklist-completed' (sanitize activity.checklist.title) cardLink}}}.

      if($eq activity.activityType 'uncompleteChecklist')
        | {{{_ 'activity-checklist-uncompleted' (sanitize activity.checklist.title) cardLink}}}.

      if($eq activity.activityType 'checkedItem')
        | {{{_ 'activity-checked-item' (sanitize checkItem) (sanitize activity.checklist.title) cardLink}}}.

      if($eq activity.activityType 'uncheckedItem')
        | {{{_ 'activity-unchecked-item' (sanitize checkItem) (sanitize activity.checklist.title) cardLink}}}.

      if($eq activity.activityType 'addChecklistItem')
        | {{{_ 'activity-checklist-item-added' (sanitize activity.checklist.title) cardLink}}}.
        .activity-checklist(href="{{ activity.card.originRelativeUrl }}")
          +viewer
            = activity.checklistItem.title

      if($eq activity.activityType 'removedChecklistItem')
        | {{{_ 'activity-checklist-item-removed' (sanitize activity.checklist.title) cardLink}}}.

      //- comment activity ----------------------------------------------------
      if($eq mode 'card')
        //- if we are in card mode we display the comment in a way that it
        //- can be edited by the owner
        if($eq activity.activityType 'addComment')
          +inlinedForm(classNames='js-edit-comment')
            +editor(autofocus=true)
              = activity.comment.text
            .edit-controls
              button.primary(type="submit") {{_ 'edit'}}
              .fa.fa-times-thin.js-close-inlined-form
          else
            .activity-comment
              +viewer
                = activity.comment.text
            +commentReactions(reactions=activity.comment.reactions commentId=activity.comment._id)
            span(title=activity.createdAt).activity-meta {{ moment activity.createdAt }}
              if($eq currentUser._id activity.comment.userId)
                +editOrDeleteComment
              else if currentUser.isBoardAdmin
                +editOrDeleteComment

        if($eq activity.activityType 'deleteComment')
          | {{{_ 'activity-deleteComment' activity.commentId}}}.

        if($eq activity.activityType 'editComment')
          | {{{_ 'activity-editComment' activity.commentId}}}.
      else
        //- if we are not in card mode we only display a summary of the comment
        if($eq activity.activityType 'addComment')
          | {{{_ 'activity-on' cardLink}}}
          a.activity-comment(href="{{ activity.card.originRelativeUrl }}")
            +viewer
              = activity.comment.text

      //- date activity ------------------------------------------------
      if($eq activity.activityType 'a-receivedAt')
        | {{{_ 'activity-receivedDate' (sanitize receivedDate) cardLink}}}.

      if($eq activity.activityType 'a-startAt')
        | {{{_ 'activity-startDate' (sanitize startDate) cardLink}}}.

      if($eq activity.activityType 'a-dueAt')
        | {{{_ 'activity-dueDate' (sanitize dueDate) cardLink}}}.

      if($eq activity.activityType 'a-endAt')
        | {{{_ 'activity-endDate' (sanitize endDate) cardLink}}}.

      //- customField activity ------------------------------------------------
      if($eq activity.activityType 'createCustomField')
        | {{_ 'activity-customfield-created' customField}}.

      if($eq activity.activityType 'setCustomField')
        | {{{_ 'activity-set-customfield' (sanitize lastCustomField) (sanitize lastCustomFieldValue) cardLink}}}.

      if($eq activity.activityType 'unsetCustomField')
        | {{{_ 'activity-unset-customfield' (sanitize lastCustomField) cardLink}}}.

      //- label activity ------------------------------------------------------
      if($eq activity.activityType 'addedLabel')
        | {{{_ 'activity-added-label' (sanitize lastLabel) cardLink}}}.

      if($eq activity.activityType 'removedLabel')
        | {{{_ 'activity-removed-label' (sanitize lastLabel) cardLink}}}.

      //- list activity -------------------------------------------------------
      if($neq mode 'card')
        if($eq activity.activityType 'createList')
          | {{{_ 'activity-added' (sanitize listLabel) boardLabelLink}}}.

        if($eq activity.activityType 'importList')
          | {{{_ 'activity-imported' (sanitize listLabel) boardLabelLink sourceLink}}}.

        if($eq activity.activityType 'removeList')
          | {{{_ 'activity-removed' (sanitize activity.title) boardLabelLink}}}.

        if($eq activity.activityType 'archivedList')
          | {{_ 'activity-archived' (sanitize listLabel)}}.

      //- member activity ----------------------------------------------------
      if($eq activity.activityType 'joinMember')
        if($eq user._id activity.member._id)
          | {{{_ 'activity-joined' cardLink}}}.
        else
          | {{{_ 'activity-added' memberLink cardLink}}}.

      if($eq activity.activityType 'unjoinMember')
        if($eq user._id activity.member._id)
          | {{{_ 'activity-unjoined' cardLink}}}.
        else
          | {{{_ 'activity-removed' memberLink cardLink}}}.

      //- swimlane activity --------------------------------------------------
      if($eq activity.activityType 'createSwimlane')
        | {{{_ 'activity-added' (sanitize activity.swimlane.title) boardLabelLink}}}.

      if($eq activity.activityType 'archivedSwimlane')
        | {{{_ 'activity-archived' (sanitize activity.swimlane.title)}}}.


      //- I don't understand this part ----------------------------------------
      if(currentData.timeKey)
        | {{_ activity.activityType }}
        = ' '
        i(title=currentData.timeValue).activity-meta {{ moment currentData.timeValue 'LLL' }}
        if (currentData.timeOldValue)
          = ' '
            | {{{_ "previous_as" }}}
            = ' '
            i(title=currentData.timeOldValue).activity-meta {{ moment currentData.timeOldValue 'LLL' }}
        = ' @'
      else if(currentData.timeValue)
        | {{_ activity.activityType currentData.timeValue}}

      span(title=activity.createdAt).activity-meta {{ moment activity.createdAt }}
