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


  let(:user) { FactoryGirl.create(:user, country: 'US') }


  it "logged out" do
    visit "/landing/jamclass/students"

    find('h1.jamclass-h1', 'Let Us Find You The Perfect Music Teacher')
    find('h2.jamclass-h2', 'And Connect You Online With Our Patented, Unique Technology')

    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: '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('h3', text: 'Student Levels Taught:')

    user = User.find_by_email('student_123@jamkazam.com')
    user.is_a_student.should be true
    user.is_a_teacher.should be false
    user.musician.should be true
    user.origin_utm_source.should eql "organic"
    user.origin_utm_campaign.should eql "127.0.0.1"
    user.origin_utm_medium.should eql "organic"
    user.origin_referrer.should_not be_nil
  end

  it "logged out with origin info" do
    visit "/landing/jamclass/students?utm_source=abc&utm_medium=ads&utm_campaign=campaign1"

    find('h1.jamclass-h1', 'Let Us Find You The Perfect Music Teacher')
    find('h2.jamclass-h2', 'And Connect You Online With Our Patented, Unique Technology')

    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: '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('h3', text: 'Student Levels Taught:')

    user = User.find_by_email('student_125@jamkazam.com')
    user.is_a_student.should be true
    user.is_a_teacher.should be false
    user.musician.should be true
    user.origin_utm_source.should eql "abc"
    user.origin_utm_campaign.should eql "campaign1"
    user.origin_utm_medium.should eql "ads"
    user.origin_referrer.should be_nil
  end


  it "logged in" do
    fast_signin(user, "/landing/jamclass/students")

    find('h1.jamclass-h1', 'Let Us Find You The Perfect Music Teacher')
    find('h2.jamclass-h2', 'And Connect You Online With Our Patented, Unique Technology')

    find('button.cta-button', text: 'TRY TESTDRIVE').trigger(:click)

    find('h3', text: 'Student Levels Taught:')

    user.reload
    user.is_a_student.should be true
    user.is_a_teacher.should be false
    user.musician.should be true
  end

  it "logged in with 4-pack" do
    package = FactoryGirl.create(:test_drive_package, :four_pack)

    fast_signin(user, "/landing/jamclass/students?utm-teachers=#{package.name}")

    find('h1.jamclass-h1', 'Let Us Find You The Perfect Music Teacher')
    find('h2.jamclass-h2', 'And Connect You Online With Our Patented, Unique Technology')
    find('p', text: 'Like the TestDrive concept, but 4 teachers is too many for you?')

    find('button.cta-button', text: 'TRY TESTDRIVE').trigger(:click)

    package.test_drive_package_teachers.each do |package_teacher|
      teacher = package_teacher.user
      find('.teacher-name-packaged', text: teacher.last_name)
    end

    user.reload
    user.is_a_student.should be true
    user.is_a_teacher.should be false

    user.test_drive_package_choices.count.should eql 1
    choice = user.test_drive_package_choices[0]
    choice.test_drive_package_choice_teachers.count.should eql 4

    fill_out_payment


    find('#banner h1', text: 'TestDrive Purchased')
    find('#banner .dialog-inner', text: 'Each teacher has received your request and should respond shortly')
    find('#banner .close-btn').trigger(:click)
  end


  it "4-count package, user picks 2" do

    package = FactoryGirl.create(:test_drive_package, :four_pack)
    visit "/landing/jamclass/students?utm-teachers=#{package.name}"

    find('h1.jamclass-h1', 'Let Us Find You The Perfect Music Teacher')
    find('h2.jamclass-h2', 'And Connect You Online With Our Patented, Unique Technology')

    teacher1 = package.test_drive_package_teachers[0].user
    teacher2 = package.test_drive_package_teachers[1].user
    teacher3 = package.test_drive_package_teachers[2].user
    teacher4 = package.test_drive_package_teachers[3].user
    find('p', text: 'Like the TestDrive concept, but 4 teachers is too many for you?')
    find('a.pick-two').trigger(:click)
    find('input[data-teacher-id="' + teacher1.id + '"]').trigger(:click)
    find('input[data-teacher-id="' + teacher2.id + '"]').trigger(:click)
    find('a.select-teachers').trigger(:click)

    fill_in "email", with: 'student_package2@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('.teacher-name-packaged', text: teacher1.last_name)
    find('.teacher-name-packaged', text: teacher2.last_name)

    page.should_not have_selector('.teacher-name-packaged', text: teacher3.last_name)
    page.should_not have_selector('.teacher-name-packaged', text: teacher4.last_name)

    find('.explanation', text: '2 private online music lessons - 1 each from these 2 instructors')

    fill_out_payment(nil, "Super HahaGuy")

    find('#banner h1', text: 'TestDrive Purchased')
    find('#banner .dialog-inner', text: 'Each teacher has received your request and should respond shortly')
    find('#banner .close-btn').trigger(:click)


    user = User.find_by_email('student_package2@jamkazam.com')
    user.test_drive_package_choices.count.should eql 1
    choice = user.test_drive_package_choices[0]
    choice.test_drive_package_choice_teachers.count.should eql 2


    purchase = user.most_recent_test_drive_purchase
    purchase.lesson_package_type.is_test_drive?.should be true
    purchase.lesson_package_type.test_drive_count.should eql 2

    user.student_lesson_bookings.count.should eql 2
    foundTeacher1 = false
    foundTeacher2 = false
    user.student_lesson_bookings.each do |booking|
      if booking.teacher == teacher1
        foundTeacher1 = true
      end
      if booking.teacher == teacher2
        foundTeacher2 = true
      end
    end
    foundTeacher1.should be true
    foundTeacher2.should be true
  end

  it "2-count package, user picks 2" do

    package = FactoryGirl.create(:test_drive_package, :two_pack)
    visit "/landing/jamclass/students?utm-teachers=#{package.name}"

    find('h1.jamclass-h1', 'Let Us Find You The Perfect Music Teacher')
    find('h2.jamclass-h2', 'And Connect You Online With Our Patented, Unique Technology')

    teacher1 = package.test_drive_package_teachers[0].user
    teacher2 = package.test_drive_package_teachers[1].user
    find('p.price-explain', text: 'Take full 30-minute lessons from each of these 2 amazing teachers for just $14.99 each')

    fill_in "email", with: 'student_package3@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('.teacher-name-packaged', text: teacher1.last_name)
    find('.teacher-name-packaged', text: teacher2.last_name)

    find('.explanation', text: '2 private online music lessons - 1 each from these 2 instructors')

    fill_out_payment(nil, "Super HahaGuy")

    find('#banner h1', text: 'TestDrive Purchased')
    find('#banner .dialog-inner', text: 'Each teacher has received your request and should respond shortly')
    find('#banner .close-btn').trigger(:click)


    user = User.find_by_email('student_package3@jamkazam.com')
    user.test_drive_package_choices.count.should eql 1
    choice = user.test_drive_package_choices[0]
    choice.test_drive_package_choice_teachers.count.should eql 2


    purchase = user.most_recent_test_drive_purchase
    purchase.lesson_package_type.is_test_drive?.should be true
    purchase.lesson_package_type.test_drive_count.should eql 2

    user.student_lesson_bookings.count.should eql 2
    foundTeacher1 = false
    foundTeacher2 = false
    user.student_lesson_bookings.each do |booking|
      if booking.teacher == teacher1
        foundTeacher1 = true
      end
      if booking.teacher == teacher2
        foundTeacher2 = true
      end
    end
    foundTeacher1.should be true
    foundTeacher2.should be true
  end

  it "2-count package, user picks 1" do

    package = FactoryGirl.create(:test_drive_package, :two_pack)
    visit "/landing/jamclass/students?utm-teachers=#{package.name}"

    find('h1.jamclass-h1', 'Let Us Find You The Perfect Music Teacher')
    find('h2.jamclass-h2', 'And Connect You Online With Our Patented, Unique Technology')

    teacher1 = package.test_drive_package_teachers[0].user
    teacher2 = package.test_drive_package_teachers[1].user
    find('a.pick-one').trigger(:click)
    find('input[data-teacher-id="' + teacher2.id + '"]').trigger(:click)
    find('a.select-teachers').trigger(:click)

    fill_in "email", with: 'student_package4@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('.teacher-name-packaged', text: teacher2.last_name)

    page.should_not have_selector('.teacher-name-packaged', text: teacher1.last_name)

    find('.explanation', text: 'This purchase entities you to take a private online music lesson from this instructor')

    fill_out_payment(nil, "Super HahaGuy")

    find('#banner h1', text: 'TestDrive Purchased')
    find('#banner .dialog-inner', text: 'You have purchased 1 TestDrive credit and have used it to request a JamClass with ' + teacher2.name)
    find('#banner .close-btn').trigger(:click)


    user = User.find_by_email('student_package4@jamkazam.com')
    user.test_drive_package_choices.count.should eql 1
    choice = user.test_drive_package_choices[0]
    choice.test_drive_package_choice_teachers.count.should eql 1


    purchase = user.most_recent_test_drive_purchase
    purchase.lesson_package_type.is_test_drive?.should be true
    purchase.lesson_package_type.test_drive_count.should eql 1

    user.student_lesson_bookings.count.should eql 1
    foundTeacher2 = false
    user.student_lesson_bookings.each do |booking|
      if booking.teacher == teacher2
        foundTeacher2 = true
      end
    end
    foundTeacher2.should be true
  end

  it "1-count package" do

    package = FactoryGirl.create(:test_drive_package, :one_pack)
    visit "/landing/jamclass/students?utm-teachers=#{package.name}"

    find('h1.jamclass-h1', 'Let Us Find You The Perfect Music Teacher')
    find('h2.jamclass-h2', 'And Connect You Online With Our Patented, Unique Technology')

    teacher1 = package.test_drive_package_teachers[0].user

    fill_in "email", with: 'student_package5@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('.teacher-name-packaged', text: teacher1.last_name)

    find('.explanation', text: 'This purchase entities you to take a private online music lesson from this instructor')

    fill_out_payment(nil, "Super HahaGuy")

    find('#banner h1', text: 'TestDrive Purchased')
    find('#banner .dialog-inner', text: 'You have purchased 1 TestDrive credit and have used it to request a JamClass with ' + teacher1.name)
    find('#banner .close-btn').trigger(:click)


    user = User.find_by_email('student_package5@jamkazam.com')
    user.test_drive_package_choices.count.should eql 1
    choice = user.test_drive_package_choices[0]
    choice.test_drive_package_choice_teachers.count.should eql 1

    purchase = user.most_recent_test_drive_purchase
    purchase.lesson_package_type.is_test_drive?.should be true
    purchase.lesson_package_type.test_drive_count.should eql 1

    user.student_lesson_bookings.count.should eql 1
    foundTeacher1 = false
    user.student_lesson_bookings.each do |booking|
      if booking.teacher == teacher1
        foundTeacher1 = true
      end
    end
    foundTeacher1.should be true
  end


  it "package 4 count" do
    package = FactoryGirl.create(:test_drive_package, :four_pack)
    visit "/landing/jamclass/students?utm-teachers=#{package.name}"

    find('h1.jamclass-h1', 'Let Us Find You The Perfect Music Teacher')
    find('h2.jamclass-h2', 'And Connect You Online With Our Patented, Unique Technology')

    find('p', text: 'Like the TestDrive concept, but 4 teachers is too many for you?')

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

    package.test_drive_package_teachers.each do |package_teacher|
      teacher = package_teacher.user
      find('.teacher-name-packaged', text: teacher.last_name)
    end


    user = User.find_by_email('student_package1@jamkazam.com')
    user.is_a_student.should be true
    user.is_a_teacher.should be false
    user.musician.should be true
    user.origin_utm_source.should eql "organic"
    user.origin_utm_campaign.should eql "127.0.0.1"
    user.origin_utm_medium.should eql "organic"
    user.origin_referrer.should_not be_nil

    user.test_drive_package_choices.count.should eql 1
    choice = user.test_drive_package_choices[0]
    choice.test_drive_package_choice_teachers.count.should eql 4

    fill_out_payment(nil, "Super HahaGuy")

    find('#banner h1', text: 'TestDrive Purchased')
    find('#banner .dialog-inner', text: 'Each teacher has received your request and should respond shortly')
    find('#banner .close-btn').trigger(:click)

    user.reload
    user.first_name.should eql 'Super'
    # find each of teh 4 lessons requested
    user.student_lesson_bookings.each do |booking|
      lesson = booking.lesson_sessions[0]
      lesson.status.should eql LessonSession::STATUS_REQUESTED
      booking.card_presumed_ok.should be true
      find('tr[data-lesson-session-id="' + lesson.id + '"] td.startTimeColumn', text: 'No time has been scheduled yet')
    end

    booking = user.student_lesson_bookings[0]
    lesson = booking.lesson_sessions[0]
    teacher = lesson.teacher

    jamclass_hover_option(lesson, 'status', 'View Status')

    find('h2', text: 'your lesson has been requested')

    # verify that the lesson stats screen looks right
    find('a.button-orange.schedule', text: 'PROPOSE TIME')
    find('label', text: 'Propose a day/time')
    find('.generic-time-stmt.no-slot', text: 'Your lesson has no scheduled time yet.')

    switch_user(teacher, '/client#/jamclass')
    jamclass_hover_option(lesson, 'status', 'View Status')
    find('.description', text: 'Message from ' + user.first_name)
    counter_day(lesson)

    switch_user(user, '/client#/jamclass')
    jamclass_hover_option(lesson, 'status', 'View Status')
    find('.counter-stmt', text: 'Proposed alternate day/time is')
    approve_lesson(lesson)

    validate_status(lesson, 'Scheduled')

    jamclass_hover_option(lesson, 'status', 'View Status')

    find('.generic-time-stmt', text: 'will take place this')
  end
end
