require 'spec_helper'

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


  let(:austin) { austin_geoip }
  let(:dallas) { dallas_geoip }
  let(:searcher) { FactoryGirl.create(:user, last_jam_locidispid: austin_geoip[:locidispid], last_jam_addr: austin_ip) }
  let(:requested_rsvp_slot) {FactoryGirl.create(:requested_rsvp_slot, user: searcher)}

  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

    stub_const("APP_CONFIG", web_config)
  end

  it "view pending requests on music session creator side" do
    fast_signin(requested_rsvp_slot.music_session.creator, Nav.session_detail(requested_rsvp_slot.music_session))

    find('h2', text: 'RSVPs')
    find('div.rsvp-help-notice', text: 'You have new RSVPs to review and approve, see details.')
    find('#pendingRSVPs .rsvp-table td', text: searcher.name)
    find("#pendingRSVPs .rsvp-table img[data-instrument-id='#{requested_rsvp_slot.instrument_id}']")
  end

  it "view pending requests on music session searcher side" do
    fast_signin(searcher, Nav.session_detail(requested_rsvp_slot.music_session))

    find('.sessions-header a.cancel-rsvp.button-orange')
    find('h2', text: 'RSVPs')
    find('div.rsvp-help-notice', text: 'Your RSVP has not been processed by session organizer yet')
  end

  it "approve pending requests" do
    fast_signin(requested_rsvp_slot.music_session.creator, Nav.session_detail(requested_rsvp_slot.music_session))

    find('a.approveRsvpRequest[request-id="' + requested_rsvp_slot.rsvp_requests[0].id + '"]').trigger(:click)
    find('.session-musicians td', text: searcher.name)

    fast_signin(searcher, Nav.session_detail(requested_rsvp_slot.music_session))
    find('.session-musicians td', text: searcher.name)
  end

  it "decline pending requests" do
    fast_signin(requested_rsvp_slot.music_session.creator, Nav.session_detail(requested_rsvp_slot.music_session))

    find('a.declineRsvpRequest[request-id="' + requested_rsvp_slot.rsvp_requests[0].id + '"]').trigger(:click)

    should_not have_selector('td', text: searcher.name)
  end

  it "cancel current session rsvp" do
    fast_signin(requested_rsvp_slot.music_session.creator, Nav.session_detail(requested_rsvp_slot.music_session))
    find('a.approveRsvpRequest[request-id="' + requested_rsvp_slot.rsvp_requests[0].id + '"]').trigger(:click)
    find('.session-musicians td', text: searcher.name)

    find('a.cancelSessionRsvp[request-id="' + requested_rsvp_slot.rsvp_requests[0].id + '"]').trigger(:click)
    find('h1', text: 'cancel rsvp')
    find('.session-name', text: requested_rsvp_slot.music_session.name)
    find('a#btnCancelRsvp').trigger(:click)
    should_not have_selector('td', text: searcher.name)
  end

  it "shows latency information correctly", intermittent: true do
    # intermittent: this fails a good amount on all environments

    # this will try to show all 6  latency badges.  unknown, good, fair, poor, unacceptable, and me
    session_creator = requested_rsvp_slot.music_session.creator
    session_creator.last_jam_locidispid = dallas_geoip[:locidispid]
    session_creator.last_jam_addr = dallas_ip
    session_creator.save!

    fast_signin(session_creator, Nav.session_detail(requested_rsvp_slot.music_session))
    find('h2', text: 'RSVPs')


    target_path = Nav.session_detail(requested_rsvp_slot.music_session)
    verify_session_detail_score(nil, session_creator, searcher, target_path)
    verify_session_detail_score(nil, session_creator, session_creator, target_path)
    verify_session_detail_score(3, session_creator, searcher, target_path)
    verify_session_detail_score(40, session_creator, searcher, target_path)
    verify_session_detail_score(80, session_creator, searcher, target_path)
    verify_session_detail_score(110, session_creator, searcher, target_path)
  end
end
