# TODO: Need to unsubscribe as project changes
$(document).on "turbolinks:load", ->
  # Only connect if a project-id meta tag is present
  return unless meta = document.querySelector("meta[name=project-id]")
  # Get the id of the project to subscribe to
  projectId = meta.getAttribute("content")

  App.projects = App.cable.subscriptions.create { channel: "ProjectsChannel", id: projectId },
    connected: ->
      # Called when the subscription is ready for use on the server

    disconnected: ->
      # Called when the subscription has been terminated by the server

    received: (data) ->
      switch data.event
        when "video_status_update" then @showVideoStatusUpdate(data.content)
        when "download_status_update" then @showDownloadStatusUpdate(data.content)
        when "conference_recording_ready" then @showDownloadStatusUpdate(data.content)

    showVideoStatusUpdate: (content) ->
      $("[data-ujs-target='video-analysis-msg']").replaceWith content

    showDownloadStatusUpdate: (content) ->
      $(".flash-message").html content
