require 'spec_helper'

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

  let(:user) { FactoryGirl.create(:user, gifted_jamtracks: 0, has_redeemable_jamtrack: false) }
  let(:jt_us) { FactoryGirl.create(:jam_track, :name=>'jt_us', sales_region: 'Worldwide', 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") }
  let(:jt_rock) { FactoryGirl.create(:jam_track, :name=>'jt_rock', genres: [JamRuby::Genre.find('rock')], make_track: true, original_artist: "badfood") }
  let(:jt_blues) { FactoryGirl.create(:jam_track, :name=>'jt_blues', genres: [JamRuby::Genre.find('blues')], make_track: true, original_artist: "foodbart") }

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


  before(:each) do
    ShoppingCart.delete_all
    JamTrackRight.delete_all
    JamTrack.delete_all
    JamTrackTrack.delete_all
    JamTrackLicensor.delete_all

    jt_us.touch
    jt_ww.touch
    jt_rock.touch
    jt_blues.touch

    stub_const("APP_CONFIG", web_config)

    fast_signin user, "/client#/home"
    find('.homecard.jamtrack h2', text: 'jamtracks')
  end

  def find_jamtrack jamtrack, options = {}
    jamtrack_record = find(".jamtrack-record[data-jamtrack-id=\"#{jamtrack.id}\"]")    
    #jamtrack_record.find('.detail-value', text: jamtrack.name)
    #jamtrack_record.find('.detail-value', text: jamtrack.recording_type)
    #jamtrack_record.find('.detail-value', text: jamtrack.original_artist)
    # jamtrack_record.find('.detail-value', text: jamtrack.genre.description)
    # jamtrack_record.find('.detail-value', text: [jamtrack.songwriter, jamtrack.publisher].join(', '))
    # jamtrack_record.find('.copyright-value', text: jamtrack.licensor.name)
    jamtrack_record.find('.jamtrack-price', text: "$ #{jamtrack.price}")

    if jamtrack.sales_region == 'United States'
      jamtrack_record.find('a.license-us-why', text: 'why?').trigger(:click)
      find('#jamtrack-availability-dialog h1', text: 'JamTrack Availability:')
      find('#jamtrack-availability-dialog #dialog-close-button').trigger(:click)
    end

    if options[:added_cart]
      jamtrack_record.find('a.jamtrack-add-cart-disabled', text: 'ALREADY IN CART')
    else
      jamtrack_record.find('a.jamtrack-add-cart.button-orange', text: 'ADD TO CART')
    end
    jamtrack_record
  end

  def not_find_jamtrack jamtrack
    should_not have_selector(".jamtrack-record[data-jamtrack-id=\"#{jamtrack.id}\"]")
  end
  
  describe "Shopping Carts" do

    before(:each) do
      visit "/client#/jamtrack/search"
      find('h1', text: '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', jt_us.name)
      find('#jamtrackSearch .search-btn').trigger(:click)
    end

    it "adds/deletes JamTrack to/from Cart" do

      find("a.jamtrack-add-cart[data-jamtrack-id=\"#{jt_us.id}\"]").trigger(:click)

      find('h1', text: 'shopping cart')
      find('.cart-item-caption', text: "JamTrack: #{jt_us.name}")
      find('.cart-item-price', text: "$ #{jt_us.price}")

      find('a.button-orange', text: 'CONTINUE SHOPPING').trigger(:click)
      find('button.disabled.search-btn')

      find_jamtrack jt_us, {added_cart: true}

      find('a.header-shopping-cart').trigger(:click)
      find("a.remove-cart").trigger(:click)
      find('a.button-orange', text: 'CONTINUE SHOPPING').trigger(:click)
      find('button.disabled.search-btn')

      find_jamtrack jt_us

      find("a.jamtrack-add-cart[data-jamtrack-id=\"#{jt_us.id}\"]").trigger(:click)
      find('.shopping-sub-total', text: "Subtotal:$ #{jt_us.price}")
      find('a.button-orange', text: 'CONTINUE SHOPPING').trigger(:click)
      find('button.disabled.search-btn')

      find_jamtrack jt_ww

      find("a.jamtrack-add-cart[data-jamtrack-id=\"#{jt_ww.id}\"]").trigger(:click)
      find('.shopping-sub-total', text: "Subtotal:$ #{jt_us.price + jt_ww.price}")
      #find('a.button-orange', text: 'CONTINUE SHOPPING').trigger(:click)

    end

    it "can expand" do
      jamtrack = find_jamtrack(jt_us)
      jamtrack.find('.jamtrack-detail-btn').trigger(:click)
      
      jt_us.jam_track_tracks.each do |track|
        jamtrack.find('.instrument-name', track.instrument.description)
        jamtrack.find('.part', "(#{track.part}}")
      end
    end

  end
end
