# BACKGROUND -----

Given(/^I have some new blog data to enter$/) do
  @context.create_blog_category_data!
  @context.create_blog_settings_data!
end

# GENERAL -----

Given(/^I am viewing the blog admin area$/) do
  @context.visit_page("/admin/blog_posts")
end

Given(/^I am on the blog post's details page$/) do
  @context.visit_blog_details_page
end

Given(/^I am on the blog (home|resources|category1) page$/) do |page_name|
  @context.visit_blog_page(page_name)
end

# CONTENT UPGRADES & NEWSLETTER -----

When(/^I click the newsletter signup button$/) do
  find('div.blog-updates.blog-signup-modal-button ', text: "Get free resources").click
end

When(/^I fill in (?:a|an) (valid|invalid) email for the newsletter$/) do |validity|
  @context.fill_in_email(".blog-signup-modal .email-input", validity == "valid")
  @context.click_a_tag("Subscribe")
end

When(/^I fill in the (1st|second) content download hero with (?:a|an) (valid|invalid) email$/) do |content_download_ordinance, validity|
  content_download_index = content_download_ordinance[0].to_i
  @context.sign_up_for_content_download(content_download_index, validity == "valid")
end

Given(/^I click on the (1st|2nd) content download$/) do |content_download_ordinance|
  content_download_index = content_download_ordinance[0].to_i
  all("a.large-preview-card")[content_download_index].click
end

Then(/^I should be on the (1st|2nd) content download's landing page$/) do |content_download_ordinance|
  content_download_index = content_download_ordinance[0].to_i
  @context.expect_content_download_landing_page(content_download_index)
end

Then(/^I should be on the (1st|2nd|3rd) content download's thank you page$/) do |ordinal|
  @context.expect_content_download_thank_you_page(ordinal.to_i)
end

# BLOG FORM -----

When(/^I save the blog form$/) do
  @context.click_a_tag("Save")
end

When(/^I fill in (?:a|the) (blog settings|category) (?:tab|tabs) with (valid|invalid) settings$/) do |destination, validity|
  if destination == "blog settings"
    @context.fill_in_blog_settings(validity.to_sym)
  elsif destination == "category"
    @context.fill_in_category_settings(validity.to_sym)
  end
end

Then(/^I should see the appropriate (blog settings|category) error messages$/) do |destination|
  if destination == "blog settings"
    @context.expect_invalid_blog_settings_content
  elsif destination == "category"
    @context.expect_invalid_category_content
  end
end

Then(/^the public blog (settings|category) (?:page|pages) should have the correct content$/) do |destination|
  if destination == "blog settings"
    @context.expect_valid_blog_settings_content
  elsif destination == "category"
    @context.expect_valid_category_content
  end
end

When(/^I fill in the new post modal with a title and title text$/) do
  @context.fill_in_new_blog_post_form
  within('.admin-modal') { find('a', text: "Add Blog Post").click }
  @context.wait_for_ajax
end

Then(/^I should see the (top hero|bottom hero|newsletter|) email error formatting$/) do |email_error_type|
  @context.expect_email_input_error(email_error_type)
end

# BLOG POSTS -----

When(/^I add a new blog post$/) do
  @context.click_a_tag("Add Blog Post")
end

Then(/^the new blog post should exist$/) do
  @context.expect_blog_post_existence
end

Then(/^I should be redirected to the blog post details page$/) do
  @context.expect_content_on_page("Blog Post Details")
end

When(/^I update the post's content$/) do
  @context.update_blog_post_content!
end

Then(/^the blog post's (public|category) page should reflect the changes$/) do |destination|
  if destination == "public"
    @context.expect_public_blog_post_updates
  elsif destination == "category"
    @context.expect_public_category_page_updates
  end
end

When(/^I select the first category checkbox$/) do
  @context.select_first_category
end

When(/^I save the blog post$/) do
  @context.click_a_tag("Save")
end

When(/^I click the delete button$/) do
  @context.click_a_tag("Delete")
end

When(/^I choose to permanently delete the post$/) do
  @context.click_a_tag("Yes")
  @context.wait_for_ajax
end

Then(/^the post should not exist$/) do
  @context.expect_deleted_post
end

# AUTOMATIC TASKS

Given(/^a scheduled blog post exists$/) do
  @context.given_scheduled_blog_post
end
