context = window

@SessionHelper = class SessionHelper

  constructor: (app, session, participantsEverSeen, isRecording, downloadingJamTrack, preppingVstEnable, sessionRules, subscriptionRules) ->
    @app = app
    @session = session
    @participantsEverSeen = participantsEverSeen
    @isRecording = isRecording
    @downloadingJamTrack = downloadingJamTrack
    @preppingVstEnable = preppingVstEnable
    @isLesson = @session?.lesson_session?
    @sessionRules = sessionRules
    @subscriptionRules = subscriptionRules
    if @isLesson
      @lessonId = @session.lesson_session.id

  inSession: () ->
    @session?



  participants: () ->
    if @session
      return @session.participants
    else
      []

  users: () ->
    found = {}

    for participant in @participants()
      found[participant.user.id] = participant.user

    found

  findParticipantByUserId: (userId) ->
    foundParticipant = null
    for participant in @participants()
      if participant.user.id == userId
        foundParticipant = participant
        break

    foundParticipant


  otherParticipants: () ->
    others = []
    for participant in @participants()
      myTrack = @app.clientId == participant.client_id

      others.push(participant) unless myTrack
    others

  sessionController: () ->
    info = {}

    # XXX testing:
    info["can_control"] = false
    info["session_controller"] = @participants()[0].user

    if @session
      if @session.session_controller_id == null
        info['session_controller'] = null
        info['can_control'] = true
      else
        for participant in @participants()
          if participant.user.id == @session.session_controller_id
            info['session_controller'] = participant.user
            info['can_control'] = participant.user.id == context.JK.currentUserId
            break

    info

  # if any participant has the metronome open, then we say this session has the metronome open
  isMetronomeOpen: () ->
    @session? && @session.metronome_active

  isPlayingRecording: () ->
    # this is the server's state; there is no guarantee that the local tracks
    # requested from the backend will have corresponding track information
    return !!(@session && @session.claimed_recording);

  recordedTracks: () ->
    if @session && @session.claimed_recording
      @session.claimed_recording.recording.recorded_tracks
    else
      null

  recordedBackingTracks: () ->
    if @session && @session.claimed_recording
      @session.claimed_recording.recording.recorded_backing_tracks
    else
      null

  backingTracks: () ->
    backingTracks = []
    # this may be wrong if we loosen the idea that only one person can have a backing track open.
    # but for now, the 1st person we find with a backing track open is all there is to find...

    for participant in @participants()
      if participant.backing_tracks.length > 0
        backingTracks = participant.backing_tracks
        break

    backingTracks

  backingTrack: () ->
    result = null
    if @session
      # TODO: objectize this for VRFS-2665, VRFS-2666, VRFS-2667, VRFS-2668
      result =
        path: @session.backing_track_path
    result

  jamTracks: () ->
    if @session && @session.jam_track
      @session.jam_track.tracks.filter((track)->
        track.track_type == 'Track' || track.track_type == 'Click'
      )
    else
      null

  jamTrackMixdown: () ->
    { id: @session?.jam_track?.mixdown.id }

  jamTrackName: () ->
    @session?.jam_track?.name

  recordedJamTracks:() ->
    if @session && @session.claimed_recording
      @session.claimed_recording.recording.recorded_jam_track_tracks
    else
      null

  recordedJamTrackName: () ->
    jam_track = @session?.claimed_recording?.recording?.jam_track

    if jam_track? then jam_track.name  else null

  recordingName: () ->
    @session?.claimed_recording?.name

  getParticipant: (clientId) ->
    found = null
    for participant in @participants()
      if participant.client_id == clientId
        found = participant
        break

    logger.warn('unable to find participant with clientId: ' + clientId) unless found
    found

  id: () ->
    @session.id