
module JamRuby
  class Broadcast < ActiveRecord::Base

    @@log = Logging.logger[Broadcast]

    STATUS_COMPLETED = 'complete'
    STATUS_ABANDONED = 'abandoned'
    STATUS_REVOKED   = 'revoked'
    STATUS_DELETED =   'deleted'

    DONE_STATUSES = [STATUS_ABANDONED, STATUS_REVOKED, STATUS_DELETED, STATUS_COMPLETED]
    belongs_to :music_session, :class_name => 'JamRuby::MusicSession'

    def self.current_broadcast(music_session)
      Broadcast.where(music_session_id: music_session.id).where('broadcast_status not in (?)',  Broadcast::DONE_STATUSES).first
    end

    def self.unlink_broadcast(music_session)
      broadcast = current_broadcast(music_session)
      if broadcast
        broadcast.broadcast_status = STATUS_DELETED
        broadcast.save
      end
    end

    # data should be JSON hash from google API
    def update_broadcast_data(data)
      self.broadcast_status = data["status"]["lifeCycleStatus"]
      self.broadcast_data = data.to_json
    end
  end
end
