# one time init stuff for the /client view

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

context.JK.ClientInit = class ClientInit
  constructor: () ->
    @gearUtils = context.JK.GearUtils
    @ALERT_NAMES = context.JK.ALERT_NAMES;
    @lastCheckedBroadcast = null

  init: () =>
    if context.gon.isNativeClient
      this.nativeClientInit()

    context.JK.onBackendEvent(@ALERT_NAMES.WINDOW_OPEN_FOREGROUND_MODE, 'client_init', @watchBroadcast);

    this.watchBroadcast()

    if context.jamClient.RegisterSessionJoinLeaveRequestCallBack?
      context.jamClient.RegisterSessionJoinLeaveRequestCallBack("SessionStore.handleJoinLeaveRequestCallback")

  checkBroadcast: () =>
    promise = window.BroadcastActions.load.trigger()
    if promise
      promise.catch(() ->
        false
      )


  watchBroadcast: () =>
    if context.JK.currentUserId
      # create a 15 second buffer to not check too fast for some reason (like the client firing multiple foreground events)
      if !@lastCheckedBroadcast? || @lastCheckedBroadcast.getTime() < new Date().getTime() - 15000
        @lastCheckedBroadcast = new Date()
        setTimeout(@checkBroadcast, 3000)


  nativeClientInit: () =>
    @gearUtils.bootstrapDefaultPlaybackProfile();

    window.VideoActions.checkPromptConfigureVideo()


