context = window
SessionActions = @SessionActions
rest = context.JK.Rest()

@SessionVideoBtn = React.createClass({

  mixins: [
    Reflux.listenTo(UserStore, "onUserChanged")
  ]

  useVideoConferencingServer: () ->
      context.SessionStore.currentSession.use_video_conferencing_server

  onUserChanged: (userState) ->
    @setState({user: userState?.user})
    
  openBrowserToPayment: () ->
    context.JK.popExternalLink("/client#/account/subscription", true)

  openBrowserToPlanComparison: () ->
    context.JK.popExternalLink("https://jamkazam.freshdesk.com/support/solutions/articles/66000122535-what-are-jamkazam-s-free-vs-premium-features-")
    return 'noclose'

  openBrowserToNewVideoServer: () ->
    canVideo = window.SessionStore.canVideo()
    if canVideo
      rest.getVideoConferencingRoomUrl(context.SessionStore.id()).done((response) => context.JK.popExternalLink(response.url + '&audiooff=true'))
    else
      buttons = []
      buttons.push({name: 'CLOSE', buttonStyle: 'button-grey'})
      buttons.push({name: 'COMPARE PLANS', buttonStyle: 'button-grey', click: (() => (@openBrowserToPlanComparison()))})
      buttons.push({
        name: 'UPGRADE PLAN',
        buttonStyle: 'button-orange',
        click: (() => (@openBrowserToPayment()))
      })
      context.JK.Banner.show({
        title: "Your Current Plan Does Not Allow Video",
        html: context._.template($('#template-plan-no-video').html(), {}, { variable: 'data' }),
        buttons: buttons})


  sessionWebCam: (e) ->
    e.preventDefault();
   
    if @useVideoConferencingServer()
      @openBrowserToNewVideoServer()
    else
      @connectWithLegacyVideoServer()


  connectWithLegacyVideoServer: () ->
    canVideo = window.SessionStore.canVideo()

    if canVideo
      SessionActions.toggleSessionVideo()
    else
      buttons = []
      buttons.push({name: 'CLOSE', buttonStyle: 'button-grey'})
      buttons.push({name: 'COMPARE PLANS', buttonStyle: 'button-grey', click: (() => (@openBrowserToPlanComparison()))})
      buttons.push({
        name: 'UPGRADE PLAN',
        buttonStyle: 'button-orange',
        click: (() => (@openBrowserToPayment()))
      })
      context.JK.Banner.show({
        title: "Your Current Plan Does Not Allow Video",
        html: context._.template($('#template-plan-no-video').html(), {}, { variable: 'data' }),
        buttons: buttons})

  render: () ->
    `<a className="session-share session-video-btn button-grey-toggle left" onClick={this.sessionWebCam}>
      <img src="/assets/content/icon_video.png" align="texttop" height="14" width="14"/>
      VIDEO
    </a>`
})
