step "the section table should be positioned in the section" do
  page.find(".section-table-#{@table.id}") # wait for table presence

  left = page.evaluate_script("$('.section-table-#{@table.id}').position().left")
  left.to_i.should > 0

  top = page.evaluate_script("$('.section-table-#{@table.id}').position().top")
  top.to_i.should > 0
end

step "the section table should be marked as having an active order" do
  assert_element_class ".section-table-#{@table.id}", 'active_order'
end

step "the section table should still be marked as having an active order" do
  step "the section table should be marked as having an active order"
end

step "the section table should be marked as occupied" do
  assert_element_class ".section-table-#{@table.id}", 'occupied'
end

step "the section table should be marked as in need of help" do
  assert_element_class ".section-table-#{@table.id}", 'needs_help'
end

step "the section table should not be marked as in need of help" do
  table = page.find(".section-table-#{@table.id}")
  table['class'].should_not include 'needs_help'
end

step 'the table should be marked as occupied and having an active order' do
  assert_element_class ".section-table-#{@table.id}", %w[occupied active_order]
end

step "I click on section table as a supplier" do
  table = page.find(".section-table-#{@table.id}")
  table.click
end

step "the section table should be marked as in need of payment" do
  page.should have_selector(".section-table-#{@table.id}.needs_payment")
end

step "A popup having table options should appear in supplier section area" do
  page.should have_selector(".section-table-#{@table.id} .table-actions")
end

step "I click on the mark list as helped in the section table popup as supplier" do
  # btn = page.find(".list-is-helped-button-#{@list.id}")
  page.find(".section-table-#{@table.id} .mark-list-as-helped-button").click
  # btn.click
end

step "the section table popup should no longer have the mark list as helped button" do
  page.should_not have_selector(".section-table-#{@table.id} .mark-list-as-helped-button")
end

step "I click on the close list button in the section table table popup" do
  # first click on the table to open the table actions popup
  #table = page.find(".section-table-#{@table.id}")
  # now click the close list button inside the table actions popup
  js_click ".section-table-#{@table.id} .table-actions .close-list-button"
end

step "the section table should not have any active list markings anymore" do
  table = page.find(".section-table-#{@table.id}")
  css_class = table['class']
  css_class.should_not include 'needs_help'
  css_class.should_not include 'occupied'
  css_class.should_not include 'active_order'
  css_class.should_not include 'needs_payment'
  ember_find('table', @table.id)['active_list'].should_not be_present
end

step "I should be redirected to the supplier section view" do
  sleep 1
  #page.current_path.should == "/supplier/sections/#{@section.id}"
  # page.evaluate_script('window.location.pathname').should == "/supplier/sections/#{@section.id}"
  js_path.should == "/supplier#/sections/#{@section.id}"
end

step "the last supplier section has a table with known coordinates" do
  @table = create :table, position_x: 4, position_y: 5, number: 74, supplier: @supplier, section: @sections.last
end

step "I click the supplier section edit mode button" do
  find('.section-edit-mode-button').click
end

step "I click the supplier section normal mode button" do
  find('.section-normal-mode-button').click
end

step "I fill in the supplier edit section form with new values" do
  js_set_field '.section-edit-title-field', 'RenamedSection'
  js_set_field '.section-edit-width-field', 40
  js_set_field '.section-edit-height-field', 52.7
end

step "the last section tab header should have the newly filled in name" do
  find(".section-tab-header-#{@sections.last.id}").text.should == 'RenamedSection'
end

step "the supplier last section properties should be updated to the new values" do
  sleep 1
  section = @sections.last
  section.reload
  section.title.should == 'RenamedSection'
  section.width.should == 40.0
  section.height.should == 52.7
end

step "I drag the table from the last section to the first section tab header" do
  first_section_tab_header = find(".section-tab-header-#{@sections.first.id}")
  table_view = find(".section-table-#{@table.id}")
  table_view.drag_to first_section_tab_header
end

step "the supplier table should have been linked to the first section" do
  @table.reload
  @table.section_id.should == @sections.first.id
end

step "the supplier clicks on the table having the new list in the section view" do
  js_click ".section-table-#{@table.id}"
end

step "the supplier table actions of the new list should be visible" do
  page.should have_selector ".section-table-#{@table.id} .table-actions .close-list-button"
end

step "the list should be linked to the original table in the section view" do
  step "the section table should be marked as having an active order"
end

step "the list should be linked to the other table in the section view" do
  table = page.find(".section-table-#{@other_table.id}")
  table['class'].should include 'active_order'
  table['class'].should include 'occupied'
end
