require 'spec_helper'

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

  subject { page }

  before(:all) do
    Capybara.javascript_driver = :poltergeist
    Capybara.current_driver = Capybara.javascript_driver
    Capybara.default_max_wait_time = 30 # these tests are SLOOOOOW
  end

  before(:each) do
    UserMailer.deliveries.clear
    ActiveMusicSession.delete_all
    @event = FactoryGirl.create(:event, :slug => 'so_latency', show_sponser:true)
    visit "/events/so_latency"
  end

  it "should not break as page gets more and more well-defined" do
    find('h1', text: @event.title)
    find('h2', text: 'ARTIST LINEUP')
    find('p', text: @event.description)
    find('.landing-sidebar')
    find('.sponsor span', 'SPONSORED BY:')

    # add an event session to the event, with nothing defined
    @event_session = FactoryGirl.create(:event_session, event: @event)
    visit "/events/so_latency"
    find('.landing-band.event img')['src'].include?('/assets/web/logo-256').should be true
    find('.event-title', text: 'TBD')
    find('.time strong', text: 'TBD')

    # define the event better by associating with a band
    band = FactoryGirl.create(:band)
    @event_session.band = band
    @event_session.save!
    visit "/events/so_latency"
    find('.landing-details.event .bio', text: band.biography)
    find('.landing-band.event img')['src'].include?('/assets/shared/avatar_generic_band').should be true

    # update starts at
    starts_at = 1.hours.ago
    @event_session.starts_at = starts_at
    @event_session.save!
    visit "/events/so_latency"
    timezone = ActiveSupport::TimeZone.new('Central Time (US & Canada)')
    find('.time strong', text: timezone.at(@event_session.starts_at.to_i).strftime('%l:%M %P').strip)

    # update ends at
    ends_at = 1.hours.from_now
    @event_session.ends_at = ends_at
    @event_session.save!
    visit "/events/so_latency"
    # UI shouldn't change; as long as it doesn't crash we are OK

    # now start a session, and don't sent session_removed_at
    music_session = FactoryGirl.create(:active_music_session, band: band)
    music_session = music_session.music_session
    music_session.session_removed_at.should be_nil
    visit "/events/so_latency"
    find('.landing-details .session-button span', text:'LISTEN NOW')
    find('.landing-details .session-button a').trigger(:click)
    find('.sessions-page .landing-band', text: band.name) # indication of session landing page
    find(".session-controls[data-music-session=\"#{music_session.id}\"]")

    # force the pinned_state to say 'not_started'
    @event_session.pinned_state = 'not_started'
    @event_session.save!
    visit "/events/so_latency"
    expect(page).not_to have_css('.landing-details .session-button a') # no button at all

    # force the pinned_state to say 'not_started'
    @event_session.pinned_state = 'over'
    @event_session.save!
    visit "/events/so_latency"
    find('.landing-details .session-button a', text:'SESSION ENDED').trigger(:click)
    find('.sessions-page .landing-band', text: band.name) # indication of session landing page
    find(".session-controls[data-music-session=\"#{music_session.id}\"]")

    # unpin
    @event_session.pinned_state = nil
    @event_session.save!


    # turn fan_access = false... this will hide the button
    music_session.fan_access = false
    music_session.save!
    visit "/events/so_latency"
    expect(page).not_to have_css('.landing-details .session-button a') # no button at all


    # now start a second session, and don't sent session_removed_at
    music_session = FactoryGirl.create(:active_music_session, band: band)
    music_session = music_session.music_session
    music_session.session_removed_at.should be_nil
    visit "/events/so_latency"
    find('.landing-details .session-button span', text:'LISTEN NOW')
    find('.landing-details .session-button a').trigger(:click)
    find('.sessions-page .landing-band', text: band.name) # indication of session landing page
    find(".session-controls[data-music-session=\"#{music_session.id}\"]")
    visit "/events/so_latency"

    # then end it, and see session_ended
    music_session.session_removed_at = Time.now
    music_session.save!
    visit "/events/so_latency"
    find('.landing-details .session-button a', text:'SESSION ENDED').trigger(:click)
    find('.sessions-page .landing-band', text: band.name) # indication of session landing page
    find(".session-controls[data-music-session=\"#{music_session.id}\"]")

    # test that it sorts correctly by putting this earlier event first
    @event_session2 = FactoryGirl.create(:event_session, event: @event)
    @event_session2.starts_at = 4.hours.ago
    @event_session2.save!
    visit "/events/so_latency"
    first(".landing-band.event[data-event-session='#{@event_session2.id}']:nth-child(1)")

    # test that it sorts correctly by putting this later event second
    @event_session2.starts_at = 4.hours.from_now
    @event_session2.save!
    visit "/events/so_latency"
    first(".landing-band.event[data-event-session='#{@event_session.id}']:nth-child(1)")

    # test that it sorts correctly by putting this later event first, because ordinal is specified
    @event_session2.ordinal = 0
    @event_session2.save!
    visit "/events/so_latency"
    first(".landing-band.event[data-event-session='#{@event_session2.id}']:nth-child(1)")

    # associate a recording, and verify that the display changes to have no description, but has a recording widget
    mix = FactoryGirl.create(:mix)
    mix.recording.music_session_id = music_session.id
    mix.recording.save!
    visit "/events/so_latency"
    find(".feed-entry.recording-entry[data-claimed-recording-id='#{mix.recording.candidate_claimed_recording.id}']")

    # associate a second recording, and verify it's ordered by name
    mix2 = FactoryGirl.create(:mix)
    mix2.recording.music_session_id = music_session.id
    mix2.recording.save!
    mix2.recording.claimed_recordings.length.should == 1
    mix2.recording.claimed_recordings[0].name = '____AAA'
    mix2.recording.claimed_recordings[0].save!
    visit "/events/so_latency"
    find(".feed-entry.recording-entry[data-claimed-recording-id='#{mix2.recording.candidate_claimed_recording.id}']:nth-child(1)")
    find(".feed-entry.recording-entry[data-claimed-recording-id='#{mix.recording.candidate_claimed_recording.id}']:nth-child(3)")

    # and do a re-order test
    mix2.recording.claimed_recordings[0].name = 'zzzzz'
    mix2.recording.claimed_recordings[0].save!
    visit "/events/so_latency"
    find('.feed-entry.recording-entry .name', text:'zzzzz')
    find(".feed-entry.recording-entry[data-claimed-recording-id='#{mix.recording.candidate_claimed_recording.id}']:nth-child(1)")
    find(".feed-entry.recording-entry[data-claimed-recording-id='#{mix2.recording.candidate_claimed_recording.id}']:nth-child(3)")
  end
end
