Given(/^a webinar named (.+)$/) do |name|
  @context.given_webinar(name)
end

Given(/^a user visits the (.+) registration page$/) do |name|
  @context.visit_page("/webinars/#{name}/register")
end

Given(/^a user visits the (.+) registration page with an email param$/) do |name|
  @context.email = "someone#{DateTime.now.to_i}@example.com"
  @context.visit_page("/webinars/#{name}/register?email=#{@context.email}")
end

Given(/^a user visits the (.+) thank you page$/) do |name|
  @context.visit_page("/webinars/#{name}/thank-you")
end

Given(/^a user visits the (.+) play page$/) do |name|
  @context.visit_page("/webinars/#{name}/play")
end

Given(/^a user visits the (.+) replay page$/) do |name|
  @context.visit_page("/webinars/#{name}/replay")
end

When('a user registers for the webinar') do
  @context.email = "someone#{DateTime.now.to_i}@example.com"
  find('div.orange-button', text: 'Claim your spot').click
  expect(find('div.webinar-modal-container').visible?).to be true
  find('input[placeholder="Email address"]').set(@context.email)
  find('a.orange-button').click
end

When('they register for the webinar a second time') do
  find('div.orange-button', text: 'Claim your spot').click
  expect(find('div.webinar-modal-container').visible?).to be true
  find('input[placeholder="Email address"]').set(@context.email)
  find('a.orange-button').click
end

When('a user submits the pre-opened registration modal') do
  find('.webinar-registration-modal .register-button', text: 'Claim your spot').click
end

When(/enters? the webinar/) do
  input_elements = page.all(:css, '.email-gate input')
  name_box = input_elements[0]
  email_box = input_elements[1]
  submit_button = input_elements[2]
  if name_box
    name_box.set('Peter')
    email_box.set('peter@teachable.com')
    submit_button.click
  end
end

Then('they should see the register page') do
  @context.expect_content_on_page('Claim your spot')
end

Then('they should see the thank you page') do
  @context.expect_content_on_page('You have successfully signed up for:')
end

Then('they should see the play page') do
  @context.expect_content_on_page('Time left until the webinar starts...')
end

Then('they should see the replay page') do
  @context.expect_content_on_page('Welcome to the replay:')
end

Then('the registration should be in the database') do
  expect(Registrant.last.attributes).to include('email' => @context.email)
  expect(Registration.count).to be(1)
end

Then('they should be on the checkout page') do
  @context.switch_to_recently_opened_tab
  expect(page).to have_current_path("/create-account", ignore_query: true)
end

Then('the checkout page should have the correct upgrade_src parameter') do
  expect(page).to have_current_path(/upgrade_src=test-suite-play/)
end
