
step "the user clicks on the order :product_name twice" do |product_name|
  @last_product = instance_variable_get product_name.underscore.gsub(/\s/, '_').prepend('@')
  button = find ".order-product-#{@last_product.id} .add-product-to-list"
  2.times { button.click }
end

step "the user clicks on the order product button :product_name" do |product_name|
  @last_product = instance_variable_get product_name.underscore.gsub(/\s/, '_').prepend('@')
  button = find ".order-product-#{@last_product.id} .add-product-to-list"
  button.click
end

step "the user clicks on the user order button" do
  find('.order-selected-products-button').click
  sleep 1
end

step "the user has an active order" do
  step 'there is a section'
  step 'there is a table'
  step 'the user has an active list with an active order'
end

step "the user has a placed order" do
  step 'there is a section'
  step 'there is a table'
  step 'the user has an active list with a placed order'
end

step "there is a signed in user with an active order" do
  step "I am signed in as a user"
  step "the user has an active order"
end
step "there is a signed in user with a placed order" do
  step "I am signed in as a user"
  step "the user has a placed order"
end

step "there is another signed in user on the same list" do
  step 'there is another signed in user user'
  step 'the other user is part of the list'
end

step 'the other user orders a product :count times' do |count|
  @list.place_order products: {@product.id => count.to_i}, user: @other_user
end

step 'the other user is part of the list' do
  @other_user.active_list_id = @list.id
  @list.user_ids = @list.user_ids | [@other_user.id]
  @other_user.save
  @list.save
end

step "the user clicks on the more info button for the product with name :product_name" do |product_name|
  product = @product && @product.name == product_name ? @product : Product.find_by_name(product_name)
  find(".order-product-#{product.id} .show-product-description").click
end

step "the user page should have product information :product_description" do |product_description|
  page.should have_content product_description
end

step "the user order :product_name should be in the order list with price" do |product_name|
  concerning_product = Product.find_by_name(product_name)
  ember_order = ember_store['product_orders'].find{|po| po['product_id'] == concerning_product.id}
  quantity = ember_order['quantity']
  order_price = quantity * concerning_product.price
  within '.product-orders .product-order' do
    page.should have_content product_name
    if quantity > 1
      page.should have_content "#{quantity} x"
    end
    page.should have_content order_price
  end
end

step "the user order total should not be visible for only one order" do
  page.should_not have_selector ".product-orders .total"
end

step "the user order total should be visible with the correct total price" do
  within ".product-orders .total" do
    page.should have_content "8.96" # more dynamic in the future if needed
  end
end

step "the user clicks on the clear order for the last order button" do
  within page.all('.product-orders .product-order').last do
    find('.product-order-remove').click
  end
end

step "the user should see an empty active order panel" do
  no_orders_message = page.evaluate_script(%|t('product_orders.no_orders')|)
  page.find('.product-orders').text.should == no_orders_message
end

step 'the user is on the order products page' do
  visit "/user#/tables/#{@table.id}"
end

step 'the user should see the products listed for the active list' do
  page.should have_content @heineken_beer.name
  page.should have_content @apple_pie.name
end

step "the user has an older list for the same supplier" do
  expect([@supplier, @table, @user]).to be_all
  @older_list = create :list, supplier: @supplier, table: @table, user_ids: [@user.id]

end
