context = window
MIX_MODES = context.JK.MIX_MODES

SessionActions = @SessionActions


@SessionScreen = React.createClass({

  mixins: [Reflux.listenTo(@AppStore,"onAppInit"), Reflux.listenTo(@SessionActions.allowLeaveSession, "onAllowLeaveSession"), Reflux.listenTo(@SessionStore,"onSessionStoreChanged")]


  getInitialState: () ->
    {isLesson: false, lessonId: null}

  render: () ->

    videoBtn = null
    filesBtn = null

    if gon.global.video_available != 'none'
      videoBtn = `<SessionVideoBtn />`

    filesBtn = `<SessionFilesBtn app={this.app} lessonId={this.state.lessonId} isLesson={this.state.isLesson}/>`

    `<div className="session-container">
      <div className="in-session-controls">
        <SessionSettingsBtn />
        <SessionVolumeSettingsBtn />
        <SessionShareBtn />
        <SessionRecordBtn />
        {videoBtn}
        {filesBtn}
        <SessionResyncBtn />
        <SessionLeaveBtn />
      </div>
      <div className="tracks" id="new-tracks">
        <SessionMyTracks mode={MIX_MODES.MASTER} />
        <SessionOtherTracks mode={MIX_MODES.PERSONAL} />
        <SessionMediaTracks mode={MIX_MODES.PERSONAL} />
        <SessionNotifications />
      </div>
    </div>`

  componentDidMount: () ->
    @logger = context.JK.logger

  beforeShow: (data) ->
    @logger.debug("session beforeShow")
    @allowLeave = false

  afterShow: (data) ->
    @logger.debug("session afterShow")

    SessionActions.joinSession.trigger(data.id)

  beforeHide: () ->
    context.JK.HelpBubbleHelper.clearJamTrackGuide();

  beforeLeave: (data) ->
    @logger.debug("session beforeLeave", @allowLeave)

    if @allowLeave
      return true
    else
      leaveSessionWarningDialog = new context.JK.LeaveSessionWarningDialog(context.JK.app,
        () =>
          @allowLeave = true
          SessionActions.leaveSession(hash: data.hash)
          #context.location.hash = data.hash
      )

      leaveSessionWarningDialog.initialize()
      @app.layout.showDialog('leave-session-warning')
      return false

  beforeDisconnect: () ->
    @logger.debug("session beforeDisconnect")
    return { freezeInteraction: true };

  onSessionStoreChanged: (session) ->
    this.setState({isLesson: session.isLesson, lessonId: session.lessonId, sessionId: session.id})
  onAllowLeaveSession: () ->
    @allowLeave = true

  onAppInit: (@app) ->

    screenBindings = {
      'beforeShow': @beforeShow,
      'afterShow': @afterShow,
      'beforeHide': @beforeHide,
      'beforeLeave' : @beforeLeave,
      'beforeDisconnect' : @beforeDisconnect,
    };

    @app.bindScreen('session', screenBindings);
})
