module StripeMock
  class ErrorQueue
    def clear
      @queue = []
    end
  end

  def self.clear_errors
    instance.error_queue.clear if instance
    client.error_queue.clear if client
  end
end

def book_lesson(user, teacher, options)

  if options[:package_count].nil?
    options[:package_count] = 4
  end

  if options[:slots].nil?
    slots = []
    if options[:monthly]
      slots << FactoryGirl.build(:lesson_booking_slot_recurring)
      slots << FactoryGirl.build(:lesson_booking_slot_recurring)
    else
      slots << FactoryGirl.build(:lesson_booking_slot_single)
      slots << FactoryGirl.build(:lesson_booking_slot_single)
    end

  else
    slots = options[:slots]
  end

  if user.stored_credit_card == false
    token = create_stripe_token
    result = user.payment_update({token: token, zip: '78759', normal: true})
    #user.stored_credit_card = true
    #user.save!
  end

  if options[:test_drive]
    booking = LessonBooking.book_test_drive(user, teacher, slots, "Hey I've heard of you before.")
  elsif options[:normal]
    booking = LessonBooking.book_normal(user, teacher, slots, "Hey I've heard of you before.", false, LessonBooking::PAYMENT_STYLE_SINGLE, 60)
  elsif options[:monthly]
    booking = LessonBooking.book_normal(user, teacher, slots, "Hey I've heard of you before.", true, LessonBooking::PAYMENT_STYLE_MONTHLY, 60)
  end
  if booking.errors.any?
    puts "BOOKING #{booking.errors.inspect}"

    if booking.lesson_booking_slots[0].errors.any?
      puts "SLOT0 #{booking.lesson_booking_slots[0].errors.inspect}"
    end
  end

  booking.errors.any?.should be_false
  lesson = booking.lesson_sessions[0]
  start = lesson.scheduled_start
  end_time = lesson.scheduled_start + (60 * lesson.duration)

  booking.card_presumed_ok.should be_true

  if options[:test_drive]
    if user.most_recent_test_drive_purchase.nil?
      LessonPackagePurchase.create(user, booking, LessonPackageType.package_for_test_drive_count(options[:package_count]))
    end
  elsif options[:monthly]
    LessonPackagePurchase.create(user, booking, LessonPackageType.single, Date.today.year, Date.today.month)
  end


  if options[:counter]
    if options[:monthly]
      counter_slot = FactoryGirl.build(:lesson_booking_slot_recurring)
    else
      counter_slot = FactoryGirl.build(:lesson_booking_slot_single)
    end

    lesson.counter({proposer: options[:counterer] || user, message: "countered YEAH!", slot: counter_slot})
    lesson.reload
    lesson.status.should eql LessonSession::STATUS_COUNTERED
  end

  if options[:accept]
    lesson.accept({message: 'Yeah I got this', slot: slots[0].id, accepter: teacher})
    lesson.errors.any?.should be_false
    lesson.reload
    lesson.slot.should eql slots[0]
    lesson.status.should eql LessonSession::STATUS_APPROVED
  end

  if options[:cancel]
    lesson.cancel({canceler: options[:canceler] || user, message: "sorry about that"})
    lesson.reload
    lesson.status.should eql LessonSession::STATUS_CANCELED
  end

  if options[:miss]
    # teacher & student get into session

    Timecop.travel(end_time + 1)
    lesson.analyse
    lesson.session_completed
  elsif options[:teacher_miss]
    uh2 = FactoryGirl.create(:music_session_user_history, user: user, history: lesson.music_session, created_at: start, session_removed_at: end_time)
    # artificially end the session, which is covered by other background jobs
    lesson.music_session.session_removed_at = end_time
    lesson.music_session.save!
    Timecop.travel(end_time + 1)
    lesson.analyse
    lesson.session_completed
  elsif options[:success]
    uh1 = FactoryGirl.create(:music_session_user_history, user: user, history: lesson.music_session, created_at: start, session_removed_at: end_time)
    uh2 = FactoryGirl.create(:music_session_user_history, user: teacher, history: lesson.music_session, created_at: start, session_removed_at: end_time)
    # artificially end the session, which is covered by other background jobs
    lesson.music_session.session_removed_at = end_time
    lesson.music_session.save!
    Timecop.travel(end_time + 1)

    unless options[:no_after_logic]
      lesson.analyse
      lesson.session_completed
    end

  elsif options[:finish]
    # teacher & student get into session
    uh2 = FactoryGirl.create(:music_session_user_history, user: teacher, history: lesson.music_session, created_at: start, session_removed_at: end_time)
    if options[:student_show]
      uh2 = FactoryGirl.create(:music_session_user_history, user: user, history: lesson.music_session, created_at: start, session_removed_at: end_time)
    end
    # artificially end the session, which is covered by other background jobs
    lesson.music_session.session_removed_at = end_time
    lesson.music_session.save!

    Timecop.travel(end_time + 1)

    unless options[:no_after_logic]
      lesson.analyse
      lesson.session_completed
    end

    if options[:monthly]
      LessonBooking.hourly_check
    end
  end

  lesson
end

def testdrive_lesson(user, teacher, options = {finish: false, accept: true, cancel: false, miss: false, slots: nil})
  options[:test_drive] = true
  book_lesson(user, teacher, options)
end


def normal_lesson(user, teacher, options = {finish: false, accept: true, cancel: false, miss: false, slots: nil})
  options[:normal] = true
  book_lesson(user, teacher, options)
end


def monthly_lesson(user, teacher, options = {finish: false, accept: true, cancel: false, miss: false, slots: nil})
  options[:monthly] = true
  book_lesson(user, teacher, options)
end