  require 'spec_helper'

describe "Student 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
  end

  before(:each) do
    User.where('school_id is not null').update_all(school_id: nil)
    SchoolInvitation.delete_all
    School.delete_all
    UserMailer.deliveries.clear
  end



  let(:user) { FactoryGirl.create(:user, country: 'US') }
  let(:school) {FactoryGirl.create(:school, name: 'Hot Dog')}


  it "logged out and no invitation" do

    visit "/school/#{school.id}/student"

    find('.header-content h1', 'REGISTER AS STUDENT')
    find('.header-content h2', "with #{school.name}")

    find('button.cta-button', text: 'SIGN UP').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: 'school_student_123@jamkazam.com'
    fill_in "password",    with: 'jam123'
    find('.register-area ins', visible: false).trigger(:click)
    find('button.cta-button', text: 'SIGN UP').trigger(:click)

    #find('h2', text: 'my lessons')
    find('#type-label', text: 'musician')


    student = User.find_by_email('school_student_123@jamkazam.com')
    student.is_a_student.should be true
    student.is_a_teacher.should be false
    student.musician.should be true
    student.teacher.should be_nil
    student.school.should eql school
    student.affiliate_referral.should eql school.affiliate_partner
    student.origin_utm_source.should eql "organic"
    student.origin_utm_campaign.should eql "127.0.0.1"
    student.origin_utm_medium.should eql "organic"
    student.origin_referrer.should_not be_nil
    find('#user-profile #username', text: student.name)

    UserMailer.deliveries.count.should eql 2
    UserMailer.deliveries[0].html_part.body.include?("If you already know the teacher from whom you want to learn") == 1
  end

  it "logged out and has invitation" do

    school_invitation = FactoryGirl.create(:school_invitation, school: school, accepted: false, as_teacher: false)

    visit "/school/#{school.id}/student?invitation_code=#{school_invitation.invitation_code}"

    find('.header-content h1', 'REGISTER AS STUDENT')
    find('.header-content h2', "with #{school.name}")

    find('button.cta-button', text: 'SIGN UP').trigger(:click)

    # should fail because we haven't filled out email/password/terms
    find('.register-area .errors', text: "Password is too short (minimum is 6 characters)")

    #fill_in "email",    with: 'school_student_124@jamkazam.com'
    fill_in "password",    with: 'jam123'
    find('.register-area ins', visible: false).trigger(:click)
    find('button.cta-button', text: 'SIGN UP').trigger(:click)

    #find('h2', text: 'my lessons')
    find('#type-label', text: 'musician')

    student = User.find_by_email(school_invitation.email)
    student.is_a_student.should be true
    student.is_a_teacher.should be false
    student.musician.should be true
    student.teacher.should be_nil
    student.school.should eql school
    student.affiliate_referral.should eql school.affiliate_partner

    school_invitation.reload
    school_invitation.accepted.should be true
    find('#user-profile #username', text: student.name)
  end


  it "logged in" do
    school.touch
    fast_signin(user,"/school/#{school.id}/student")

    find('.header-content h1', 'REGISTER AS  STUDENT')
    find('.header-content h2', "with #{school.name}")

    find('button.cta-button', text: 'GO TO JAMKAZAM').trigger(:click)

    # this should show on the /client#/home page (WILL CHANGE)
    #find('h2', text: 'my lessons')
    find('#type-label', text: 'musician')
    find('#user-profile #username', text: user.name)

    user.reload
    user.affiliate_referral.should be_nil
    #user.is_a_student.should be true
    #user.is_a_teacher.should be false
    #user.musician.should be true
  end

  it "user origin" do

    visit "/school/#{school.id}/student?utm_source=abc&utm_medium=ads&utm_campaign=campaign1"

    find('.header-content h1', 'REGISTER AS STUDENT')
    find('.header-content h2', "with #{school.name}")

    find('button.cta-button', text: 'SIGN UP').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: 'school_student_125@jamkazam.com'
    fill_in "password",    with: 'jam123'
    find('.register-area ins', visible: false).trigger(:click)
    find('button.cta-button', text: 'SIGN UP').trigger(:click)

    #find('h2', text: 'my lessons')
    find('#type-label', text: 'musician')


    student = User.find_by_email('school_student_125@jamkazam.com')
    student.is_a_student.should be true
    student.is_a_teacher.should be false
    student.musician.should be true
    student.teacher.should be_nil
    student.school.should eql school
    student.origin_utm_source.should eql "abc"
    student.origin_utm_campaign.should eql "campaign1"
    student.origin_utm_medium.should eql "ads"
    student.origin_referrer.should be_nil
    student.affiliate_referral.should eql school.affiliate_partner

    find('#user-profile #username', text: student.name)

    UserMailer.deliveries.count.should eql 2
    UserMailer.deliveries[0].html_part.body.include?("If you already know the teacher from whom you want to learn") == 1
  end

end
