class MusicSessionsController < ApplicationController

  respond_to :html

  def show
    @music_session = MusicSession.find(params[:id])
    render :layout => "web"
  end

  def session_info

    @can_view = true
    @music_session = MusicSession.find(params[:id])

    unless current_user.nil?
      invitations = Invitation.where("music_session_id = ? AND receiver_id = ?", @music_session.id, current_user.id)
      has_invitation = !invitations.blank?

      unless @music_session.scheduled_start.nil?
        # session has not started yet
        if @music_session.scheduled_start > Time.now.utc
          if @music_session.creator.id == current_user.id || @music_session.open_rsvps || has_invitation
            @can_view = true
          else
            @can_view = false
          end
          
        # session has started
        else
          if @music_session.musician_access || current_user.id == @music_session.creator.id
            @can_view = true
          else
            if current_user.has_approved_rsvp(@music_session)
              @can_view = true
            else
              @can_view = false
            end
          end
        end
      end

      # run these 3 queries only if the user has access to the page
      if @can_view
        @approved_rsvps = @music_session.approved_rsvps
        @open_slots = @music_session.open_slots
        @pending_invitations = @music_session.pending_invitations
      end

      render :layout => "web"

    else
      render :layout => "web"
    end
  end

end
