if Backbone?
  class @ThreadResponseView extends DiscussionContentView
    tagName: "li"

    events:
        "click .vote-btn": "toggleVote"
        "submit .comment-form": "submitComment"
        "click .action-endorse": "toggleEndorse"
        "click .action-delete": "delete"

    render: ->
      @template = _.template($("#thread-response-template").html())
      @$el.html(@template(@model.toJSON()))
      @initLocal()
      @delegateEvents()
      if window.user.voted(@model)
        @$(".vote-btn").addClass("is-cast")
      @renderAttrs()
      @$el.find(".posted-details").timeago()
      @convertMath()
      @markAsStaff()
      @renderComments()
      @

    convertMath: ->
      element = @$(".response-body")
      element.html DiscussionUtil.postMathJaxProcessor DiscussionUtil.markdownWithHighlight element.html()
      MathJax.Hub.Queue ["Typeset", MathJax.Hub, element[0]]

    markAsStaff: ->
      if DiscussionUtil.isStaff(@model.get("user_id"))
        @$el.addClass("staff")
        @$el.prepend('<div class="staff-banner">staff</div>')

    renderComments: ->
      comments = new Comments()
      comments.comparator = (comment) ->
        comment.get('created_at')
      collectComments = (comment) ->
        comments.add(comment)
        children = new Comments(comment.get('children'))
        children.each (child) ->
          child.parent = comment
          collectComments(child)
      @model.get('comments').each collectComments
      comments.each (comment) => @renderComment(comment, false, null)

    renderComment: (comment) =>
      comment.set('thread', @model.get('thread'))
      view = new ResponseCommentView(model: comment)
      view.render()
      @$el.find(".comments li:last").before(view.el)
      view

    toggleVote: (event) ->
      event.preventDefault()
      @$(".vote-btn").toggleClass("is-cast")
      if @$(".vote-btn").hasClass("is-cast")
        @vote()
      else
        @unvote()

    vote: ->
      url = @model.urlFor("upvote")
      @$(".votes-count-number").html(parseInt(@$(".votes-count-number").html()) + 1)
      DiscussionUtil.safeAjax
        $elem: @$(".discussion-vote")
        url: url
        type: "POST"
        success: (response, textStatus) =>
          if textStatus == 'success'
            @model.set(response)

    unvote: ->
      url = @model.urlFor("unvote")
      @$(".votes-count-number").html(parseInt(@$(".votes-count-number").html()) - 1)
      DiscussionUtil.safeAjax
        $elem: @$(".discussion-vote")
        url: url
        type: "POST"
        success: (response, textStatus) =>
          if textStatus == 'success'
            @model.set(response)

    submitComment: (event) ->
      event.preventDefault()
      url = @model.urlFor('reply')
      body = @$(".comment-form-input").val()
      if not body.trim().length
        return
      comment = new Comment(body: body, created_at: (new Date()).toISOString(), username: window.user.get("username"), user_id: window.user.get("id"), id:"unsaved")
      view = @renderComment(comment)
      @trigger "comment:add", comment
      @$(".comment-form-input").val("")

      DiscussionUtil.safeAjax
        $elem: $(event.target)
        url: url
        type: "POST"
        dataType: 'json'
        data:
          body: body
        success: (response, textStatus) ->
          comment.set(response.content)
          view.render() # This is just to update the id for the most part, but might be useful in general
    delete: (event) ->
      event.preventDefault()
      if not @model.can('can_delete')
        return
      console.log $(event.target)
      url = @model.urlFor('delete')
      if not confirm "Are you sure to delete this response? "
        return
      @model.remove()
      @$el.remove()
      $elem = $(event.target)
      DiscussionUtil.safeAjax
        $elem: $elem
        url: url
        type: "POST"
        success: (response, textStatus) =>

    toggleEndorse: (event) ->
      event.preventDefault()
      if not @model.can('can_endorse')
        return
      $elem = $(event.target)
      url = @model.urlFor('endorse')
      endorsed = @model.get('endorsed')
      data = { endorsed: not endorsed }
      @model.set('endorsed', not endorsed)
      @trigger "comment:endorse", not endorsed
      DiscussionUtil.safeAjax
        $elem: $elem
        url: url
        data: data
        type: "POST"
