# Add a declarative step here for populating the DB with movies.

Given /the following movies exist/ do |movies_table|
  movies_table.hashes.each do |movie|
  #debugger
    # each returned element will be a hash whose key is the table header.
    # you should arrange to add that movie to the database here.
    Movie.create!(movie)
  end
  #flunk "Unimplemented"
end

# Make sure that one string (regexp) occurs before or after another one
#   on the same page

Then /I should see "(.*)" before "(.*)"/ do |e1, e2|
  #  ensure that that e1 occurs before e2.
  #  page.body is the entire content of the page as a string.
  #flunk "Unimplemented"
  page.body.should =~ /#{e1}.*#{e2}/m
end

# Make it easier to express checking or unchecking several boxes at once
#  "When I uncheck the following ratings: PG, G, R"
#  "When I check the following ratings: G"

When /I (un)?check the following ratings: (.*)/ do |uncheck, rating_list|
  #debugger
  #null
  # HINT: use String#split to split up the rating_list, then
  #   iterate over the ratings and reuse the "When I check..." or
  #   "When I uncheck..." steps in lines 89-95 of web_steps.rb
  rating_list.split(", ").each do |rating|
   if uncheck != nil
     uncheck("ratings[#{rating}]")
   else
     check("ratings[#{rating}]")
   end
  end  
end

Then /I should see movies with following ratings: (.*)/ do |rating_list|
  rating_list.split(", ").each do |rating|
    page.should have_xpath('//table/tbody/tr/td', :text => /^#{rating}$/)
    #page.should have_xpath('//table/tbody/tr/td', :text => "PG")
    #page.should have_xpath('//table/tbody/tr/td', :text => "R")
  end
end

Then /I should not see movies with following ratings: (.*)/ do |rating_list|
  rating_list.split(", ").each do |rating|
    page.should have_no_xpath('//table/tbody/tr/td', :text => /^#{rating}$/)
     #page.should have_no_xpath('//table/tbody/tr/td', :text => /^PG-13$/)
     #page.should have_no_xpath('//table/tbody/tr/td', :text => /^G$/)
  end
end

Then /I should see all the movies/ do 
  # Make sure that all the movies in the app are visible in the table
  rating_list = ["PG", "G", "PG-13", "R"]
  rating_list.each do |rating|
    page.should have_xpath('//table/tbody/tr/td', :text => /^#{rating}$/)
  end
end
