require 'spec_helper'

describe "Teacher 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
    Teacher.delete_all
    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}/teacher"

    find('.header-content h1', 'REGISTER AS TEACHER')
    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_teacher_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')


    teacher = User.find_by_email('school_teacher_123@jamkazam.com')
    teacher.is_a_student.should be false
    teacher.is_a_teacher.should be true
    teacher.musician.should be true
    teacher.teacher.should_not be_nil
    teacher.school.should be_nil
    teacher.teacher.school.should eql school
    teacher.affiliate_referral.should eql school.affiliate_partner

    find('#user-profile #username', text: teacher.name)
    UserMailer.deliveries.count.should eql 2
    UserMailer.deliveries[0].html_part.body.include?("Set Up Your Teacher Profile") == 1
  end

  it "logged out and has invitation" do

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

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

    find('.header-content h1', 'REGISTER AS A TEACHER')
    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')

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

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


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

    find('.header-content h1', 'REGISTER AS A TEACHER')
    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

end
