class VideoRoomChannel < ApplicationCable::Channel
  def subscribed
    @video_room = find_video_room
    stream_for @video_room
    broadcast_to @video_room, { type: 'ping', from: params[:client_id] }
  end

  def unsubscribed
    Turbo::StreamsChannel.broadcast_remove_to(
      find_video_room,
      target: "medium_#{current_client.id}"
    )
  end

  def greet(data)
    Turbo::StreamsChannel.broadcast_append_to(
      data['to'],
      target: 'media',
      partial: 'media/medium',
      locals: { client_id: data['from'] }
    )
  end

  private

  def find_video_room
    VideoRoom.new(id: params[:id])
  end
end
