require 'spec_helper'

describe "Create Session", :js => true, :type => :feature, :capybara_feature => true do
  let(:user1) { FactoryGirl.create(:user) }
  let(:user2) { FactoryGirl.create(:user) }

  context "functionally test all ways to Create Session" do

    context "I have already scheduled a session..." do
      let (:now) { Time.now } 
      let (:first_session)  { FactoryGirl.create(:music_session, creator: user1, name: "First one",  scheduled_start: now + 5.minutes, timezone: "Central Time (US & Canada),America/Chicago")}
      let (:second_session) { FactoryGirl.create(:music_session, creator: user1, name: "Second one", scheduled_start: now + 2.hours, timezone: "Central Time (US & Canada),America/Chicago", :create_type => MusicSession::CREATE_TYPE_START_SCHEDULED)}
      let (:third_session)  { FactoryGirl.create(:music_session, creator: user1, name: "Third one",  scheduled_start: now + 17.days, timezone: "Central Time (US & Canada),America/Chicago", :create_type => MusicSession::CREATE_TYPE_START_SCHEDULED)}
      let (:not_my_session) { FactoryGirl.create(:music_session, creator: user2, name: "Who cares",  scheduled_start: now + 30.minutes, timezone: "Central Time (US & Canada),America/Chicago", :create_type => MusicSession::CREATE_TYPE_START_SCHEDULED)}

      before do
        #instantiate these test sessions in non-sequential order
        third_session.touch; first_session.touch; not_my_session.touch; second_session.touch;
        #[first_session, second_session, third_session, not_my_session].each { |s| puts "#{s.name}: #{s.id}" }

        emulate_client
        page.driver.resize(1500, 800) #purely aesthetic
        sign_in_poltergeist user1
        find('.createsession').trigger(:click)
        wait_for_ajax
        expect(page).to have_selector 'li[create-type="start-scheduled"] ins'
      end

      it "sessions are shown in schedule order on the Create Session screen" do
        sleep 2 #arg
        radio_buttons = page.all('ul#scheduled-session-list li')
        first, second, third = *radio_buttons[0..2]
        expect(first.text).to include first_session.name
        expect(second.text).to include second_session.name
        expect(third.text).to include third_session.name
        expect(first).to have_selector 'input[checked=checked]'
        expect(page).to_not have_text not_my_session.name
      end

      it "future sessions can be edited from the Create Session screen" do
        #pending "possible bug, does not occur when testing manually"
        page.find('a#edit_scheduled_sessions').trigger(:click)
        #expect(page).to have_selector "div[data-id='#{first_session.id}']"   #see pending note
        expect(page).to have_selector "div[data-id='#{second_session.id}']"
        expect(page).to have_selector "div[data-id='#{third_session.id}']"
        expect(page).to_not have_selector "div[data-id='#{not_my_session.id}']"
      end

      context "...start it now" do
        it "starts the first one" do   
          wait_for_ajax
          sleep(2)
          find('.btn-next').trigger(:click)
          wait_for_ajax
          expect(page).to have_selector('.session-step-title', text: 'Review & Confirm')
          expect(page).to have_content first_session.name
          find('.btn-next').trigger(:click)
          wait_for_ajax
          expect(page).to have_selector('h2', text: 'my live tracks')
          find('#session-screen .session-my-tracks .session-track.my-track')
        end

        context "attempt to start a session more than an hour from now" do
          let (:first_session) { FactoryGirl.create(:music_session, creator: user1, scheduled_start: now + 75.minutes) }

          it "warns the user that session starts in the future, and user can start session" do
            #pending "play with Database.db_timezone on this one"
            sleep 1
            find('.btn-next').trigger(:click)
            sleep 2
            expect(page).to have_selector('h1', text: 'Future Session')
            expect(page).to have_content "Are you sure"
            find('#btn-confirm-ok', text: 'START SESSION NOW').trigger(:click)
            sleep 1
            expect(page).to have_selector('.session-step-title', text: 'Review & Confirm')
            expect(page).to have_content first_session.name
            find('.btn-next').trigger(:click)
            expect(page).to have_selector('h2', text: 'my live tracks')
            find('#session-screen .session-my-tracks .session-track.my-track')
          end
        end
      end
    end


    shared_examples_for :a_future_session do
      specify "creator can see the session on Create Session page" do
        in_client(creator) do
          page.find('.createsession').trigger(:click)
          expect(page).to have_selector('h1',    text: 'create session')
          sessions = page.first('ul#scheduled-session-list li')
          expect(sessions.text).to include session_name
        end
      end

      specify "creator can see the session on Find Session page" do
        in_client(creator) do
          visit "/client#/findSession"
          wait_until_curtain_gone
          expect(page).to have_selector('#session-name-disp',  text: "#{session_name} (#{session_genre})")
          #expect(page).to have_selector('#session-name-disp',  text: @session_genre)
        end
      end

      specify "another user can see the session on Find Session page" do
        in_client(someone_else) do
          emulate_client
          page.driver.resize(1500, 800)
          sign_in_poltergeist someone_else
          visit "/client#/findSession"
          wait_until_curtain_gone
          expect(find('table#sessions-scheduled')).to have_content session_name
          expect(find('table#sessions-scheduled')).to have_content session_genre
        end
      end

      specify "another user can RSVP to the session" do
        in_client(someone_else) do
          emulate_client
          page.driver.resize(1500, 800)
          sign_in_poltergeist someone_else
          visit "/client#/findSession"
          wait_until_curtain_gone

          within('table#sessions-scheduled') do
            find('a.rsvp-link').trigger(:click)
          end

          within('div.dialog-inner') do
            find('div.session-name').should have_content session_name
            find('div.slot-instructions').should have_content "Check the box(es) next to the track(s) you want to play"
            # fill_in '.txtComment', with: "Looking forward to the session"
            #first('div.rsvp-instruments input').trigger(:click)
            sleep 1
            find('#btnSubmitRsvp').trigger(:click)
            sleep 2
          end
        end
      end

      specify "creator can start the session" do
        in_client(creator) do
          page.find('.createsession').trigger(:click)
          expect(page).to have_selector('h1',    text: 'create session')
          expect(page).to have_content session_name
          find('li[create-type="start-scheduled"] ins').trigger(:click)
          find('.btn-next').trigger(:click)
          find('.btn-next').trigger(:click)

          btn = first('#btn-alert-ok')# accept the 'If you start this session now, the scheduled start time...'
          btn.trigger(:click) if btn

          expect(page).to have_selector('h2', text: 'my live tracks')
          find('#session-screen .session-my-tracks .session-track.my-track')
        end
      end
    end

    context "I want to schedule a session for a specific future time" do
      before do
        MusicSession.delete_all
        @creator, @session_name, @session_genre = schedule_session(creator: user1)
      end

      it_should_behave_like :a_future_session do
        let(:creator) { @creator }
        let(:session_name) { @session_name }
        let(:session_genre) { @session_genre }
        let(:someone_else) { FactoryGirl.create(:user) }
      end
    end

    context "I want to choose the time after others RSVP to my session" do
      before do
        MusicSession.delete_all
        @creator, @session_name, @session_genre = schedule_session(creator: user1, rsvp: true)
      end

      it_should_behave_like :a_future_session do
        let(:creator) { @creator }
        let(:session_name) { @session_name }
        let(:session_genre) { @session_genre }
        let(:someone_else) { FactoryGirl.create(:user) }
      end
    end

    context "I want to start a new session right now for others to join" do
      before do
        MusicSession.delete_all
        @creator, @session_name, @session_genre = schedule_session(creator: user1, immediate: true)
      end

      specify "creator is in the session" do
        in_client @creator do
          expect(page).to have_selector('h2', text: 'my live tracks')
          find('#session-screen .session-my-tracks .session-track.my-track')
        end
      end

      specify "another user can see this active session on Find Session page" do
        in_client(user2) do
          emulate_client
          page.driver.resize(1500, 800)
          sign_in_poltergeist user2
          visit "/client#/findSession"
          wait_until_curtain_gone
          expect(find('table#sessions-active')).to have_content @session_name
          expect(find('table#sessions-active')).to have_content @session_genre
        end
      end
    end

    context "I want to quick start a test session just for me" do
      before do
        MusicSession.delete_all
        @creator, @session_name, @session_genre = schedule_session(creator: user1, quickstart: true)
      end

      specify "creator is in the session" do
        in_client @creator do
          expect(page).to have_selector('h2', text: 'my live tracks')
          find('#session-screen .session-my-tracks .session-track.my-track')
        end
      end

      specify "another user does NOT see the session on Find Session page" do
        in_client(user2) do
          emulate_client
          page.driver.resize(1500, 800)
          sign_in_poltergeist user2
          visit "/client#/findSession"
          wait_until_curtain_gone
          expect(find('table#sessions-scheduled')).to_not have_content @session_name
        end
      end
    end
  end
end
