step "there is an active list and order" do
  step 'there is a confirmed and open supplier'
  @user = create :user
  @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, state: 'placed'
  @product_order = create :product_order, order: @order, product: @product, quantity: 3, price: 2.11, product_name: 'Old Product name'
  # @list.set_price.should == 6.33 # does not belong here, but good test. must take product order price above product price
  Qwaiter::Counter.set "supplier_counter:#{@supplier.id}:orders_placed", 11
  Qwaiter::Counter.set "supplier_counter:#{@supplier.id}:orders_in_process", 7
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
  sleep 0.5
  el = find(".list-row-#{@list.id} .currency")
  # 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}"
  el.find('.link-to-section').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 == "# #{@other_table.number}"
  el.find('.link-to-section').text.should == @other_section.title
end

step "the new list should appear in the supplier dashboard" do
  el = find(".list-row-#{@new_list.id}")
  el.find('.currency').text.should =~ /6.66/
  el.find('.link-to-section').text.should == @other_section.title
  el.find('.table-number').text.should == "# #{@other_table.number}"
end

step "I click on the close list button in the supplier dashboard" do
  find(".list-row-#{@list.id} .close-list-button").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-button").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(%|.sections-header-container [data-section="#{@section.id}"] .section-header-title|).click
end

step "I click on the section main board section again" do
  step "I select the section in the supplier dashboard"
end

step "I select the other section in the supplier dashboard" do
  # find(%|.section_selector option[value="#{@other_section.id}"]|).select_option
  find(%|.sections-header-container [data-section="#{@other_section.id}"] .section-header-title|).click
end

step "I reset the section selector in the supplier dashboard" do
  # find(%|.section_selector option[value=""]|).select_option
  find(%|.sections-header-container [data-section="all"] .section-header-title|).click
end

step "the active list changes to another table in another section" do
  step "the list changes to another 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} .link-to-section").text ).to eq "New Section"
  expect( find(".order-row-#{@list.orders.first.id} .link-to-section").text ).to eq "New Section"
end

# Marking order as wrong
step "the supplier marks the order as wrong in the main board view" do
  find(".order-row-#{@order.id} .mark-order-cancelled-button").click
end

step "the supplier main board order should not be visible anymore" do
  page.should_not have_selector ".order-row-#{@order.id}"
end

step "the supplier main board list total should be updated" do
  find(".list-row-#{@list.id} .currency").text.should == "€ 0.00"
end

step "the supplier placed orders counter should be reduced" do
  expect_that_eventually selector: '.supplier-orders-placed-count-number', has_text: "10"
end

step "confirm the supplier close list modal" do
  find('.modal-confirm').click
end
