require 'spec_helper'

describe "Gift Card Landing", :js => true, :type => :feature, :capybara_feature => true do

  subject { page }

  before(:all) do
    ShoppingCart.delete_all
    JamTrackRight.delete_all
    JamTrack.delete_all
    JamTrackTrack.delete_all
    JamTrackLicensor.delete_all
    GiftCardPurchase.delete_all
    GiftCard.delete_all
  end

  before(:all) do
    @jamtrack_rocketman = FactoryGirl.create(:jam_track, slug: 'elton-john-rocket-man', name: 'Rocket Man', original_artist: 'Elton John', sales_region: 'United States', make_track: true, plan_code: 'jamtrack-acdc-backinblack')
  end


  let(:jamtrack_rocketman) {@jamtrack_rocketman}
  let(:user) { FactoryGirl.create(:user, country: 'US') }

  let(:billing_info) {
    {
        first_name: 'Seth',
        last_name: 'Call',
        address1: '10704 Buckthorn Drive',
        city: 'Austin',
        state: 'Texas',
        country: 'US',
        zip: '78759',
        number: '4111111111111111',
        month: '08',
        year: '2017',
        verification_value: '012'
    }
  }

  it "logged out (5) and affiliate" do
    partner = FactoryGirl.create(:affiliate_partner)
    affiliate_params = partner.affiliate_query_params
    visit "/landing/gift-card?" + affiliate_params

    find('h1.jam-track-name', '$10 or $20 JAMTRACKS GIFT CARDS')
    find('h2.original-artist', 'A PERFECT GIFT FOR THE HOLIDAYS')
    jamtrack_rocketman.jam_track_tracks.each do |track|
      if track.master?
        find('.tracks.previews[data-id="' + track.id + '"] img.instrument-icon[data-instrument-id="other"]')
        find('.tracks.previews[data-id="' + track.id + '"] .instrument-name', text: 'Master Mix')
      else
        find('.tracks.previews[data-id="' + track.id + '"] img.instrument-icon[data-instrument-id="' + track.instrument.id + '"]')
        find('.tracks.previews[data-id="' + track.id + '"] .instrument-name', text: track.instrument.description)
      end
    end
    find('a.browse-all')['href'].should eq("/client?search=#/jamtrack/search")
    find('button.five-jt', text: 'ADD $10 CARD TO CART').trigger(:click)

    # land in shopping cart first
    find('.proceed-checkout').trigger(:click)

    # checkoutSignin page now
    find('h3', text: 'NOT A MEMBER?')

    # hit 'NEXT' to create account and enter payment info
    find('.btnNext.button-orange').trigger(:click)

    find('.hint.cvv')

    # fill out all billing info and account info
    fill_in 'billing-first-name', with: 'Seth'
    fill_in 'billing-last-name', with: 'Call'
    fill_in 'billing-address1', with: '10704 Buckthorn Drive'
    fill_in 'billing-city', with: 'Austin'
    fill_in 'billing-state', with: 'Texas'
    fill_in 'billing-zip', with: '78759'
    fill_in 'card-number', with: '4111111111111111'
    fill_in 'card-verify', with: '012'

    within('#checkout-payment-info') do
      # fill in user/email/tos
      fill_in 'email', with: 'bogus+gc1@jamkazam.com'
      fill_in 'password', with: 'jam123'
    end

    find('#divJamKazamTos ins.iCheck-helper').trigger(:click) # accept TOS

    # try to submit, and see order page
    find('#payment-info-next').trigger(:click)

    find('.order-items-value.sub-total', text:'10.00')
    find('.order-items-value.taxes', text:'0.83')
    find('.order-items-value.order-total', text:'$10.00')
    find('.order-items-value.grand-total', text:'$10.83')

    # click the ORDER button
    find('.place-order-center a.button-orange.place-order').trigger(:click)

    # and now we should see confirmation, and a notice that we are in a normal browser
    find('.thanks-detail.gift-card')

    created_user = User.find_by_email('bogus+gc1@jamkazam.com')

    sleep 3 # challenge to all comers!  WHY DO I HAVE TO SLEEP FOR THIS ASSERTION TO BE TRUE! GAH .  and 1 second won't do it

    created_user.reload
    created_user.has_redeemable_jamtrack.should be true
    created_user.gifted_jamtracks.should eq(0)
    created_user.gift_card_purchases.length.should eq(1)

    # verify sales data
    created_user.sales.length.should eq(1)
    sale = created_user.sales.last
    sale.sale_line_items.length.should eq(1)
    line_item = sale.sale_line_items[0]
    line_item.product_type.should eq('GiftCardType')
    line_item.product_id.should eq('jam_tracks_5')
    line_item.quantity.should eq(1)
    line_item.free.should eq(0)
    line_item.unit_price.should eq(10.00)
    line_item.sale.should eq(sale)
    line_item.affiliate_referral.should eq(partner)
    line_item.affiliate_refunded.should be false
    line_item.affiliate_refunded_at.should be_nil
    line_item.affiliate_referral_fee_in_cents.should eq(10.00 * partner.rate * 100)
  end

  it "logged out (10)" do
    visit "/landing/gift-card"

    find('h1.jam-track-name', '$10 or $20 JAMTRACKS GIFT CARDS')
    find('h2.original-artist', 'A PERFECT GIFT FOR THE HOLIDAYS')
    jamtrack_rocketman.jam_track_tracks.each do |track|
      if track.master?
        find('.tracks.previews[data-id="' + track.id + '"] img.instrument-icon[data-instrument-id="other"]')
        find('.tracks.previews[data-id="' + track.id + '"] .instrument-name', text: 'Master Mix')
      else
        find('.tracks.previews[data-id="' + track.id + '"] img.instrument-icon[data-instrument-id="' + track.instrument.id + '"]')
        find('.tracks.previews[data-id="' + track.id + '"] .instrument-name', text: track.instrument.description)
      end
    end
    find('a.browse-all')['href'].should eq("/client?search=#/jamtrack/search")
    find('button.ten-jt', text: 'ADD $20 CARD TO CART').trigger(:click)

    # land in shopping cart first
    find('.proceed-checkout').trigger(:click)

    # checkoutSignin page now
    find('h3', text: 'NOT A MEMBER?')

    # hit 'NEXT' to create account and enter payment info
    find('.btnNext.button-orange').trigger(:click)

    find('.hint.cvv')

    # fill out all billing info and account info
    fill_in 'billing-first-name', with: 'Seth'
    fill_in 'billing-last-name', with: 'Call'
    fill_in 'billing-address1', with: '10704 Buckthorn Drive'
    fill_in 'billing-city', with: 'Austin'
    fill_in 'billing-state', with: 'Texas'
    fill_in 'billing-zip', with: '78759'
    fill_in 'card-number', with: '4111111111111111'
    fill_in 'card-verify', with: '012'

    within('#checkout-payment-info') do
      # fill in user/email/tos
      fill_in 'email', with: 'bogus+gc2@jamkazam.com'
      fill_in 'password', with: 'jam123'
    end

    find('#divJamKazamTos ins.iCheck-helper').trigger(:click) # accept TOS

    # try to submit, and see order page
    find('#payment-info-next').trigger(:click)

    find('.order-items-value.sub-total', text:'20.00')
    find('.order-items-value.taxes', text:'1.65')
    find('.order-items-value.order-total', text:'$20.00')
    find('.order-items-value.grand-total', text:'$21.65')

    # click the ORDER button
    find('.place-order-center a.button-orange.place-order').trigger(:click)

    # and now we should see confirmation, and a notice that we are in a normal browser
    find('.thanks-detail.gift-card')

    created_user = User.find_by_email('bogus+gc2@jamkazam.com')

    sleep 3 # challenge to all comers!  WHY DO I HAVE TO SLEEP FOR THIS ASSERTION TO BE TRUE! GAH .  and 1 second won't do it

    created_user.reload
    created_user.has_redeemable_jamtrack.should be true
    created_user.gifted_jamtracks.should eq(0)
    created_user.gift_card_purchases.length.should eq(1)

    # verify sales data
    created_user.sales.length.should eq(1)
    sale = created_user.sales.last
    sale.sale_line_items.length.should eq(1)
    line_item = sale.sale_line_items[0]
    line_item.product_type.should eq('GiftCardType')
    line_item.product_id.should eq('jam_tracks_10')
    line_item.quantity.should eq(1)
    line_item.free.should eq(0)
    line_item.unit_price.should eq(20.00)
    line_item.sale.should eq(sale)
  end

  it "logged in (5)" do
    fast_signin(user,"/landing/gift-card")

    find('h1.jam-track-name', '$10 or $20 JAMTRACKS GIFT CARDS')
    find('h2.original-artist', 'A PERFECT GIFT FOR THE HOLIDAYS')
    jamtrack_rocketman.jam_track_tracks.each do |track|
      if track.master?
        find('.tracks.previews[data-id="' + track.id + '"] img.instrument-icon[data-instrument-id="other"]')
        find('.tracks.previews[data-id="' + track.id + '"] .instrument-name', text: 'Master Mix')
      else
        find('.tracks.previews[data-id="' + track.id + '"] img.instrument-icon[data-instrument-id="' + track.instrument.id + '"]')
        find('.tracks.previews[data-id="' + track.id + '"] .instrument-name', text: track.instrument.description)
      end
    end
    find('a.browse-all')['href'].should eq("/client?search=#/jamtrack/search")
    find('button.five-jt', text: 'ADD $10 CARD TO CART').trigger(:click)

    # land in shopping cart because we are a user; log in
    find('.proceed-checkout').trigger(:click)

    find('h1', text: 'check out')

    # fill out all billing info and account info
    fill_in 'billing-first-name', with: 'Seth'
    fill_in 'billing-last-name', with: 'Call'
    fill_in 'billing-address1', with: '10704 Buckthorn Drive'
    fill_in 'billing-city', with: 'Austin'
    fill_in 'billing-state', with: 'Texas'
    fill_in 'billing-zip', with: '78759'
    fill_in 'card-number', with: '4111111111111111'
    fill_in 'card-verify', with: '012'

    # try to submit, and see order page
    find('#payment-info-next').trigger(:click)

    find('.order-items-value.sub-total', text:'10.00')
    find('.order-items-value.taxes', text:'0.83')
    find('.order-items-value.order-total', text:'$10.00')
    find('.order-items-value.grand-total', text:'$10.83')

    # click the ORDER button
    find('.place-order-center a.button-orange.place-order').trigger(:click)

    # and now we should see confirmation, and a notice that we are in a normal browser
    find('.thanks-detail.gift-card')

    user.reload

    sleep 3 # challenge to all comers!  WHY DO I HAVE TO SLEEP FOR THIS ASSERTION TO BE TRUE! GAH .  and 1 second won't do it

    user.reload
    user.has_redeemable_jamtrack.should be true
    user.gifted_jamtracks.should eq(0)
    user.gift_card_purchases.length.should eq(1)
  end

end
