require 'spec_helper'

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

  subject { page }

  let(:user1) { FactoryGirl.create(:user) }

  let(:amazon_2_free_card) { FactoryGirl.create(:amazon_2_free) }

  before(:all) do
    User.delete_all
    FactoryGirl.create(:teacher, ready_for_session_at: Time.now)
  end


  describe "not logged in" do
    describe "amazon_2_free_card" do
      it "succeeds" do
        visit '/account/activate/code'

        amazon_2_free_card.credits.should eql 2

        find('.instructions', text: 'Paste or enter your promotional code so we can validate it and credit you with 2 free lessons.')
        fill_in "code", with: amazon_2_free_card.code
        find('a.amazon-a-button-text', text: 'Submit Code').trigger(:click)

        find('.success-msg wbr', 'Your code has been validated!')
        fill_in "first", with: "Seth"
        fill_in "email", with: "amzposa1@jamkazam.com"
        fill_in "password", with: "jam123"
        select 'Acoustic Guitar', from: "instrument"
        find('.checkbox-input input').trigger(:click)

        find('a.amazon-a-button-text', text: 'Create Account').trigger(:click)

        find('.success-msg', "You're all set!")

        sleep 3
        user = User.find_by_email("amzposa1@jamkazam.com")
        amazon_2_free_card.reload
        amazon_2_free_card.user.should eq(user)
        amazon_2_free_card.requires_purchase.should be false
        amazon_2_free_card.purchased.should be true
        user.reload
        user.jamclass_credits.should eq(amazon_2_free_card.credits)
        user.timezone.should_not be_nil
        user.instruments.should eq([Instrument.find('acoustic guitar')])
      end

      it "validates correctly" do
        visit '/account/activate/code'

        amazon_2_free_card.credits.should eql 2

        find('.instructions', text: 'Paste or enter your promotional code so we can validate it and credit you with 2 free lessons.')
        fill_in "code", with: amazon_2_free_card.code
        find('a.amazon-a-button-text', text: 'Submit Code').trigger(:click)

        find('.success-msg wbr', 'Your code has been validated!')
        fill_in "first", with: "Seth"
        select 'Acoustic Guitar', from: "instrument"

        find('a.amazon-a-button-text', text: 'Create Account').trigger(:click)
        find('.error', text: "Email can't be blank")

        fill_in "first", with: "Seth"
        fill_in "email", with: "amzpos2@jamkazam.com"
        fill_in "password", with: "jam123"
        select 'Acoustic Guitar', from: "instrument"
        find('.checkbox-input input').trigger(:click)

        find('a.amazon-a-button-text', text: 'Create Account').trigger(:click)

        find('.success-msg', "You're all set!")

        sleep 3
        user = User.find_by_email("amzpos2@jamkazam.com")
        amazon_2_free_card.reload
        amazon_2_free_card.user.should eq(user)
        amazon_2_free_card.requires_purchase.should be false
        amazon_2_free_card.purchased.should be true
        user.reload
        user.jamclass_credits.should eq(amazon_2_free_card.credits)
        user.remind_take_lesson_times.should eql 1
        user.remind_take_lesson_at.should_not be_nil
      end
    end
  end


  describe "logged in" do
    it "succeeds" do
      fast_signin(user1, '/account/activate/code')

      find('.instructions', text: 'Paste or enter your promotional code so we can validate it and credit you with 2 free lessons.')

      fill_in "code", with: amazon_2_free_card.code

      find('a.amazon-a-button-text', text: 'Submit Code').trigger(:click)

      find('a.amazon-a-button-text', text: 'Apply Credits').trigger(:click)

      find('.success-msg', text: "Account successfully created!")

      user1.reload
      amazon_2_free_card.reload
      amazon_2_free_card.user.should eq(user1)
      amazon_2_free_card.requires_purchase.should be false
      amazon_2_free_card.purchased.should be true
      user1.jamclass_credits.should eq(amazon_2_free_card.credits)
    end
  end

  describe "logged in" do
    it "validates" do
      fast_signin(user1, '/account/activate/code')

      find('.instructions', text: 'Paste or enter your promotional code so we can validate it and credit you with 2 free lessons.')

      find('.amazon-a-button-text', text: 'Submit Code').trigger(:click)

      find('.error', text: "Code not valid")

      fill_in "code", with: amazon_2_free_card.code

      find('a.amazon-a-button-text', text: 'Submit Code').trigger(:click)

      find('a.amazon-a-button-text', text: 'Apply Credits').trigger(:click)

      find('.success-msg', text: "Account successfully created!")

      user1.reload
      amazon_2_free_card.reload
      amazon_2_free_card.user.should eq(user1)
      amazon_2_free_card.requires_purchase.should be false
      amazon_2_free_card.purchased.should be true
      user1.jamclass_credits.should eq(amazon_2_free_card.credits)
      user1.timezone.should_not be_nil
    end
  end
end
