require 'spec_helper'

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

  let(:user) { FactoryGirl.create(:user, has_redeemable_jamtrack: true) }
  let(:jt_us) { FactoryGirl.create(:jam_track, :name=>'jt_us', sales_region: 'United States', make_track: true, original_artist: "foobar", price:2.99) }
  let(:jt_ww) { FactoryGirl.create(:jam_track, :name=>'jt_ww', sales_region: 'Worldwide', make_track: true, original_artist: "barfoo", price:3.99, allow_free: false) }
  let(:jt_rock) { FactoryGirl.create(:jam_track, :name=>'the live', genres: [JamRuby::Genre.find('rock')], make_track: true, original_artist: "the food") }
  let(:jt_blues) { FactoryGirl.create(:jam_track, :name=>'live dog', genres: [JamRuby::Genre.find('blues')], make_track: true, original_artist: "food dog") }

  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
    ShoppingCart.delete_all
    JamTrackRight.delete_all
    JamTrack.delete_all
    JamTrackTrack.delete_all
    JamTrackLicensor.delete_all

    stub_const("APP_CONFIG", web_config)

  end

  it "searches for exact artist name" do
    jt_rock.touch
    jt_blues.touch
    fast_signin user, "/client?artist=the food#/jamtrack/search"
    find('h1', text: 'search jamtracks')
    find('.jamtrack-record[data-jamtrack-id="' + jt_rock.id + '"]')
    page.assert_selector('.jamtrack-record', count: 1)
  end


  it "searches for exact song name" do
    jt_rock.touch
    jt_blues.touch
    fast_signin user, "/client?song=live dog#/jamtrack/search"
    find('h1', text: 'search jamtracks')
    find('.jamtrack-record[data-jamtrack-id="' + jt_blues.id + '"]')
    page.assert_selector('.jamtrack-record', count: 1)
  end

  it "search repeatedly" do

    fast_signin user, "/client#/jamtrack/search"
    find('h1', text: 'search jamtracks')

    find('#jamtrackSearch .Select-control').trigger(:mousedown)

    # wait for the 'Type to search' prompt to show
    find('#jamtrackSearch .Select-search-prompt')

    send_key_sequence('#jamtrackSearch .Select-control', 'abc')

    jt_us.touch

    find('.Select-input').trigger(:blur)
    find('#jamtrackSearch .search-btn').trigger(:click)

    find('#jamtrackSearch .Select-control').trigger(:mousedown)
    send_key_sequence('#jamtrackSearch .Select-control', 'foo')

    #screenshot_and_open_image

    find('.jamtrack-record[data-jamtrack-id="' + jt_us.id + '"]')
    find('.show-artist', text: jt_us.original_artist).trigger(:click)

    find('h2.jamtrack-results-header', text: "search results: jamtracks for artist \"#{jt_us.original_artist}\"")
    find('.jamtrack-record[data-jamtrack-id="' + jt_us.id + '"]')


    # TODO: do a second search.  Unfortunately, i Can't figure out how to simulate events to cause the search bar to reset
    #jt_blues.touch
    #find('.Select-input').trigger(:blur)
    #find('#jamtrackSearch .Select-control').trigger(:mousedown)
    #send_key_sequence('#jamtrackSearch .Select-control', 'foo')

    #find('.jamtrack-record[data-jamtrack-id="' + jt_blues.id + '"]')
    #screenshot_and_open_image

    #jt_ww.touch
    #jt_rock.touch
    #jt_blues.touch

  end

  it "search with non-free and free jamtracks in the mix" do
    jt_us.touch
    jt_ww.touch

    fast_signin user, "/client#/jamtrack/search"
    find('h1', text: 'search jamtracks')
    find('#jamtrackSearch .search-btn').trigger(:click)


    find('.jamtrack-record[data-jamtrack-id="' + jt_us.id + '"]')

    find('a.jamtrack-add-cart.is_free[data-jamtrack-id="' + jt_us.id + '"]', text: 'GET IT FREE!')
    find('.jamtrack-record[data-jamtrack-id="' + jt_ww.id + '"] .jamtrack-price.non-free', text: "$ #{jt_ww.price}")
    find('a.jamtrack-add-cart[data-jamtrack-id="' + jt_ww.id + '"][data-variant="stream"]', text: 'ADD TO CART').trigger(:click)
    find('h1', text: 'shopping cart')

    find('.cart-item-price', text: "$ #{jt_ww.price}")

    find('a', text: 'CONTINUE SHOPPING').trigger(':click')

    find('a.jamtrack-add-cart.is_free[data-jamtrack-id="' + jt_us.id + '"][data-variant="full"]', text: 'GET IT FREE!').trigger(:click)

    find('.cart-item-price', text: "$ #{jt_us.download_price}")
    find('.cart-item-price', text: "$ #{jt_ww.price}")

  end
end
