require 'spec_helper'

describe "Individual JamTrack", :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
  end

  let(:user) { FactoryGirl.create(:user) }
  let(:jamtrack_acdc_backinblack) { @jamtrack_acdc_backinblack }
  let(:jamtrack_not_free) {@jamtrack_not_free}

  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'
    }
  }


  before(:all) do
    @jamtrack_acdc_backinblack = FactoryGirl.create(:jam_track, name: 'Back in Black', original_artist: 'AC/DC', sales_region: 'United States', make_track: true, plan_code: 'jamtrack-acdc-backinblack')
    @jamtrack_not_free = FactoryGirl.create(:jam_track, price: 2.99, name: 'Not Free', original_artist: 'AC/DC', sales_region: 'United States', make_track: true, plan_code: 'jamtrack-not-free', allow_free: false)
 end


  describe "AC/DC Back in Black" do

    it "logged out" do
      visit "/landing/jamtracks/#{@jamtrack_acdc_backinblack.slug}"

      find('h1.jam-track-name', text: jamtrack_acdc_backinblack.name.upcase)
      find('h2.original-artist', text: "by " + jamtrack_acdc_backinblack.original_artist.upcase)
      jamtrack_acdc_backinblack.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 include("/client?search=#/jamtrack/search")
      find('button.cta-button', text: 'GET IT FREE!').trigger(:click)

      # should fail because we haven't filled out email/password/terms
      find('.register-area .errors', text: "Email can't be blank")

      fill_in "email",    with: 'testuser_123@jamkazam.com'
      fill_in "password",    with: 'jam123'
      find('.terms-checkbox').trigger(:click)
      find('button.cta-button', text: 'GET IT FREE!').trigger(:click)

      # this should show on the /client#/jamtrack page
      find('.no-free-jamtrack')
      # make sure we can find the webplayer link
      find('.purchased-jam-tracks .play-jamtrack')

      # now go back, and then try to click checkout again
      visit "/landing/jamtracks/#{@jamtrack_acdc_backinblack.slug}"
      find('button.cta-button', text: 'Add To Cart')
      find('.price-advisory', text:"$2.99")
      find('button.cta-button').trigger(:click)
      find('h1', text: 'shopping cart')

      user = User.find_by_email!("testuser_123@jamkazam.com")
      user.origin_utm_source.should eql "organic"
      user.origin_utm_campaign.should eql "127.0.0.1"
      user.origin_utm_medium.should eql "organic"
      user.origin_referrer.should_not be_nil
    end

    it "logged out on non-free JamTrack" do
      visit "/landing/jamtracks/#{jamtrack_not_free.slug}"

      find('h1.jam-track-name', text: jamtrack_not_free.name.upcase)
      find('h2.original-artist', text: "by " + jamtrack_not_free.original_artist.upcase)
      jamtrack_acdc_backinblack.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 include("/client?search=#/jamtrack/search")
      find('.price-advisory', text: '$2.99')
      find('button.cta-button', text: 'Add To Cart').trigger(:click)

      # should fail because we haven't filled out email/password/terms
      find('.register-area .errors', text: "Email can't be blank")

      fill_in "email",    with: 'testuser_nonfree@jamkazam.com'
      fill_in "password",    with: 'jam123'
      find('.terms-checkbox').trigger(:click)
      find('button.cta-button', text: 'Add To Cart').trigger(:click)

      # should be in the shopping cart page
      find('h1', text: 'shopping cart')

      user = User.find_by_email!("testuser_nonfree@jamkazam.com")
      user.origin_utm_source.should eql "organic"
      user.origin_utm_campaign.should eql "127.0.0.1"
      user.origin_utm_medium.should eql "organic"
      user.origin_referrer.should_not be_nil
    end

    it "logged out with origin info" do
      visit "/landing/jamtracks/#{@jamtrack_acdc_backinblack.slug}?utm_source=abc&utm_medium=ads&utm_campaign=campaign1"

      find('h1.jam-track-name', text: jamtrack_acdc_backinblack.name.upcase)
      find('h2.original-artist', text: "by " + jamtrack_acdc_backinblack.original_artist.upcase)
      jamtrack_acdc_backinblack.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 include("/client?search=#/jamtrack/search")
      find('button.cta-button', text: 'GET IT FREE!').trigger(:click)

      # should fail because we haven't filled out email/password/terms
      find('.register-area .errors', text: "Email can't be blank")

      fill_in "email",    with: 'testuser_123_o@jamkazam.com'
      fill_in "password",    with: 'jam123'
      find('.terms-checkbox').trigger(:click)
      find('button.cta-button', text: 'GET IT FREE!').trigger(:click)

      # this should show on the /client#/jamtrack page
      find('.no-free-jamtrack')

      user = User.find_by_email!("testuser_123_o@jamkazam.com")
      user.origin_utm_source.should eql "abc"
      user.origin_utm_campaign.should eql "campaign1"
      user.origin_utm_medium.should eql "ads"
      user.origin_referrer.should be_nil
    end

    it "logged in" do
      fast_signin(user, "/landing/jamtracks/#{@jamtrack_acdc_backinblack.slug}")

      find('h1.jam-track-name', text: jamtrack_acdc_backinblack.name.upcase)
      find('h2.original-artist', text: "by " + jamtrack_acdc_backinblack.original_artist.upcase)
      jamtrack_acdc_backinblack.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('button.cta-button').trigger(:click)
      find('.browse-jamtracks', text: 'search jamtracks')

    end
  end
end
