require 'spec_helper'

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

  subject { page }


  let(:austin) { austin_geoip }
  let(:dallas) { dallas_geoip }
  let(:user) { FactoryGirl.create(:user, last_jam_locidispid: austin_geoip[:locidispid], last_jam_addr: austin_ip) }
  let(:finder) { FactoryGirl.create(:user, last_jam_locidispid: dallas_geoip[:locidispid], last_jam_addr: dallas_ip) }
  
  before(:all) do
    Capybara.default_max_wait_time = 20
  end


  before(:each) do
    UserMailer.deliveries.clear
    ActiveMusicSession.delete_all
    SessionInfoComment.delete_all
    Notification.delete_all
    RsvpRequestRsvpSlot.delete_all
    RsvpRequest.delete_all
    RsvpSlot.delete_all
    Invitation.delete_all
    MusicSession.delete_all
    Score.delete_all
    User.delete_all
    Score.connection.execute('delete from current_network_scores').check
  end

  describe "basic" do

    before(:each) do
      create_phony_database
      emulate_client
      fast_signin(user, "/client#/findSession")
      FactoryGirl.create(:friendship, :user => user, :friend => finder)
      find('#no-active-sessions')
      find('#no-scheduled-sessions')
    end


    # when no sessions have been created:
    it "shows there are no sessions" do
      # verify no sessions are found
      expect(page).to have_selector('#no-active-sessions')
      expect(page).to have_selector('#no-scheduled-sessions')
    end

    it "finds another public session"  do
      create_join_session(user, [finder])
    end

    describe "listing behavior" do

      describe "active sessions" do
        let!(:session1_creator) { FactoryGirl.create(:single_user_session, last_jam_locidispid: dallas_geoip[:locidispid], last_jam_addr: dallas_ip) }

        it "find one active session" do
          page.should have_no_selector('#btn-refresh.disabled')
          find('#btn-refresh').trigger(:click)
          page.should have_no_selector('#btn-refresh.disabled')
          find('#sessions-active .found-session', count: 1)
        end


        it "find many active sessions" do
          20.times do
            FactoryGirl.create(:single_user_session)
          end
          page.should have_no_selector('#btn-refresh.disabled')
          find('#btn-refresh').trigger(:click)
          page.should have_no_selector('#btn-refresh.disabled')
          page.assert_selector('#sessions-active .found-session', count: 20)
          # attempt to scroll down--the end of session list should show, and there should now be 21 items
          # page.execute_script('jQuery("#findSession .content-body-scroller").scrollTo("100%",100)') #scroll to the bottom of the element
          # find('#end-of-session-list')
          # page.all('div#sessions-active .found-session').count.should == 21
        end

        it "shows latency information correctly" do
          # this will try to show all 6  latency badges.  unknown, good, fair, poor, unacceptable, and me
          verify_find_session_score(nil,   '#sessions-active', user, session1_creator)
          verify_find_session_score(3,     '#sessions-active', user, session1_creator)
          verify_find_session_score(40,    '#sessions-active', user, session1_creator)
          verify_find_session_score(80,    '#sessions-active', user, session1_creator)
          verify_find_session_score(110,   '#sessions-active', user, session1_creator)

          fast_signout
          fast_signin(session1_creator, "/client#/findSession")

          verify_find_session_score(nil,   '#sessions-active', session1_creator, session1_creator)

          # this should cause it to move to the scheduled session view; we'll check all the values again
          ActiveMusicSession.delete_all
          sleep(3)
          verify_find_session_score(nil,   '#sessions-scheduled', session1_creator, session1_creator)

          fast_signout
          fast_signin(user, "/client#/findSession")

          verify_find_session_score(nil,   '#sessions-scheduled', user, session1_creator)
          verify_find_session_score(3,     '#sessions-scheduled', user, session1_creator)
          verify_find_session_score(40,    '#sessions-scheduled', user, session1_creator)
          verify_find_session_score(80,    '#sessions-scheduled', user, session1_creator)
          verify_find_session_score(110,   '#sessions-scheduled', user, session1_creator)

        end
      end

      describe "scheduled sessions" do
        let!(:scheduled_session) {FactoryGirl.create(:music_session) }

        it "find one scheduled session" do
          page.should have_no_selector('#btn-refresh.disabled')
          find('#btn-refresh').trigger(:click)
          page.should have_no_selector('#btn-refresh.disabled')
          page.assert_selector('div#sessions-scheduled .found-session', count: 1)
        end

        it "finds many scheduled sessions" do
          20.times do
            FactoryGirl.create(:music_session)
          end
          page.should have_no_selector('#btn-refresh.disabled')
          find('#btn-refresh').trigger(:click)
          page.should have_no_selector('#btn-refresh.disabled')
          page.assert_selector('div#sessions-scheduled .found-session', count: 20)

          page.execute_script('jQuery("#findSession .content-body-scroller").scrollTo("100%",100)') #scroll to the bottom of the element
          find('#no-scheduled-sessions')
          page.assert_selector('div#sessions-scheduled .found-session', count: 21)
        end
      end
    end
  end

  describe "active sessions" do
    it "should render session details" do
      # name

      # genre

      # description

      # notation files

      # musicians in session

      # approved RSVPs

      # open slots

      # latency tag

      # chat language

      # musician access

      # fan access

      # legal policy
    end

    it "should allow user to join if invited" do
    end

    it "should allow user to join if musician_access is true" do
    end

    it "should allow user to join if invited and musician_access is false" do
    end
  end

  describe "scheduled sessions" do
    it "should render session details" do
      # name

      # genre

      # description

      # notation files

      # approved RSVPs

      # open slots

      # latency tag

      # chat language

      # musician access

      # fan access

      # legal policy
    end

    it "should allow user to RSVP if invited" do
    end

    it "should allow user to RSVP if open_rsvps is true" do
    end

    it "should allow user to RSVP if invited and open_rsvps is false" do
    end
  end

  describe "start session behavior" do
    before(:each) do

      @music_session = FactoryGirl.create(:music_session, creator: user)

      @invited_user = FactoryGirl.create(:user)
      FactoryGirl.create(:friendship, :user => @invited_user, :friend => user)
      FactoryGirl.create(:friendship, :user => user, :friend => @invited_user)
      @invitation = FactoryGirl.create(:invitation, :sender => user, :receiver => @invited_user, :music_session => @music_session)

      @rsvp_user = FactoryGirl.create(:user)
      @rsvp_slot = FactoryGirl.create(:rsvp_slot, music_session: @music_session)
      @rsvp_request = FactoryGirl.create(:rsvp_request_for_slots, chosen: nil, user: @rsvp_user, slots: [@rsvp_slot])
    end

    it "should always show start session link for session creator" do
      pending
      fast_signin(user, Nav.find_session)
      find("#sessions-scheduled .rsvp-msg span.text a.start", text: "Start session now?")
      fast_signout
    end

    it "should not show start session link for anyone who is not creator, invitee, or RSVP user" do
      random_user = FactoryGirl.create(:user)
      fast_signin(random_user, Nav.find_session)
      page.should have_no_selector("#sessions-scheduled .rsvp-msg span.text a.start")
      fast_signout
    end

    it "should show start session link for invited or RSVP users" do
      pending
       # make session date/time TBD
      @music_session.scheduled_start = Time.now + 5.minutes
      @music_session.save!

      # invited user
      fast_signin(@invited_user, Nav.find_session)
      page.should have_selector("#sessions-scheduled .rsvp-msg span.text a.start", text: "Start session now?")
      fast_signout

      # RSVP user
      fast_signin(@rsvp_user, Nav.find_session)
      page.should have_no_selector("#sessions-scheduled .rsvp-msg span.text a.start", text: "Start session now?")
      page.should have_selector("#sessions-scheduled .rsvp-msg span.text a.cancel", text: "Cancel RSVP")
      fast_signout

      # now approve the RSVP
      @rsvp_request.rsvp_requests_rsvp_slots[0].chosen = true
      @rsvp_request.rsvp_requests_rsvp_slots[0].save!

      fast_signin(@rsvp_user, Nav.find_session)
      page.should have_selector("#sessions-scheduled .rsvp-msg span.text a.start", text: "Start session now?")
      page.should have_selector("#sessions-scheduled .rsvp-msg span.text a.cancel", text: "Cancel RSVP")
      fast_signout
    end

    it "should not show start session link for invited or RSVP users when date/time is TBD" do
      pending
      # make session date/time TBD
      @music_session.scheduled_start = nil
      @music_session.save!

      # invited user
      fast_signin(@invited_user, Nav.find_session)
      page.should have_no_selector("#sessions-scheduled .rsvp-msg span.text a.start", text: "Start session now?")
      fast_signout

      # RSVP user
      fast_signin(@rsvp_user, Nav.find_session)
      page.should have_no_selector("#sessions-scheduled .rsvp-msg span.text a.start", text: "Start session now?")
      page.should have_selector("#sessions-scheduled .rsvp-msg span.text a.cancel", text: "Cancel RSVP")
      fast_signout

      # now approve the RSVP
      @rsvp_request.rsvp_requests_rsvp_slots[0].chosen = true
      @rsvp_request.rsvp_requests_rsvp_slots[0].save!

      # "start session" should still be hidden
      fast_signin(@rsvp_user, Nav.find_session)
      page.should have_no_selector("#sessions-scheduled .rsvp-msg span.text a.start", text: "Start session now?")
      page.should have_selector("#sessions-scheduled .rsvp-msg span.text a.cancel", text: "Cancel RSVP")
      fast_signout
    end

    it "should not show start session link for invited or RSVP users when more than 15 minutes remain to start time" do
      pending
      # make session date/time more than 15 min away
      @music_session.scheduled_start = Time.now + 60.minutes
      @music_session.save!

      # invited user
      fast_signin(@invited_user, Nav.find_session)
      page.should have_no_selector("#sessions-scheduled .rsvp-msg span.text a.start", text: "Start session now?")
      fast_signout

      # RSVP user
      fast_signin(@rsvp_user, Nav.find_session)
      page.should have_no_selector("#sessions-scheduled .rsvp-msg span.text a.start", text: "Start session now?")
      page.should have_selector("#sessions-scheduled .rsvp-msg span.text a.cancel", text: "Cancel RSVP")
      fast_signout

      # now approve the RSVP
      @rsvp_request.rsvp_requests_rsvp_slots[0].chosen = true
      @rsvp_request.rsvp_requests_rsvp_slots[0].save!

      # "start session" should still be hidden
      fast_signin(@rsvp_user, Nav.find_session)
      page.should have_no_selector("#sessions-scheduled .rsvp-msg span.text a.start", text: "Start session now?")
      page.should have_selector("#sessions-scheduled .rsvp-msg span.text a.cancel", text: "Cancel RSVP")
      fast_signout
    end
  end

  describe "rsvp behavior" do
    before(:each) do
      stub_const("APP_CONFIG", web_config)
    end

    it "handles is_unstructured_rsvp sessions correctly" do

      # create an unstructured session
      music_session = FactoryGirl.create(:music_session, creator: user, is_unstructured_rsvp: true)

      fast_signin(finder, Nav.find_session)

      # verify it says the right thing in the 'Still Needed' section
      find("#sessions-scheduled tr.found-session div.instruments", text: 'Any Instrument (Any Skill Level)')

      # bring up the RSVP dialog
      find('.rsvp-link').trigger(:click)

      # select the only option (Any Instrument)
      find('.rsvp-instruments input[value="unstructured"]').trigger(:click)

      # submit the RSVP
      find('#btnSubmitRsvp').trigger(:click)

      # TODO: verify that the UI works - after VRFS-1892
    end

    it "RSVP text shows correctly" do
      music_session = FactoryGirl.create(:music_session, creator: user)

      # session creator cannot cancel
      fast_signin(user, Nav.find_session)
      page.should have_no_selector("#sessions-scheduled .rsvp-msg span.text a.cancel", text: "Cancel RSVP")
      sign_out

      # create a slot so the session can be joined
      rsvp_slot = FactoryGirl.create(:rsvp_slot, music_session: music_session)
      go_to_root
      fast_signin(finder, Nav.find_session)
      find("#sessions-scheduled .rsvp-link")

      # now manipulate the database to make different states for the rsvp link/text area in find sessions
      # make a request (not yet chosen)
      rsvp_request = FactoryGirl.create(:rsvp_request_for_slots, chosen: nil, user: finder, slots: [rsvp_slot])

      # first state: an unconfirmed RSVP
      go_to_root
      fast_signin(finder, Nav.find_session)
      find("#sessions-scheduled .rsvp-msg span.text a.cancel", text: "Cancel RSVP")

      rsvp_request.rsvp_requests_rsvp_slots[0].chosen = true
      rsvp_request.rsvp_requests_rsvp_slots[0].save!

      # second state: a confirmed RSVP
      go_to_root
      fast_signin(finder, Nav.find_session)
      find("#sessions-scheduled a.cancel", text: "Cancel RSVP")


      # need to now CANCEL, and check what it says:  // VRFS-1891

      # also need to check that it is open_rsvp = false.

    end
  end
end
