require 'spec_helper'

describe "Product Pages", :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 }


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

  end


  describe "JamBlaster" do

    before(:all) do
      @old_recaptcha=Rails.application.config.recaptcha_enable
      Rails.application.config.recaptcha_enable=false
    end

    after(:all) do
      Rails.application.config.recaptcha_enable=@old_recaptcha
    end

    it "logged out and then request jamblaster via sign in" do
      visit "/products/jamblaster"

      find('h1', text: 'The JamBlaster by JamKazam')

      find('a.white-bordered-button').trigger(:click)
      find('#banner h1', text: 'please sign in or sign up').trigger(:click)
      find("#banner a", text: 'SIGN IN').trigger(:click)


      # now we are at the sign in page
      page.should have_selector('form.signin-form')
      within('form.signin-form') do
        fill_in "Email Address:",    with: user.email
        fill_in "Password:", with: user.password
        click_button "SIGN IN"
      end

      # should be back loat the jamblaster product page, and told we placed our order
      find('h1', text: 'The JamBlaster by JamKazam')
      find('#banner h1', text: 'virtual order placed')

      user.reload
      user.want_jamblaster.should be true
    end

    it "logged out and then request jamblaster via sign up" do
      visit "/products/jamblaster"

      find('h1', text: 'The JamBlaster by JamKazam')

      find('a.white-bordered-button').trigger(:click)
      find('#banner h1', text: 'please sign in or sign up').trigger(:click)
      find("#banner a", text: 'SIGN UP').trigger(:click)

      # we are now at the sign up page
      fill_in "jam_ruby_user[first_name]",    with: "Mike"
      fill_in "jam_ruby_user[last_name]", with: "Jones"
      fill_in "jam_ruby_user[email]", with: "jamblaster_lover@jamkazam.com"
      fill_in "jam_ruby_user[password]", with: "jam123"
      fill_in "jam_ruby_user[password_confirmation]", with: "jam123"
      check("jam_ruby_user[instruments][drums][selected]")
      check("jam_ruby_user[terms_of_service]")
      click_button "CREATE ACCOUNT"

      # should be back at the jamblaster product page, and told we placed our order
      find('h1', text: 'The JamBlaster by JamKazam')
      find('#banner h1', text: 'virtual order placed')

      some_user = User.find_by_email!('jamblaster_lover@jamkazam.com')
      some_user.want_jamblaster.should be true
    end

    it "logged in" do
      fast_signin(user, "/products/jamblaster")

      find('h1', text: 'The JamBlaster by JamKazam')
      find('a.white-bordered-button').trigger(:click)
      find('#banner', text: 'please confirm')
      find('#banner a.yes-btn').trigger(:click)
      find('#banner h1', text: 'virtual order placed')

      user.reload
      user.want_jamblaster.should be true
    end

  end

  describe "Platform" do
    it "logged out" do
      visit "/products/platform"

      find('h1', text: 'The JamKazam Platform')
      find('a.white-bordered-button').trigger(:click)

      find('h2', text: 'Create your free JamKazam account')
    end

    it "logged in" do
      fast_signin(user, "/products/platform")

      find('h1', text: 'The JamKazam Platform')
      find('a.white-bordered-button').trigger(:click)

      # clicking /signup just redirects you to the client
      find('h2', text: 'create session')
    end
  end

  describe "JamTracks" do
    it "logged out" do
      visit "/products/jamtracks"

      find('h1', text: 'JamTracks by JamKazam')
      find('a.white-bordered-button').trigger(:click)

      find('h1', text: 'jamtracks')
    end

    it "logged in" do
      fast_signin(user, "/products/jamtracks")

      find('h1', text: 'JamTracks by JamKazam')
      find('a.white-bordered-button').trigger(:click)

      find('h1', text: 'jamtracks')
    end
  end
end
