require 'spec_helper'

# tests what happens when the websocket connection goes away
describe "Redeem Gift Card", :js => true, :type => :feature, :capybara_feature => true do

  subject { page }

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

  before(:all) do
    User.delete_all
    JamTrack.delete_all

    @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 "not logged in" do
    it "suceeeds" do
      visit '/redeem_giftcard'

      find('h2', text:'Redeem Your Gift Card')
      fill_in "code",     with: gift_card.code
      fill_in "email",    with: "gifter1@jamkazam.com"
      fill_in "password", with: "jam123"
      find('.redeem-container ins').trigger(:click)

      find('button.redeem-giftcard').trigger(:click)

      find('.done-action a.go-browse').trigger(:click)

      find('.free-jamtrack')

      user = User.find_by_email("gifter1@jamkazam.com")
      gift_card.reload
      gift_card.user.should eq(user)
      user.reload
      user.gifted_jamtracks.should eq(5)
    end

    it "validates correctly" do
      visit '/redeem_giftcard'

      find('h2', text:'Redeem Your Gift Card')

      find('button.redeem-giftcard').trigger(:click)

      find('.errors.active', text: "Email can't be blank")

      find('h2', text:'Redeem Your Gift Card')
      fill_in "code",     with: gift_card.code
      fill_in "email",    with: "gifter2@jamkazam.com"
      fill_in "password", with: "jam123"
      find('.redeem-container ins').trigger(:click)

      find('button.redeem-giftcard').trigger(:click)

      find('.done-action a.go-browse').trigger(:click)

      find('.free-jamtrack')

      user = User.find_by_email("gifter2@jamkazam.com")
      gift_card.reload
      gift_card.user.should eq(user)
      user.reload
      user.gifted_jamtracks.should eq(5)
    end

    it "converts shopping cart items to free" do

      visit '/redeem_giftcard'

      anon_user_id = page.driver.cookies["user_uuid"]
      anon_user = AnonymousUser.new(anon_user_id.value, {})

      cart = ShoppingCart.add_jam_track_to_cart(anon_user, jamtrack_acdc_backinblack)
      cart.skip_mix_check = true
      cart.marked_for_redeem = 0
      cart.save!

      find('h2', text:'Redeem Your Gift Card')
      fill_in "code",     with: gift_card.code
      fill_in "email",    with: "gifter_carted1@jamkazam.com"
      fill_in "password", with: "jam123"
      find('.redeem-container ins').trigger(:click)

      find('button.redeem-giftcard').trigger(:click)

      find('.done-action a.go-browse').trigger(:click)

      find('.free-jamtrack')

      cart.reload
      cart.marked_for_redeem.should eq(1)

    end
  end

  describe "logged in" do
    it "succeeds" do
      fast_signin(user1, '/redeem_giftcard')

      find('h2', text:'Redeem Your Gift Card')
      fill_in "code",     with: gift_card.code

      find('button.redeem-giftcard').trigger(:click)

      find('.done-action a.go-browse').trigger(:click)

      find('.free-jamtrack')

      gift_card.reload
      gift_card.user.should eq(user1)
      user1.reload
      user1.gifted_jamtracks.should eq(5)
    end
  end

  describe "logged in" do
    it "validates" do
      fast_signin(user1, '/redeem_giftcard')

      find('h2', text:'Redeem Your Gift Card')

      find('button.redeem-giftcard').trigger(:click)

      find('.errors.active', text: "Gift Card Code does not exist")

      fill_in "code",     with: gift_card.code

      find('button.redeem-giftcard').trigger(:click)

      find('.done-action a.go-browse').trigger(:click)

      find('.free-jamtrack')

      gift_card.reload
      gift_card.user.should eq(user1)
      user1.reload
      user1.gifted_jamtracks.should eq(5)
    end

    it "converts shopping cart items to free" do

      fast_signin(user1, '/redeem_giftcard')
      cart = ShoppingCart.add_jam_track_to_cart(user1, jamtrack_acdc_backinblack)
      cart.skip_mix_check = true
      cart.marked_for_redeem = 0
      cart.save!

      visit '/redeem_giftcard'

      find('h2', text:'Redeem Your Gift Card')

      fill_in "code",     with: gift_card.code
      find('button.redeem-giftcard').trigger(:click)
      find('.done-action a.go-browse').trigger(:click)
      find('.free-jamtrack')

      gift_card.reload
      gift_card.user.should eq(user1)
      user1.reload
      user1.gifted_jamtracks.should eq(5)
      cart.reload
      cart.marked_for_redeem.should eq(1)
    end
  end
end
