$ = jQuery
context = window
logger = context.JK.logger
PLAYBACK_MONITOR_MODE = context.JK.PLAYBACK_MONITOR_MODE
RecordingActions = @RecordingActions

@MediaPlaybackStore = Reflux.createStore(
  {
    listenables: @MediaPlaybackActions

    playbackStateChanged: false
    positionUpdateChanged: false
    currentTimeChanged: false
    playbackState: null
    positionMs: 0
    durationMs: 0
    isRecording: false
    sessionHelper: null


    init: () ->
      this.listenTo(context.SessionStore, this.onSessionChanged);

    onCurrentTimeChanged: (time) ->
      @time = time
      @currentTimeChanged = true
      @issueChange()

    onSessionChanged: (session) ->
      @isRecording = session.isRecording
      @sessionHelper = session

    onMediaStartPlay: (data) ->
      logger.debug("calling jamClient.SessionStartPlay");
      context.jamClient.SessionStartPlay(data.playbackMode);

    onMediaStopPlay: (data) ->
      # if a JamTrack is open, and the user hits 'pause' or 'stop', we need to automatically stop the recording
      if @sessionHelper.jamTracks() && @isRecording
        logger.debug("preemptive jamtrack stop")
        @startStopRecording();

      if !data.endReached
        logger.debug("calling jamClient.SessionStopPlay. endReached:", data.endReached)
        context.jamClient.SessionStopPlay()

    onMediaPausePlay: (data) ->
      # if a JamTrack is open, and the user hits 'pause' or 'stop', we need to automatically stop the recording
      if @sessionHelper.jamTracks() && @isRecording
        logger.debug("preemptive jamtrack stop")
        @startStopRecording();


      if !data.endReached
        logger.debug("calling jamClient.SessionPausePlay. endReached:", data.endReached)
        context.jamClient.SessionPausePlay()

    startStopRecording: () ->
      if @isRecording
        RecordingActions.stopRecording.trigger()
      else
        RecordingActions.startRecording.trigger()

    onMediaChangePosition: (data) ->
      seek = data.positionMs;

      if data.playbackMonitorMode == PLAYBACK_MONITOR_MODE.JAMTRACK
        # if positionMs == 0, then seek it back to whatever the earliest play start is to catch all the prelude

        if(seek == 0)
          duration = context.jamClient.SessionGetJamTracksPlayDurationMs();
          seek = duration.start;

      logger.debug("calling jamClient.SessionTrackSeekMs(" + seek + ")");

      if data.playbackMonitorMode == PLAYBACK_MONITOR_MODE.JAMTRACK
        context.jamClient.SessionJamTrackSeekMs(seek);
      else
        context.jamClient.SessionTrackSeekMs(seek);


    issueChange: () ->

      @state =
        playbackState: @playbackState
        playbackStateChanged: @playbackStateChanged
        positionUpdateChanged: @positionUpdateChanged
        currentTimeChanged: @currentTimeChanged
        positionMs: @positionMs
        durationMs: @durationMs
        isPlaying: @isPlaying
        time: @time

      this.trigger(@state)
      @playbackStateChanged = false
      @positionUpdateChanged = false
      @currentTimeChanged = false

    onPlaybackStateChange: (text) ->
      @playbackState = text
      @playbackStateChanged = true

      @issueChange()

    onPositionUpdate: (playbackMode) ->
      if playbackMode == PLAYBACK_MONITOR_MODE.JAMTRACK
        @positionMs = context.jamClient.SessionCurrrentJamTrackPlayPosMs()
        duration = context.jamClient.SessionGetJamTracksPlayDurationMs()
        @durationMs = duration.media_len
      else
        @positionMs = context.jamClient.SessionCurrrentPlayPosMs()
        @durationMs = context.jamClient.SessionGetTracksPlayDurationMs()

      @isPlaying = context.jamClient.isSessionTrackPlaying()

      @positionUpdateChanged = true
      @issueChange()

  }
)
