class ApiChatsController < ApiController

  before_filter :api_signed_in_user, :check_session

  respond_to :json

  def create
    @chat_msg = ChatMessage.new
    @chat_msg.user_id = current_user.id
    @chat_msg.music_session_id = @music_session.id
    @chat_msg.message = params[:message]

    if @chat_msg.save
      ChatMessage.send_chat_msg @music_session, @chat_msg, current_user, params[:client_id]
    end

    respond_with_model(@chat_msg)
  end

  def index
    data = ChatMessage.index(current_user, params)
    @chats = data[0]
    @next = data[1]
    render "api_chats/index", :layout => nil
  end

  def check_session
    @music_session = ActiveMusicSession.find(params[:music_session])
    if @music_session.nil?
      raise ArgumentError, 'specified session not found'
    end

    unless @music_session.access? current_user
      raise JamPermissionError, 'not allowed to join the specified session'
    end
  end

end