
step "the supplier visits the new product category page" do
  visit new_suppliers_product_category_path
end

step "the supplier fills in the new product category form selecting not available on tuesdays" do
  # default all days on, so click on tuesday deactivates it
  within '.modal' do
    js_set_field '.form-row.name input', 'New product category'
    find('.form-row.tuesday label').click
    js_click '.toggle-full-day-button'
    js_set_field '.select-start_from .hour-of-day select', '08'
    js_set_field '.select-start_from .minute-of-hour select', '20'
    js_set_field '.select-end_on .hour-of-day select', '12'
    js_set_field '.select-end_on .minute-of-hour select', '30'
  end
end

step "the supplier submits the product category form" do
  find('.modal .modal-save').click
end

step "then new product category with proper properties should have been created" do
  sleep 1
  @product_category = ProductCategory.find_by_name 'New product category'
  #@product_category.week_days.should == [0, 0, 1, 0, 0, 0, 0]
  @product_category.full_day.should be false
  @product_category.start_from.should be 500 # 8:20
  @product_category.end_on.should be 750 # 12:30
  expect( @product_category.active_on_monday ).to be true
  expect( @product_category.active_on_tuesday ).to be false
  expect( @product_category.active_on_wednesday ).to be true
  #@product_category.product_ids.should == [@products.first.id]
end

step "the supplier clicks on the add product button for the product category" do
  find('.add-product-product_category-button').click
end

step "the supplier fills in the new product form and submits it" do
  within ('.menu-product-new') do
    js_set_field '.name input', "Delicious pie"
    js_set_field '.price input', "4.75"
    js_set_field '.code input', "P079"
    find('.save-product-action').click
    sleep 0.5
  end
end

step "the product created by the supplier should have been created" do
  product = Product.find_by_name('Delicious pie')
  expect( product.price ).to eq 4.75
  expect( product.code ).to eq "P079"
  expect( product.product_category ).to eq @product_category
end

step "the supplier should be on the product categoy overview path" do
  route_should_be "suppliers/product_categories#index"
end

step "the supplier clicks on the edit product category button" do
  within "#product_category_#{@product_category.id}" do
    find('.table-edit').click
  end
end

step "the supplier unchecks the first product and checks the last product" do
  find("#product-checker-#{@products.first.id}").set false
  find("#product-checker-#{@products.last.id}").set true
end

step "the supplier clicks on the tuesday and wednesday buttons selecting wednesday as active day" do
  find('[data-day="tuesday"]').click
  find('[data-day="wednesday"]').click
end

step "the the product category is active on wednesday and only linked to the last product" do
  sleep 1
  @product_category.reload
  @product_category.week_days.should == [0, 0, 0, 1, 0, 0, 0]
  @product_category.product_ids.should == [@products.last.id]
end

step "there are :count supplier product categories" do |count|
  @product_categories = create_list :product_category, count.to_i, supplier: @supplier
end

step "there is a product category with product available on :day between :category_start_time and :category_end_time" do |day, category_start_time, category_end_time|
  step 'there is a product'
  start_minute = (Time.parse(category_start_time).seconds_since_midnight / 60).round
  end_minute = (Time.parse(category_end_time).seconds_since_midnight / 60).round
  end_minute += 1440 if end_minute < start_minute #night offset option
  @product_category = create :product_category,
    supplier: @supplier,
    week_days: [0,0,0,0,0,1,0],
    product_ids: [@product.id],
    start_from: start_minute,
    end_on: end_minute
end

step "the product category should not be visible on :week_day :time" do |week_day, time|
  timestamp = Time.parse(time)
  needed_wday = Date::DAYNAMES.index{|d| d == week_day}
  raise "cannot find day of week for #{week_day}" unless needed_wday.is_a?(Integer)
  timestamp = timestamp + (needed_wday - timestamp.wday).days unless timestamp.wday == needed_wday
  product_categories = ProductCategory.for_supplier_in_time(@supplier, timestamp)
  product_categories.should be_empty
end

step "the product category should be visible on :week_day :time" do |week_day, time|
  timestamp = Time.parse(time)
  needed_wday = Date::DAYNAMES.index{|d| d == week_day}
  raise "cannot find day of week for #{week_day}" unless needed_wday.is_a?(Integer)
  timestamp = timestamp + (needed_wday - timestamp.wday).days unless timestamp.wday == needed_wday
  product_categories = ProductCategory.for_supplier_in_time(@supplier, timestamp)
  product_categories.should include @product_category
end
