
class ApiLiveStreamsController < ApiController


  respond_to :json

  def log
    @log || Logging.logger[ApiLiveStreamsController]
  end


  def index
    @live_streams = LiveStream.upcoming

    render "api_live_streams/index", :layout => nil
  end

  def claim
    order = params[:order]
    if order.nil?
      render :json => {}, :status => 404, layout: nil
      return
    end

    order.strip!

    if order.start_with? "#"
      order = order[1..-1]
    end

    event_brite_order = EventBriteOrder.find_by_order_id(order)

    if event_brite_order.nil?
      render :json => {}, :status => 404, layout: nil
    else
      EventBriteOrder.where(id: event_brite_order.id).update_all(times_claimed: event_brite_order.times_claimed + 1)
      render :json => {event_id: event_brite_order.live_stream.id, order_id: event_brite_order.order_id, event_type: 'eventbrite', slug: event_brite_order.live_stream.slug}, :status => :ok, layout: nil
    end
  end

  def show
    slug = params[:slug]
    @live_stream = LiveStream.find_by_slug!(slug)
    render "api_live_streams/show", :layout => nil
  end

  def build_user_detail(user)

    detail = ''
    participant = "---------Participant: #{user.name}---------\n"
    detail += participant
    detail += "Email: #{user.email}\n"
    detail += "Location: #{user.location}\n"
    detail += "Profile: #{user.biography}\n"
    detail += "Admin URL: #{user.admin_url}\n"
    user.bands.each do | band |
      detail += "  Band: #{band.name}  #{band.website}\n"
      detail += "  Band Profile: #{band.biography}"
    end
    detail += ("-" * participant.length) + "\n"

    detail
  end

  def email_stream_event(subject, create_entity)
    begin
      music_session_id = params["music_session_id"]
      music_session = MusicSession.find(music_session_id)

      music_session_admin_url = ''
      if music_session
        music_session_admin_url = music_session.admin_url
      end
      participants = params["participants"]

      if !participants.nil? && participants.to_i < 2
        return
      end

      duration_sec = params["duration_sec"]
      if !duration_sec.nil? && duration_sec.to_i < 180
        return
      end

      body = "\n"
      body += "Participants: #{participants}\n\n"
      body += build_user_detail(current_user)


      users = nil
      if music_session.active_music_session
        users = music_session.active_music_session.users
      else
        users = music_session.unique_users
      end

      users.each do |user|
        if user == current_user
          next
        end

        body += build_user_detail(user)
      end

      body += "\nSession Admin URL: #{music_session_admin_url}"

      body += "\n\n"

      body += "--------DUMP--------\n"
      body += params.to_yaml

      if create_entity
        client_live_stream = ClientLiveStream.new
        client_live_stream.user = current_user
        client_live_stream.music_session = music_session if music_session
        client_live_stream.message = body
        client_live_stream.save
      end

      AdminMailer.ugly({to:'david@jamkazam.com', cc:'seth@jamkazam.com,peter@jamkazam.com', body: body, subject: subject}).deliver_now
    rescue => e
      AdminMailer.ugly({to:'david@jamkazam.com', cc:'seth@jamkazam.com,peter@jamkazam.com', body: params.to_s + "\n\n" + e.to_s, subject: 'Live Stream Started!'}).deliver_now

    end
  end
  def stream_started

    #email_stream_event("Live Stream Started!", false)



    render json: {}, :status => :ok
  end

  def stream_stop

    email_stream_event("Live Stream Stopped!", true)

    render json: {}, :status => :ok
  end
end
