require 'spec_helper'

describe "JamClassScreen", :js => true, :type => :feature, :capybara_feature => true do

  subject { page }

  let(:user) { FactoryGirl.create(:user, traditional_band: true,paid_sessions: true, paid_sessions_hourly_rate: 1,  paid_sessions_daily_rate:1 ) }
  let(:teacher_user) {FactoryGirl.create(:teacher_user, ready_for_session_at: Time.now, first_name: "TeacherUser1")}
  let(:teacher_user2) {FactoryGirl.create(:teacher_user, ready_for_session_at: Time.now, first_name: "TeacherUser2")}

  it "shows Missed (Both)" do
    lesson = testdrive_lesson(user, teacher_user, {miss: true, accept: true})
    lesson.analysis_json["teacher_analysis"]["missed"].should be true
    lesson.analysis_json["student_analysis"]["missed"].should be true

    fast_signin(user, "/client#/jamclass")

    find('#jam-class-student-screen td.displayStatusColumn', text: 'Missed (Both)')
  end

  it "shows Missed (Student)" do
    lesson = testdrive_lesson(user, teacher_user, {accept: true, finish: true})
    lesson.analysis_json["teacher_analysis"]["missed"].should be false
    lesson.analysis_json["student_analysis"]["missed"].should be true

    fast_signin(user, "/client#/jamclass")

    find('#jam-class-student-screen td.displayStatusColumn', text: 'Completed')
  end

  it "shows Missed (Teacher)" do
    lesson = testdrive_lesson(user, teacher_user, {accept: true, teacher_miss: true})
    lesson.analysis_json["teacher_analysis"]["missed"].should be true
    lesson.analysis_json["student_analysis"]["missed"].should be false

    fast_signin(user, "/client#/jamclass")

    find('#jam-class-student-screen td.displayStatusColumn', text: 'Missed (Teacher)')
    # also check that clicking messages icon shows chat dialog (instead of view status screen) # VRFS-4089
    find('tr[data-lesson-session-id="' + lesson.id + '"] td.unreadColumn a').trigger(:click)
    find('.chat-list-scroller')
  end

  it "shows Completed" do
    lesson = testdrive_lesson(user, teacher_user, {accept: true, success: true})
    lesson.analysis_json["teacher_analysis"]["missed"].should be false
    lesson.analysis_json["student_analysis"]["missed"].should be false

    fast_signin(user, "/client#/jamclass")

    find('#jam-class-student-screen td.displayStatusColumn', text: 'Completed')
  end

  it "shows Decline for Teacher, Cancel for Student" do

    lesson = testdrive_lesson(user, teacher_user, {accept: false})

    fast_signin(teacher_user, "/client#/jamclass")
    find('#jam-class-student-screen td.displayStatusColumn', text: 'Requested')


    # open up hover
    find('tr[data-lesson-session-id="' + lesson.id + '"] .lesson-session-actions-btn').trigger(:click)
    find('li[data-lesson-option="cancel"] a', visible: false, text: 'Decline Request').trigger(:click)
    # confirm cancelation -
    find('#banner h1', text: 'Confirm Decline')
    find('#banner .dialog-inner', text:"Are you sure you want to decline this lesson?" )
    # don't cancel yet
    find('#banner .no-btn', text: 'CANCEL').trigger(:click)

    lesson.teacher_unread_messages.should be true
    # also check that clicking messages icon shows view status screen (instead of chat dialog) # VRFS-4089
    find('tr[data-lesson-session-id="' + lesson.id + '"] td.unreadColumn a').trigger(:click)
    find('h2', text: 'respond to lesson request')

    switch_user(user, "/client#/jamclass")

    lesson.reload
    lesson.student_unread_messages.should be false

    find('#jam-class-student-screen td.displayStatusColumn', text: 'Requested')

    # open up hover
    find('tr[data-lesson-session-id="' + lesson.id + '"] .lesson-session-actions-btn').trigger(:click)
    # should work, doesn't
    find('li[data-lesson-option="cancel"] a', visible: false, text: 'Cancel Request').trigger(:click)

    # confirm cancelation -
    find('#banner h1', text: 'Confirm Cancelation')
    find('#banner .dialog-inner', text:"Are you sure you want to cancel this lesson?" )

    find('#banner .yes-btn', text: 'YES').trigger(:click)

    find('#jam-class-student-screen td.displayStatusColumn', text: 'Canceled (Student)')

    switch_user(teacher_user, "/client#/jamclass")

    find('#jam-class-student-screen td.displayStatusColumn', text: 'Canceled (Student)')
  end

  it "Teacher cancelation" do

    lesson = testdrive_lesson(user, teacher_user, {accept: false})

    fast_signin(teacher_user, "/client#/jamclass")
    find('#jam-class-student-screen td.displayStatusColumn', text: 'Requested')


    # open up hover
    find('tr[data-lesson-session-id="' + lesson.id + '"] .lesson-session-actions-btn').trigger(:click)
    find('li[data-lesson-option="cancel"] a', visible: false, text: 'Decline Request').trigger(:click)
    # confirm cancelation -
    find('#banner .yes-btn', text: 'YES').trigger(:click)
    find('#jam-class-student-screen td.displayStatusColumn', text: 'Canceled (Teacher)')

    switch_user(user, "/client#/jamclass")

    find('#jam-class-student-screen td.displayStatusColumn', text: 'Canceled (Teacher)')

  end

  it "student cancels one of recurring" do
    lesson = monthly_lesson(user, teacher_user, {accept: true})
    lesson1 = lesson.lesson_booking.lesson_sessions[0]
    lesson2 = lesson.lesson_booking.lesson_sessions[1]

    fast_signin(user, "/client#/jamclass")
    find('tr[data-lesson-session-id="' + lesson1.id + '"] td.displayStatusColumn', text: 'Scheduled')
    find('tr[data-lesson-session-id="' + lesson2.id + '"] td.displayStatusColumn', text: 'Scheduled')

    # open up hover
    find('tr[data-lesson-session-id="' + lesson1.id + '"] .lesson-session-actions-btn').trigger(:click)
    find('li[data-lesson-option="cancel"] a', visible: false, text: 'Cancel Lesson').trigger(:click)
    # confirm cancelation -
    find('#banner a', text: 'CANCEL THIS LESSON').trigger(:click)
    find('tr[data-lesson-session-id="' + lesson1.id + '"] td.displayStatusColumn', text: 'Canceled (Student)')
    find('tr[data-lesson-session-id="' + lesson2.id + '"] td.displayStatusColumn', text: 'Scheduled')

    switch_user(teacher_user, "/client#/jamclass")

    find('tr[data-lesson-session-id="' + lesson1.id + '"] td.displayStatusColumn', text: 'Canceled (Student)')
    find('tr[data-lesson-session-id="' + lesson2.id + '"] td.displayStatusColumn', text: 'Scheduled')
  end

  it "student cancels all recurring" do
    lesson = monthly_lesson(user, teacher_user, {accept: true})
    lesson1 = lesson.lesson_booking.lesson_sessions[0]
    lesson2 = lesson.lesson_booking.lesson_sessions[1]
    lesson1.recurring.should be true
    lesson.lesson_booking.recurring.should be true

    fast_signin(user, "/client#/jamclass")
    find('tr[data-lesson-session-id="' + lesson1.id + '"] td.displayStatusColumn', text: 'Scheduled')
    find('tr[data-lesson-session-id="' + lesson2.id + '"] td.displayStatusColumn', text: 'Scheduled')

    # open up hover
    find('tr[data-lesson-session-id="' + lesson1.id + '"] .lesson-session-actions-btn').trigger(:click)
    find('li[data-lesson-option="cancel"] a', visible: false, text: 'Cancel Lesson').trigger(:click)
    # confirm cancelation -
    find('#banner a', text: 'CANCEL ALL LESSONS').trigger(:click)
    find('tr[data-lesson-session-id="' + lesson1.id + '"] td.displayStatusColumn', text: 'Canceled (Student)')
    lesson1.reload
    lesson2.reload
    lesson1.status.should eql LessonSession::STATUS_CANCELED
    lesson2.status.should eql LessonSession::STATUS_CANCELED

    find('tr[data-lesson-session-id="' + lesson2.id + '"] td.displayStatusColumn', text: 'Canceled (Student)')

    switch_user(teacher_user, "/client#/jamclass")

    find('tr[data-lesson-session-id="' + lesson1.id + '"] td.displayStatusColumn', text: 'Canceled (Student)')
    find('tr[data-lesson-session-id="' + lesson2.id + '"] td.displayStatusColumn', text: 'Canceled (Student)')
  end

  describe "counter" do
    it "test drive" do
      lesson = testdrive_lesson(user, teacher_user, {accept: false})

      fast_signin(teacher_user, "/client#/jamclass")
      validate_status(lesson, 'Requested')

      jamclass_hover_option(lesson, 'reschedule', 'Reschedule Lesson')

      # no popup should show in this case, because it's not yet scheduled

      # we should be at lesson status page
      find('h2', text: 'respond to lesson request')

      screenshot

      counter_day

      # switch to student

      switch_user(user, "/client#/jamclass")
      validate_status(lesson, 'Requested')
      jamclass_hover_option(lesson, 'status', 'View Status')

      find('h2', text: 'this lesson is coming up soon')

      screenshot

      approve_lesson(lesson)

      jamclass_hover_option(lesson, 'reschedule', 'Reschedule Lesson')
      find('#banner h1', text: 'Lesson Change Requested')
      find('#banner .close-btn').trigger(:click)

      counter_day
    end
  end
end
