#
# Common utility functions.
#

$ = jQuery
context = window
context.JK ||= {};

class RecordingUtils
  constructor: () ->
    @logger = context.JK.logger

  init: () =>
    @templateHoverMix = $('#template-sync-viewer-hover-mix')

  mixStateErrorDescription: (mix) =>
    summary = ''
    if mix.error.error_reason == 'mix-timeout'
      summary = "The mix was started, but it has taken too long to for it to finish. The mix was started #{$.timeago(mix.error.error_detail)}. JamKazam employees have been notified of the issue."
    else if mix.error.error_reason == 'unknown'
      summary = "The reason the mix failed is unknown.  JamKazam employees have been notified of the issue. "
    else if mix.error.error_reason == 'unable to download'
      summary = "The mixer could not fetch one of the tracks from JamKazam servers.  JamKazam employees have been notified of the issue."
    else if mix.error.error_reason == 'unable-parse-error-out'
      summary = "The mixer failed, but it could not report what went wrong.  JamKazam employees have been notified of the issue."
    else if mix.error.error_reason == 'postback-ogg-mix-to-s3' or mix.error_reason == 'postback-mp3-mix-to-s3'
      summary = "The mixer made the mix, but could not publish it.  JamKazam employees have been notified of the issue."
    else if mix.error.error_reason == 'unhandled-job-exception'
      summary = "The mixer had an error while mixing.  JamKazam employees have been notified of the issue."
    else if mix.error.error_reason == 'unhandled-job-exception'
      summary = "The mixer had an error while mixing.  JamKazam employees have been notified of the issue."
    summary

  mixStateDefinition: (mixState) =>

    definition = switch mixState
      when 'still-uploading' then "STILL UPLOADING means you or others in the recording have not uploaded enough information yet to make a mix."
      when 'stream-mix' then "STREAM MIX means a user's real-time mix is available to listen to."
      when 'discarded' then "DISCARDED means you chose to not keep this recording when the recording was over."
      when 'mixed' then "MIXED means all tracks have been uploaded, and a final mix has been made."
      when 'mixing' then "MIXING means all tracks have been uploaded, and the final mix is currently being made."
      when 'waiting-to-mix' then "MIX QUEUED means all tracks have been uploaded, and the final mix should start being made very soon."
      when 'error' then 'MIX FAILED means something went wrong with the mixer after all tracks were uploaded.'
      else 'There is no help for this state'

  createMixInfo: (mix) =>
    mixStateMsg = 'UNKNOWN'
    mixStateClass = 'unknown'
    mixState = 'unknown'

    if mix? and mix.state? and !mix.fake

      # the '.download' property is only used for personalized views of a mix.
      if !mix.download? mix.download.should_download

        if mix.state == 'error'
          mixStateMsg = 'MIX FAILED'
          mixStateClass = 'error'
          mixState = 'error'
        else if mix.state == 'stream-mix'
          mixStateMsg = 'STREAM MIX'
          mixStateClass = 'stream-mix'
          mixState = 'stream-mix'
        else if mix.state == 'waiting-to-mix'
          mixStateMsg = 'MIX QUEUED'
          mixStateClass = 'waiting-to-mix'
          mixState = 'waiting-to-mix'
        else if mix.state == 'mixing'
          mixStateMsg = 'MIXING'
          mixStateClass = 'mixing'
          mixState = 'mixing'
        else if mix.state == 'mixed'
          mixStateMsg = 'MIXED'
          mixStateClass = 'mixed'
          mixState = 'mixed'
        else
          mixStateMsg = mix.state
          mixStateClass = 'unknown'
          mixState = 'unknown'


      else
        mixStateMsg = 'DISCARDED'
        mixStateClass = 'discarded'
        mixState = 'discarded'
    else

      if mix.fake and mix.discarded
        mixStateMsg = 'DISCARDED'
        mixStateClass = 'discarded'
        mixState = 'discarded'
      else
        mixStateMsg = 'STILL UPLOADING'
        mixStateClass = 'still-uploading'
        mixState = 'still-uploading'

    result = {
      mixStateMsg: mixStateMsg,
      mixStateClass: mixStateClass,
      mixState: mixState,
      isError: mixState == 'error'
    }

    result

  onMixHover: () ->
    $mix = $(this).closest('.mix')
    mixStateInfo = $mix.data('mix-state')
    viewContext = $mix.data('view-context')
    if !mixStateInfo? # to support feed and syncs both using this same code, check .mix-state too
      $mixState = $mix.find('.mix-state')
      mixStateInfo = $mixState.data('mix-state')

    if !mixStateInfo?
      logger.error('no mix-state anywhere', this)
      throw 'no mix-state'

    mixStateMsg = mixStateInfo.mixStateMsg
    mixStateClass = mixStateInfo.mixStateClass
    mixState = mixStateInfo.mixState

    serverInfo = $mix.data('server-info')

    # lie if this is a virtualized mix (i.e., mix is created after recording is made)
    if !serverInfo?
      mixState = 'still-uploading'
    else if serverInfo.fake
      mixState = if serverInfo.discarded then 'discarded' else 'still-uploading'

    summary = ''
    if mixState == 'still-uploading'
      #summary = "No one in the recording has uploaded any files yet.  Once tracks have been uploaded, a mix will be made."
    else if mixState == 'discarded'
      summary = "When this recording was made, you elected to not keep it.  But at least one other person elected to keep it so your tracks are needed for the mix."
    else if mixState == 'mixed'
      #summary = 'All tracks have been uploaded, and a final mix has been made.'
    else if mixState == 'stream-mix'
      summary = 'A STREAM MIX the mix that someone heard in their headphones as the actual recording was made.  In the case of a recording involving two or more people, it will contain any imperfections caused by the internet.  Once the final, high-quality mix is available, we will automatically replace the stream mix with it.'
    else if mixState == 'mixing'
      #summary = 'All tracks have been uploaded, and the final mix is currently being made.'
    else if mixState == 'waiting-to-mix'
      #summary = 'All tracks have been uploaded, and the finale mix should start being made very soon.'
    else if mixState == 'error'
      summary = context.JK.RecordingUtils.mixStateErrorDescription(serverInfo)

    mixStateDefinition = context.JK.RecordingUtils.mixStateDefinition(mixState)

    context._.template(context.JK.RecordingUtils.templateHoverMix.html(), {summary: summary, mixStateDefinition: mixStateDefinition, mixStateMsg: mixStateMsg, mixStateClass: mixStateClass, viewContext: viewContext}, {variable: 'data'})


# global instance
context.JK.RecordingUtils = new RecordingUtils()