require 'spec_helper'

describe "Session Info", :js => true, :type => :feature, :capybara_feature => true do


  let(:austin) { austin_geoip }
  let(:dallas) { dallas_geoip }

  before(:each) do
    SessionInfoComment.delete_all
    Notification.delete_all
    RsvpRequestRsvpSlot.delete_all
    RsvpRequest.delete_all
    RsvpSlot.delete_all
    Invitation.delete_all
    MusicSession.delete_all
    User.delete_all
    MaxMindManager.create_phony_database

    @rsvp_approved_user = FactoryGirl.create(:user, last_jam_locidispid: dallas_geoip[:locidispid], last_jam_addr: dallas_ip)
    @rsvp_approved_user.save

    @rsvp_declined_user = FactoryGirl.create(:user, last_jam_locidispid: dallas_geoip[:locidispid], last_jam_addr: dallas_ip)
    @rsvp_declined_user.save

    @session_invitee = FactoryGirl.create(:user, last_jam_locidispid: dallas_geoip[:locidispid], last_jam_addr: dallas_ip)
    @session_invitee.save

    @non_session_invitee = FactoryGirl.create(:user)
    @non_session_invitee.save

    @session_creator = FactoryGirl.create(:user, last_jam_locidispid: austin_geoip[:locidispid], last_jam_addr: austin_ip)
    @session_creator.save

    # session invitations require sender and receiver to be friends
    FactoryGirl.create(:friendship, :user => @session_invitee, :friend => @session_creator)
    FactoryGirl.create(:friendship, :user => @session_creator, :friend => @session_invitee)

    FactoryGirl.create(:friendship, :user => @rsvp_approved_user, :friend => @session_creator)
    FactoryGirl.create(:friendship, :user => @session_creator, :friend => @rsvp_approved_user)

    FactoryGirl.create(:friendship, :user => @rsvp_declined_user, :friend => @session_creator)
    FactoryGirl.create(:friendship, :user => @session_creator, :friend => @rsvp_declined_user)

    @music_session = FactoryGirl.create(:music_session, :creator => @session_creator, :scheduled_start => Time.now.utc + 2.days, :musician_access => true, :approval_required => true, :open_rsvps => false)

    @url = "/sessions/#{@music_session.id}/details"

    @slot1 = FactoryGirl.create(:rsvp_slot, :music_session => @music_session, :instrument => JamRuby::Instrument.find('electric guitar'))
    @slot2 = FactoryGirl.create(:rsvp_slot, :music_session => @music_session, :instrument => JamRuby::Instrument.find('drums'))
    
    @invitation = FactoryGirl.create(:invitation, :sender => @session_creator, :receiver => @session_invitee, :music_session => @music_session)
    @invitation = FactoryGirl.create(:invitation, :sender => @session_creator, :receiver => @rsvp_approved_user, :music_session => @music_session)
    @invitation = FactoryGirl.create(:invitation, :sender => @session_creator, :receiver => @rsvp_declined_user, :music_session => @music_session)

    @rsvp1 = RsvpRequest.create({:session_id => @music_session.id, :rsvp_slots => [@slot1.id, @slot2.id], :message => "Let's Jam!"}, @rsvp_approved_user)
    @rsvp2 = RsvpRequest.create({:session_id => @music_session.id, :rsvp_slots => [@slot1.id, @slot2.id], :message => "Let's Jam!"}, @rsvp_declined_user)

    # approve slot1
    rs1 = RsvpRequestRsvpSlot.find_by_rsvp_request_id_and_rsvp_slot_id(@rsvp1.id, @slot1.id)
    rs2 = RsvpRequestRsvpSlot.find_by_rsvp_request_id_and_rsvp_slot_id(@rsvp1.id, @slot2.id)
    RsvpRequest.update({:id => @rsvp1.id, :session_id => @music_session.id, :rsvp_responses => [{:request_slot_id => rs1.id, :accept => true}, {:request_slot_id => rs2.id, :accept => false}]}, @session_creator)

    # reject slot1 and slot2
    rs1 = RsvpRequestRsvpSlot.find_by_rsvp_request_id_and_rsvp_slot_id(@rsvp2.id, @slot1.id)
    rs2 = RsvpRequestRsvpSlot.find_by_rsvp_request_id_and_rsvp_slot_id(@rsvp2.id, @slot2.id)
    RsvpRequest.update({:id => @rsvp2.id, :session_id => @music_session.id, :rsvp_responses => [{:request_slot_id => rs1.id, :accept => false}, {:request_slot_id => rs2.id, :accept => false}]}, @session_creator)
  end

  def ensure_success(options = {})
    # creator avatar
    find('div.landing-avatar')

    # creator name
    find('div.creator-name', text: @session_creator.name)

    # action button
    if options[:show_cta]
      find('#btn-action', :text => options[:button_text])
    else
      should_not have_selector('#btn-action')
    end

    # session details
    find('div.scheduled_start')
    find('div.genre', text: @music_session.genre)
    find('div.name', text: @music_session.name)
    find('div.description', text: @music_session.description)
    find('div.notations')
    find('div.language', text: @music_session.language_description)
    find('div.access', text: @music_session.access_description)
    find('div.legal_policy', text: @music_session.legal_policy.capitalize)

    # right sidebar - RSVPs
    find("div.rsvp-details[data-user-id=\"#{@rsvp_approved_user.id}\"] .avatar-tiny")
    find("div.rsvp-details[data-user-id=\"#{@rsvp_approved_user.id}\"] .rsvp-name", text: @rsvp_approved_user.name)
    find("div.rsvp-details[data-user-id=\"#{@rsvp_approved_user.id}\"] img.instrument-icon")

    # right sidebar - Still Needed
    find('div.still-needed', text: @slot2.instrument.id.capitalize)

    # right sidebar - Pending Invitations
    find('div[user-id="' + @session_invitee.id + '"]')
  end

  def ensure_failure
    find('strong.not-found', text: "PRIVATE SESSION")
  end

  describe "view" do

    ########### BEFORE SESSION STARTS ###########

    it "should render for any musician for sessions with open RSVPs before session starts" do

      @music_session.open_rsvps = true
      @music_session.save

      # attempt to access with musician who was invited but didn't RSVP
      fast_signin(@session_invitee, @url)
      ensure_success({:show_cta => true, :button_text => 'RSVP NOW!'})
      fast_signout

      # attempt to access with musician who wasn't invited
      fast_signin(@non_session_invitee, @url)
      ensure_success({:show_cta => true, :button_text => 'RSVP NOW!'})
      fast_signout

      # attempt to access with musician who RSVP'ed but wasn't approved
      fast_signin(@rsvp_declined_user, @url)
      ensure_success({:show_cta => false})
      fast_signout

      # attempt to access with musician who RSVP'ed and was approved
      fast_signin(@rsvp_approved_user, @url)
      ensure_success({:show_cta => true, :button_text => 'CANCEL RSVP'})
      fast_signout

      # attempt to access with session creator
      fast_signin(@session_creator, @url)
      ensure_success({:show_cta => false})
      fast_signout
    end

    it "should render only for session invitees for sessions with closed RSVPs before session starts" do
      # attempt to access with musician who was invited but didn't RSVP
      fast_signin(@session_invitee, @url)
      ensure_success({:show_cta => true, :button_text => 'RSVP NOW!'})
      fast_signout

      # attempt to access with musician who wasn't invited
      fast_signin(@non_session_invitee, @url)
      ensure_failure # NON-INVITEE SHOULD NOT BE ABLE TO VIEW FOR CLOSED RSVPs
      fast_signout

      # attempt to access with musician who RSVP'ed but wasn't approved
      fast_signin(@rsvp_declined_user, @url)
      ensure_success({:show_cta => false})
      fast_signout

      # attempt to access with musician who RSVP'ed and was approved
      fast_signin(@rsvp_approved_user, @url)
      ensure_success({:show_cta => true, :button_text => 'CANCEL RSVP'})
      fast_signout

      # attempt to access with session creator
      fast_signin(@session_creator, @url)
      ensure_success({:show_cta => false})
      fast_signout
    end

    ########### AFTER SESSION STARTS ###########

    # musician_access = false, approval_required = false
    it "should allow only RSVP approvals to view for 'rsvp_only' option after session starts" do
      @music_session.musician_access = false
      @music_session.approval_required = false
      @music_session.scheduled_start = Time.now.utc - 1.hours
      @music_session.save!

      # attempt to access with musician who was invited but didn't RSVP
      fast_signin(@session_invitee, @url)
      ensure_failure
      fast_signout

      # attempt to access with musician who wasn't invited
      fast_signin(@non_session_invitee, @url)
      ensure_failure
      fast_signout

      # attempt to access with musician who RSVP'ed but wasn't approved
      fast_signin(@rsvp_declined_user, @url)
      ensure_failure
      fast_signout

      # attempt to access with musician who RSVP'ed and was approved
      fast_signin(@rsvp_approved_user, @url)
      ensure_success({:show_cta => true, :button_text => 'CANCEL RSVP'})
      fast_signout

      # attempt to access with session creator
      fast_signin(@session_creator, @url)
      ensure_success({:show_cta => false})
      fast_signout
    end

    it "shows latency information correctly" do
      # this will try to show all 6  latency badges.  unknown, good, fair, poor, unacceptable, and me

      fast_signin(@session_creator, @url)
      verify_session_info_score(nil, @session_creator, @session_creator)
      verify_session_info_score(nil, @session_creator, @rsvp_approved_user)
      verify_session_info_score(3, @session_creator, @rsvp_approved_user)
      verify_session_info_score(40, @session_creator, @rsvp_approved_user)
      verify_session_info_score(80, @session_creator, @rsvp_approved_user)
      verify_session_info_score(110, @session_creator, @rsvp_approved_user)
    end

    # musician_access = true, approval_required = false
    it "should allow anyone to view for 'at will' option after session starts" do
      @music_session.musician_access = true
      @music_session.approval_required = false
      @music_session.save!

      # attempt to access with musician who was invited but didn't RSVP
      fast_signin(@session_invitee, @url)
      ensure_success({:show_cta => true, :button_text => 'RSVP NOW!'})
      fast_signout

      # attempt to access with musician who wasn't invited
      fast_signin(@non_session_invitee, @url)
      ensure_failure # NON-INVITEE SHOULD NOT BE ABLE TO VIEW FOR CLOSED RSVPs
      fast_signout

      # attempt to access with musician who RSVP'ed but wasn't approved
      fast_signin(@rsvp_declined_user, @url)
      ensure_success({:show_cta => false})
      fast_signout

      # attempt to access with musician who RSVP'ed and was approved
      fast_signin(@rsvp_approved_user, @url)
      ensure_success({:show_cta => true, :button_text => 'CANCEL RSVP'})
      fast_signout

      # attempt to access with session creator
      fast_signin(@session_creator, @url)
      ensure_success({:show_cta => false})
      fast_signout
    end

    # musician_access = true, approval_required = true
    it "should allow anyone to view for 'join by approval' option after session starts" do
      @music_session.musician_access = true
      @music_session.approval_required = true
      @music_session.save!

      # attempt to access with musician who was invited but didn't RSVP
      fast_signin(@session_invitee, @url)
      ensure_success({:show_cta => true, :button_text => 'RSVP NOW!'})
      fast_signout

      # attempt to access with musician who wasn't invited
      fast_signin(@non_session_invitee, @url)
      ensure_failure # NON-INVITEE SHOULD NOT BE ABLE TO VIEW FOR CLOSED RSVPs
      fast_signout

      # attempt to access with musician who RSVP'ed but wasn't approved
      fast_signin(@rsvp_declined_user, @url)
      ensure_success({:show_cta => false})
      fast_signout

      # attempt to access with musician who RSVP'ed and was approved
      fast_signin(@rsvp_approved_user, @url)
      ensure_success({:show_cta => true, :button_text => 'CANCEL RSVP'})
      fast_signout

      # attempt to access with session creator
      fast_signin(@session_creator, @url)
      ensure_success({:show_cta => false})
      fast_signout
    end

    it "should show no call to action button if user has not RSVPed and all slots are taken" do
      pending
      rs2 = RsvpRequestRsvpSlot.find_by_rsvp_request_id_and_rsvp_slot_id(@rsvp1.id, @slot2.id)

      # approve slot 2 as well to make all slots taken for this session
      RsvpRequest.update({:id => @rsvp1.id, :session_id => @music_session.id, :rsvp_responses => [{:request_slot_id => rs2.id, :accept => true}]}, @session_creator)

      fast_signin(@session_invitee, @url)
      expect {find('#btn-action')}.to raise_error(Capybara::ElementNotFound)
      fast_signout
    end
  end

  describe "actions" do
    it "should show RSVP Now button and launch submit dialog if user has not RSVPed" do
    end

    it "should show Cancel RSVP button and launch cancel dialog if user has RSVPed" do
    end

    it "should refresh sidebar RSVP section after user submits RSVP request" do
    end

    it "should refresh sidebar Still Needed section after user cancels RSVP request" do
    end
  end
end