context = window
ChannelGroupIds = context.JK.ChannelGroupIds
MIDI_TRACK = context.JK.MIDI_TRACK


@SessionMyTracksMixin = {

  onInputsChanged: (sessionMixers) ->

    @sessionMixers = sessionMixers

    @recompute()

  onConfigureTracksChanged: (configureTracks) ->

    @configureTracks = configureTracks

    @recompute()

  recompute: () ->
    return if !@sessionMixers?

    session = @sessionMixers.session
    mixers = @sessionMixers.mixers

    tracks = []

    if session.inSession()
      participant = session.getParticipant(@app.clientId)

      connStatsClientId = @app.clientId

      if participant.client_role == 'child' && participant.parent_client_id?
        participant.parent = session.getParticipant(participant.parent_client_id)
        connStatsClientId = participant.parent_client_id

      if participant
        photoUrl = context.JK.resolveAvatarUrl(participant.user.photo_url);

        chatMixer = mixers.chatMixer
        chat = null
        if chatMixer
          chat =
            mixers: chatMixer
            mode: @props.mode
            photoUrl: photoUrl

        name = participant.user.name;

        for track in participant.tracks
          # try to find mixer info for this track
          mixerFinder = [participant.client_id, track, true] # so that other callers can re-find their mixer data
          mixerData = mixers.findMixerForTrack(participant.client_id, track, true, @props.mode)

          hasMixer = mixerData.mixer?

          # todo: sessionModel.setAudioEstablished

          instrumentIcon = context.JK.getInstrumentIcon45(track.instrument_id);

          trackName = "#{name}: #{track.instrument}"

          no_pan = false
          associatedVst = null

          # find any VST info
          if hasMixer && @configureTracks?

            # bug in the backend; track is wrong for personal mixers (always 1), but correct for master mix
            trackAssignment = -1
            if @props.mode == context.JK.MIX_MODES.MASTER
              if mixerData.mixer.group_id == ChannelGroupIds.MidiInputMusicGroup
                trackAssignment = MIDI_TRACK
              else
                trackAssignment = mixerData.mixer.track
            else
              if mixerData.mixer.group_id == ChannelGroupIds.MidiInputMusicGroup
                trackAssignment = MIDI_TRACK
              else
                trackAssignment = mixerData.oppositeMixer?.track

            for vst in @configureTracks.vstTrackAssignments.vsts
              if vst.track == trackAssignment - 1 && vst.name != 'NONE'
                logger.debug("found VST on track", vst, track)
                associatedVst = vst
                break

          tracks.push({track: track, mixerFinder: mixerFinder, mixers: mixerData, hasMixer:hasMixer, name: name, trackName: trackName, instrumentIcon: instrumentIcon, photoUrl: photoUrl, clientId: participant.client_id, associatedVst: associatedVst, connStatsClientId: connStatsClientId})
      else
        logger.warn("SessionMyTracks: unable to find participant")

    this.setState(tracks: tracks, mySession:session, chat: chat)


}