context = window

@InLessonBroadcast = React.createClass({
  displayName: 'In Lesson Broadcast'

  getTimeRemaining: (t) ->

    if t < 0
      t = -t

    seconds = Math.floor( (t/1000) % 60 );
    minutes = Math.floor( (t/1000/60) % 60 );
    hours = Math.floor( (t/(1000*60*60)) % 24 );
    days = Math.floor( t/(1000*60*60*24) );

    return {
    'total': t,
    'days': days,
    'hours': hours,
    'minutes': minutes,
    'seconds': seconds
    };


  displayTime: () ->
    if @props.lessonSession.initialWindow
      # offset time by 10 minutes to get the 'you need to wait message' in
      untilTime = @getTimeRemaining(@props.lessonSession.until.total + (10 * 60 * 1000))
    else
      untilTime = @props.lessonSession.until
    timeString = ''
    if untilTime.days != 0
      timeString += "#{untilTime.days} days, "
    if untilTime.hours != 0 || timeString.length > 0
      timeString += "#{untilTime.hours} hours, "
    if untilTime.minutes != 0 || timeString.length > 0
      timeString += "#{untilTime.minutes} minutes, "
    if untilTime.seconds != 0 || timeString.length > 0
      timeString += "#{untilTime.seconds} seconds"

    if timeString == ''
      'now!'
    timeString

  render: () ->
    if @props.lessonSession.isStudent
      role = 'student'
      otherRole = 'teacher'
      billingStatement = 'charged for the lesson'
    else
      role = 'teacher'
      otherRole = 'student'
      billingStatement = 'not receive payment for the lesson'
    if @props.lessonSession.completed
      if @props.lessonSession.success
        content = `<div className="message">
          <p>This lesson is over.</p>
        </div>`
      else
        content = `<div className="message">
          <p>This lesson is over, but will not be billed.</p>
        </div>`
    else if @props.lessonSession.beforeSession
      content = `<div className="message">
        <p>This lesson will start in:</p>
        <p className="time">{this.displayTime()}</p>
      </div>`
    else if @props.lessonSession.initialWindow
      content = `<div className="message">
        <p>You need to wait in this session for</p>
        <p className="time">{this.displayTime()}</p>
        <p>to allow time for your {otherRole} to join you. If you leave before this timer reaches zero, and your {otherRole} joins this session, you will be marked absent and {billingStatement}.</p>
      </div>`
    else if @props.lessonSession.teacherFault
      if @props.lessonSession.isStudent
        content = `<div className="message">
          <p>You may now leave the session.</p>
          <p>Your teacher will be marked absent and penalized for missing the lesson. You will not be charged for this lesson.</p>
          <p>We apologize for your inconvenience, and we will work to remedy this situation.</p>
        </div>`
      else
        content = `<div className="message">
          <p>You may now leave the session.</p>
          <p>Your student will be marked absent and penalized for missing the lesson. You will still received payment for this lesson.</p>
          <p>We apologize for your inconvenience, and we will work to remedy this situation.</p>
        </div>`

    if content?
      `<div className="broadcast-notification lesson">
        {content}
      </div>`
    else
      null

})
