step "there is an active list and order" do
  @user ||= create :user
  step 'there is a confirmed and open supplier'
  @table = create :table, supplier: @supplier, section: @section, position_x: 2, position_y: 2
  @section.should be_present
  @list = create :list, state: 'active', supplier: @supplier, table: @table, section: @section, user_ids: [@user.id]
  @product = create :product, price: 2.22, supplier: @supplier
  @order = create :order, user: @user, list: @list, supplier: @supplier, section: @section
  @product_order = create :product_order, order: @order, product: @product, quantity: 3, price: 2.11
  @list.set_price.should == 6.33 # does not belong here, but good test. must take product order price above product price
end

step "the supplier dashboard should display the active list" do
  el = find(".list-row-#{@list.id}")
  el['class'].should_not =~ /active/
end

step "the list in the supplier dashboard should not be displayed anymore" do
  page.should_not have_selector(".list-row-#{@list.id}")
end

step "the supplier dashboard should display the active order" do
  el = find(".order-row-#{@order.id}")
  el['class'].should_not =~ /active/
end
step "the supplier order row should be marked as active" do
  el = find(".order-row-#{@order.id}")
  el['class'].should =~ /active/
end

step "the order in the supplier dashboard should not be displayed anymore" do
  page.should_not have_selector(".order-row-#{@order.id}")
end

step "the list on the supplier dashboard should not be marked as in need of help" do
  page.should_not have_selector(".list-row-#{@list.id} .icon.needs-help")
end

step "the list on the supplier dashboard should be marked as in need of help" do
  page.should have_selector(".list-row-#{@list.id} .icon.needs-help")
end

step "the list on the supplier dashboard should not be marked as in need of payment" do
  page.should_not have_selector(".list-row-#{@list.id} .icon.needs-payment")
end

step "the list on the supplier dashboard should be marked as in need of payment" do
  page.should have_selector(".list-row-#{@list.id} .icon.needs-payment")
end

step "the supplier dashboard list should display the updated price" do
  el = find(".list-row-#{@list.id} .total_list_amount")
  # original order is 3 * 2.11 =  6.33
  # new order price = 5 * 2.22 = 11.10
  # therefore the updated price should be 17.43
  el.text.should =~ /17.43/
end

step "the new order should be present in the supplier dashboard" do
  el = find(".order-row-#{@new_order.id}")
  el.find('.table_number').text.should == @table.number.to_s
  el.find('.section_title').text.should == @section.title
end

step "the new order on a table in another section should be present in the supplier dashboard" do
  el = find(".order-row-#{@new_order.id}")
  el.find('.table_number').text.should == @new_table.number.to_s
  el.find('.section_title').text.should == @new_section.title
end

step "the new list should appear in the supplier dashboard" do
  el = find(".list-row-#{@new_list.id}")
  el.find('.total_list_amount').text.should =~ /6.66/
  el.find('.section_title').text.should == 'Terrace'
  el.find('.table_number').text.should == @new_table.number.to_s
end

step "I click on the close list button in the supplier dashboard" do
  find(".list-row-#{@list.id} .close_list").click
end

step "I click on the mark list as helped button in the supplier dashboard" do
  find(".list-row-#{@list.id} .mark_list_as_helped").click
end

step "I should see the list and the new list" do
  page.should have_selector ".list-row-#{@list.id}"
  page.should have_selector ".list-row-#{@new_list.id}"
end

step "I should see the order and the new order" do
  page.should have_selector ".order-row-#{@order.id}"
  page.should have_selector ".order-row-#{@new_order.id}"
end

step "I should see the list and order but not the new list and new order" do
  page.should have_selector ".list-row-#{@list.id}"
  page.should_not have_selector ".list-row-#{@new_list.id}"
  page.should have_selector ".order-row-#{@order.id}"
  page.should_not have_selector ".order-row-#{@new_order.id}"
end

step "I should see the new list and new order but not the list and order" do
  page.should_not have_selector ".list-row-#{@list.id}"
  page.should have_selector ".list-row-#{@new_list.id}"
  page.should_not have_selector ".order-row-#{@order.id}"
  page.should have_selector ".order-row-#{@new_order.id}"
end

step "I select the section in the supplier dashboard" do
  find(%|.section_selector option[value="#{@section.id}"]|).select_option
end

step "I select the new section in the supplier dashboard" do
  find(%|.section_selector option[value="#{@new_section.id}"]|).select_option
end

step "I reset the section selector in the supplier dashboard" do
  find(%|.section_selector option[value=""]|).select_option
end

step "I should not see the supplier main board section jumper" do
  page.should_not have_selector '.main-board-section-jumper'
end

step "I click on the section main board section jumper" do
  find('.main-board-section-jumper').click
end

step "the active list changes to another table in another section" do
  @other_section = create :section, supplier: @supplier, title: 'New Section'
  @other_table = create :table, section: @other_section, supplier: @supplier, number: 65
  @list.move_to_table! @other_table
end

step "the supplier main board table number should be updated to the new table number" do
  expect( find(".list-row-#{@list.id} .table_number").text ).to eq "65"
  expect( find(".order-row-#{@list.orders.first.id} .table_number").text ).to eq "65"
end

step "the supplier main board section name should be updated to the new section" do
  expect( find(".list-row-#{@list.id} .section_title").text ).to eq "New Section"
  expect( find(".order-row-#{@list.orders.first.id} .section_title").text ).to eq "New Section"
end
