step "I provide a new supplier email address" do
  js_set_field '.supplier-email', 'new-supplier-mail@mozo.bar'
end

step "the supplier submits the supplier settings form" do
  js_click '.submit-supplier-settings'
  #sleep 1.5 # give application some time
end

step "the supplier email should not have been changed" do
  @supplier.reload
  @supplier.email.should == 'supplier@mozo.bar'
end

step "the supplier unconfirmed email should have been set to the new supplier email" do
  @supplier.reload
  @supplier.unconfirmed_email.should == 'new-supplier-mail@mozo.bar'
end

step "an email should have been sent to the original supplier email with email confirmation instructions" do
  mail = ActionMailer::Base.deliveries.last
  mail.to.should == ['supplier@mozo.bar']
  mail.body.should include I18n.t('devise.mailer.confirmation_instructions.supplier.salutation', email: @supplier.email)
  mail.body.should_not include 'translation' # indicating a translation issue
  confirmation_link_match = mail.body.match(/http:\/\/www.mozo.bar(\/suppliers\/confirmation[^"]+)"/)
  confirmation_link_match.should be_present
  @confirmation_link = confirmation_link_match[1]
end

step "the supplier changes the name to :name in the settings page" do |new_name|
  js_set_field 'input.supplier-name', new_name
end

step "the supplier clicks on the new email confirmation link" do
  visit @confirmation_link
end

step "the supplier email is the new email" do
  @supplier.reload
  @supplier.email.should == 'new-supplier-mail@mozo.bar'
end

step "the supplier selects :time_zone as Time Zone" do |visual_time_zone|
  page.select visual_time_zone, from: 'supplier_time_zone'
end

step "the supplier timezone should be :time_zone" do |time_zone|
  sleep 1
  @supplier.reload
  @supplier.time_zone.should == time_zone
end
