require 'spec_helper'

describe "Musician Search", :js => true, :type => :feature, :capybara_feature => true do

  subject { page }

  let(:austin) { austin_geoip }
  let(:dallas) { dallas_geoip }

  let(:austin_user) { FactoryGirl.create(:austin_user) }
  let(:dallas_user) { FactoryGirl.create(:dallas_user) }
  let(:miami_user) { FactoryGirl.create(:miami_user) }
  let(:seattle_user) { FactoryGirl.create(:seattle_user) }

  before(:each) do
    MaxMindManager.create_phony_database
    User.delete_all
    austin_user.touch
    dallas_user.touch

    Score.delete_all
    Score.connection.execute('delete from current_network_scores').check

    Score.createx(austin_geoip[:locidispid], 'a', 1, dallas_geoip[:locidispid], 'a', 1, 10)

    fast_signin(austin_user, "/client#/musicians")
  end

  it "shows search results" do
    expect(page).to have_selector('#musician-search-filter-results .musician-list-result')
  end

  it "shows submits query" do
    expect(page).to have_selector('#musician-search-filter-results .musician-list-result')
  end

  it "shows blank result set" do
    pending "fails intermittently on build server"

    wait_for_easydropdown('#musician_instrument')
    # get the 2nd option from the instruments list
    text = find('#musician_instrument', :visible => false).find(:xpath, 'option[2]', :visible => false).text
    # and select it
    jk_select(text, '#musician_instrument')
    expect(page).to_not have_selector('#musician-filter-results .musician-list-result')
  end

  it "shows latency information correctly" do
    pending "hover isn't working reliably in poltergeist"

    # this will try to show 5  latency badges.  unknown, good, fair, poor, unacceptable. 'me' does not happen on this screen
    austin_user.last_jam_locidispid = austin[:locidispid]
    austin_user.save!

    verify_find_musician_score(nil, austin_user, dallas_user)
    verify_find_musician_score(3,   austin_user, dallas_user)
    verify_find_musician_score(40,  austin_user, dallas_user)
    verify_find_musician_score(80,  austin_user, dallas_user)
    verify_find_musician_score(110, austin_user, dallas_user)
  end

  # TODO: This test is broken due to this bug: VRFS-3119 - bad location / distance in SQL:
  it "shows search by distance" do

    # this test does a distance search with the austin user, then opens up the 'change search location' dialog,
    # and changes the search distance

    miami_user.touch # no scores, but should still show
    seattle_user.touch # no scores, but should still show

    # show search screen
    find('#btn-musician-search-builder').trigger(:click)
    wait_for_easydropdown('.builder-sort-order select')
    jk_select('Distance', '.builder-sort-order select')
    # accept search options
    find('#btn-perform-musician-search').trigger(:click)

    # only dallas is within range
    find(".musician-list-result[data-musician-id='#{dallas_user.id}']:nth-child(1)")

=begin

    find('#musician-change-filter-city').trigger(:click)

    find('h1', text: 'change search location') # dialog should be showing

    # wait for it to finish populating
    wait_for_easydropdown('#change-search-location-dialog select[name="country"]')
    wait_for_easydropdown('#change-search-location-dialog select[name="region"]')
    wait_for_easydropdown('#change-search-location-dialog select[name="city"]')

    jk_select('FL', '#change-search-location-dialog select[name="region"]') # this should be 'Florida', but our test data
    # wait for the city to not be disabled as it reloads
    expect(page).to_not have_selector('#change-search-location-dialog .field[purpose="city"] .easydropdown-wrapper.disabled')
    jk_select('Miami', '#change-search-location-dialog select[name="city"]')
    find('#change-search-location-dialog .btnSave').trigger(:click)
    find('#musician-filter-city', text: "Miami, FL")

    find(".musician-list-result[data-musician-id='#{miami_user.id}']:nth-child(1)") # only miami is within range
=end

  end

end
