context = window
teacherActions = window.JK.Actions.Teacher

@PostProcessorMixin = {

  postProcessLesson: (lesson) ->

    if lesson.student_id == context.JK.currentUserId
      me = lesson.student
      other = lesson.teacher
      lesson.isStudent = true
      lesson.isTeacher = false
      lesson.hasUnreadMessages = lesson['student_unread_messages']
    else
      me = lesson.teacher
      other = lesson.student
      lesson.isStudent = false
      lesson.isTeacher = true
      lesson.hasUnreadMessages = lesson['teacher_unread_messages']

    lesson.me = me
    lesson.other = other
    lesson.isAdmin = context.JK.currentUserAdmin
    lesson.noSchoolOnSchoolPayment = !lesson['payment_if_school_on_school?']

    lesson.cardNotOk =  !lesson.lesson_booking.card_presumed_ok && lesson.payment_if_school_on_school?
    lesson.isActive = lesson['is_active?']
    if (lesson.status == 'requested' || lesson.status == 'countered' || lesson.lesson_booking['is_countered?'])
      lesson.isRequested = true
      if lesson.cardNotOk
        lesson.displayStatus = 'No Card'
      else
        lesson.displayStatus = 'Requested'
    if lesson['is_active?'] && (lesson.status == 'approved' && !lesson.lesson_booking['is_countered?'])
      lesson.isScheduled = true
      lesson.displayStatus = 'Scheduled'

    if !lesson.displayStatus?
      if lesson.status == 'unconfirmed'
        lesson.displayStatus = 'Unconfirmed'
      else if lesson.status == 'canceled'
        lesson.displayStatus = 'Canceled'
        if lesson.canceled_by_admin
          lesson.displayStatus = 'Canceled (by Admin)'
        else if lesson.student_canceled
          lesson.displayStatus = 'Canceled (by Student)'
        else if lesson.teacher_canceled
          lesson.displayStatus = 'Canceled (by Teacher)'

      else if lesson.status == 'suspended'
        lesson.displayStatus = 'Suspended'
      else
          lesson.missed = true
          lesson.missedRole = 'the teacher'
          lesson.missedUser = lesson.teacher

          if lesson.success
            lesson.missed = false
            lesson.missedRole = null
            lesson.displayStatus = 'Completed'
          else
            if lesson.analysis?.teacher_analysis?.missed && lesson.analysis?.student_analysis?.missed
              lesson.missedRole = 'both student and teacher'
              lesson.missedUser = lesson.teacher
              lesson.displayStatus = 'Missed (by Both)'
            else if lesson.analysis?.teacher_analysis?.missed
              lesson.missedRole = 'the teacher'
              lesson.missedUser = lesson.teacher
              lesson.displayStatus = 'Missed (by Teacher)'
            else if lesson.analysis?.student_analysis?.missed
              lesson.missedRole = 'the student'
              lesson.missedUser = lesson.student
              lesson.displayStatus = 'Missed (by Student)'
            else
              lesson.displayStatus = 'Missed'

    @postProcessUser(me)
    @postProcessUser(other)

  postProcessUser: (user) ->
    if !user.photo_url?
      user.resolved_photo_url = '/assets/shared/avatar_generic.png'
    else
      user.resolved_photo_url = user.photo_url
    if user.teacher?
      user.teacher_profile = '/client#/profile/teacher/' + user.id
      user.best_profile = user.teacher_profile
    else
      user.musician_profile = '/client#/profile/' + user.id
      user.best_profile = user.musician_profile

    if user.is_a_teacher && user.teacher?
      cheapest_lesson_stmt = '(no pricing set yet)'
      lowestPrice = null
      lowestDuration = null
      single = true
      enabledMinutes = []
      for minutes in [30, 45, 60, 90, 120]
        duration_enabled = user.teacher["lesson_duration_#{minutes}"]

        if duration_enabled
          enabledMinutes.push(minutes)
      if user.teacher.prices_per_lesson
        for minutes in enabledMinutes
          lesson_price = user.teacher["price_per_lesson_#{minutes}_cents"]
          if lesson_price?
            if !lowestPrice? ||  lesson_price < lowestPrice
              lowestPrice = lesson_price
              single = true
              lowestDuration = minutes
        for minutes in enabledMinutes
          lesson_price = user.teacher["price_per_month_#{minutes}_cents"]
          if lesson_price?
            if !lowestPrice? ||  (lesson_price / 4) < lowestPrice
              lowestPrice = lesson_price / 4
              single = false
              lowestDuration = minutes

      if lowestPrice?
        if single
          # lowest price appears to be a single lesson
          cheapest_lesson_stmt = "$#{lowestPrice / 100} for #{lowestDuration} minutes"
        else
          # lowest price appears to be a monthly recurring lesson
          cheapest_lesson_stmt = "$#{Math.round(lowestPrice * 4) / 100} per month"

      user.cheapest_lesson_stmt = cheapest_lesson_stmt
}