
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
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 product_orders: [{'product_id' => @product.id, 'quantity' => count.to_i}], user: @other_user, first_order: false
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}
  #ember_order = ember_find('product_order', concerning_product.id)
  sleep 0.1
  ember_order = ember_all('product_order').find{|po| po['product'] == 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 selects the product variant :variant" do |variant|
  # Manual in stead of js_click because capybara does not support the contains statement
  #find ".modal .choose-product-variant-button"
  find ".modal"
  page.evaluate_script %|$('.modal a:contains("#{variant}")').click()|
end

step "the user order :product_name with variant :variant should be in the order list" do |product_name, variant|
  page.should have_content "1 x #{product_name} (#{variant})"
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
  user_visit "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

step "the user has an active list with a/an :order_status order" do |order_status|
  @list = create :list, supplier: @supplier, table: @table, user_ids: [@user.id]
  @product_category ||= create :product_category, supplier: @supplier
  @product ||= create :product, supplier: @supplier, name: 'Beer', price: 2.34, product_category: @product_category
  @order = create :order, order_status.to_sym, supplier: @supplier, list: @list, user: @user
  @product_order = create :product_order, order: @order, product: @product, quantity: 2, price: 2.34
  @user.reload
  @user.active_list_id = @list.id
  @user.save
  case order_status.to_sym
  when :placed then @supplier.increment_orders_placed_count!
  when :active then @supplier.increment_orders_in_process_count!
  end
end

step "the user page contains the product category :product_category_name" do |product_category_name|
  product_category = ProductCategory.find_by_name(product_category_name)
  js_text("#product-category-#{product_category.id} .product_category-title").should include product_category.name
end

step "the user page does not contain the product category :product_category_name" do |product_category_name|
  product_category = ProductCategory.find_by_name(product_category_name)
  page.should_not have_selector "#product-category-#{product_category.id} .product_category-title"
end
