require 'spec_helper'

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

  subject { page }

  before(:all) do
    Capybara.default_max_wait_time = 15
  end

  let(:user) { FactoryGirl.create(:user) }
  let(:finder) { FactoryGirl.create(:user) }

  before(:each) do
    UserMailer.deliveries.clear
    ActiveMusicSession.delete_all
  end


  it "can't see a private session until it is made public", :slow => true do
    description = "Public or private, I cant decide!"
    create_session(creator: user, description: description)
    in_client(user) do
      set_session_access :private
    end
    in_client(finder) do
        emulate_client
        sign_in_poltergeist finder
        visit "/client#/findSession"
        expect(page).to have_selector('#no-active-sessions') # verify private session is not found
        #sign_out_poltergeist(validate: true)
        visit "/"
        should_be_at_root
    end
    in_client(user) do
      set_session_access :public
    end
    join_session(finder, description: description) # verify the public session is able to be joined
  end


  it "can open the Configure Tracks modal, and Add New Audio Gear", :slow => true do
    description = "I'm gonna bail at some point!"
    create_session(creator: user, description: description)
    join_session(finder, description: description)

    assert_all_tracks_seen(users=[user, finder])

    in_client(user) do
      find('.session-track-settings').trigger(:click)
      wait_for_ajax
      expect(page).to have_selector('h1',   text: 'configure tracks')

      find('.btn-add-new-audio-gear').trigger(:click)
      wait_for_ajax
      expect(page).to have_selector('h1',   text: 'add new audio gear')

      find('#btn-leave-session-test').trigger(:click)
      wait_for_ajax
      expect(page).to have_selector('h1',   text: 'audio gear setup')
    end

    leave_music_session_sleep_delay
    in_client(finder) { expect(page).to_not have_selector("div.track-label[title='#{user.name}']") }

  end

  many = 4

  it "can see all tracks with #{many} users in a session", :slow => true do
    others = Array.new
    (many-1).times { others.push FactoryGirl.create(:user) }
    create_join_session(user, others)
    assert_all_tracks_seen(others.push user)
    #in_client(others[0]) {page.save_screenshot('tmp/partys_all_here_now.png')}
  end

  it "a user can change the genre and the Find Session screen will be updated" do
    create_session(creator: user)
    in_client(finder) { sign_in_poltergeist finder }
    # this used to randomly select genres, but it breaks if the genre contains the name work due to multiple matching genres
    ['Electronic', 'Religious'].each do |new_genre|
      in_client(finder) do
        find_session_contains?(new_genre)
      end
    end
  end

  it "can rejoin private session as creator" do
    #pending
    creator, description = create_join_session(user, [finder])

    in_client(user) do
      set_session_access :private
      formal_leave_by user
      sign_out_poltergeist user
    end

    join_session(user, description: description)
  end

  specify "creator should receive join requests after changing session to 'by approval' (VRFS-2266)" do
    creator, description = create_session
    in_client(creator) do
      set_session_access :by_approval
    end

    request_to_join_session(finder, description: description)

    in_client(creator) do
      expand_sidebar :notifications
      expect(page).to have_content "#{finder.name} has requested to join your session."
    end
  end

  specify "metronome" do
    pending "working through issue with fake media tracks"
    user = FactoryGirl.create(:user)
    invitee = FactoryGirl.create(:user)
    FactoryGirl.create(:friendship, :user => user, :friend => invitee)
    in_client(user) do
      create_session
      
      # Call it 10 times.  The fake jam client will randomly ntp_stable:false 50% of the time
      10.times do 
        find('#open-a-metronome').trigger(:click)
      end
      sleep(2)
      
      # we should have received an error one of the times, but it should open eventually:
      find('#notification').should have_text("Couldn't open metronome")
      expect(page).to have_selector('.track-label[title="Metronome"]')
      #save_screenshot("metro.png")      
    end
  end      

  specify "invitee receives notification when creator invites musician" do
    pending "blocked on testing this via front-end - fakeJamClient doesn't support invite UX"
    user = FactoryGirl.create(:user)
    invitee = FactoryGirl.create(:user)
    FactoryGirl.create(:friendship, :user => user, :friend => invitee)
    in_client(user) do
      create_session
      find('#session-invite-musicians').trigger(:click)
      find('#btn-choose-friends-update').trigger(:click)
      within('#friend-selector-list') { find("tr[user-id='#{invitee.id}']").trigger(:click) }
      find('#btn-save-friends').trigger(:click)
      find('#btn-save-invites').trigger(:click)
    end

    in_client(invitee) do
      fast_signin(invitee, "/client#/home")
      expand_sidebar :notifications
      expect(page).to have_content "invited you"
    end
  end

  specify "user is able to see recording in 'Open Recording' modal" do
    recording = FactoryGirl.create(:recording, owner: user)
    in_client(user) do
      create_session
      find('.open-recording').trigger(:click)
      within('table.local-recordings') do
        expect(page).to have_content recording.name
      end
    end
  end
end
