Given(/^I have an existing report preset$/) do
  session_token = Support::Services::GrassAuth.instance.workstation_session_token("label_21989")
  @preset_name = "My preset #{Time.stamp}"
  Support::Services::OwsReporting.create_preset(@preset_name, session_token)
end

When(/^I begin to generate a (.*?) report type/) do |view_type|
  on(PhysicalReporting::PhysicalReportingPage) do |page|
    page.results_table_element.wait_until(&:present?)
    page.select_type_of_report(view_type)
    case view_type
    when "Product View", "Historical Product View"
      expect(page.product?).to be true
    when "Retailer View"
      expect(page.retailer?).to be true
    when "Point Of Sale View"
      expect(page.country?).to be true
    else
      fail "unknown report type: '#{view_type}'!"
    end
    expect(page.artist?).to be true
    expect(page.product_type?).to be true
    expect(page.release_date?).to be true
    expect(page.generate_report_button?).to be true
  end
end

When(/^I generate the report/) do
  on(PhysicalReporting::PhysicalReportingPage) do |page|
    page.close_open_select_element.click if page.close_open_select?
    page.generate_report_button
    page.wait_for_spinner_to_disappear
  end
end

When(/^I select (.*) in my territory dropdown$/) do |territory|
  on(PhysicalReporting::PhysicalReportingPage).select_territory territory
  # selecting territory generates report re: PHYS-3400
  on(PhysicalReporting::PhysicalReportingPage).wait_for_spinner_to_disappear
end

When(/^I select artists? (.*?)$/) do |value|
  on(PhysicalReporting::PhysicalReportingPage) do |page|
    artists = value.split(", ")
    artists.each do |artist|
      page.select_artist artist
    end
  end
end

When(/^I select product types (.*)/) do |value|
  on(PhysicalReporting::PhysicalReportingPage) do |page|
    product_types = value.split(", ")
    product_types.each do |product_type|
      page.select_product_type product_type
    end
  end
end

When(/^I remove filters (.*)/) do |value|
  on(PhysicalReporting::PhysicalReportingPage) do |page|
    product_types = value.split(", ")
    product_types.each do |product_type|
      page.remove_filter(product_type)
    end
  end
end

Then(/^the first 12 results for column (.*?) are either (.*?) or (.*?)$/) do |column, result1, result2|
  on(PhysicalReporting::PhysicalReportingPage) do |page|
    column_index = page.column_index(column)
    page.row_elements[1, 12].each do |row|
      row_text = page.cell_text(row, column_index)
      expect(row_text == result1 || row_text == result2).to eq(true)
    end
  end
end

When(/^I generate report for product (.*)/) do |value|
  on(PhysicalReporting::PhysicalReportingPage) do |page|
    page.select_product value
    page.close_open_select_element.click if page.close_open_select?
    page.generate_report_button
    page.wait_for_spinner_to_disappear
  end
end

When(/^I generate report with type (.*)/) do |value|
  on(PhysicalReporting::PhysicalReportingPage) do |page|
    page.select_product_type value
    page.close_open_select_element.click if page.close_open_select?
    page.generate_report_button
    page.wait_for_spinner_to_disappear
  end
end

When(/^I generate report with status (.*)/) do |value|
  on(PhysicalReporting::PhysicalReportingPage) do |page|
    page.select_product_status value
    page.close_open_select_element.click if page.close_open_select?
    page.generate_report_button
    page.wait_for_spinner_to_disappear
  end
end

When(/^I generate report with artist (.*)/) do |value|
  on(PhysicalReporting::PhysicalReportingPage) do |page|
    page.select_artist value
    page.close_open_select_element.click if page.close_open_select?
    page.generate_report_button
    page.wait_for_spinner_to_disappear
  end
end

When(/^I click the metadata selection edit button$/) do
  on(PhysicalReporting::PhysicalReportingPage).metadata_selection_element.click
end

When(/^I click the (.*?) column checkbox in the modal$/) do |column|
  on(PhysicalReporting::PhysicalReportingPage).click_column_checkbox column
end

When(/^I click the (.*?) column checkbox$/) do |column|
  on(PhysicalReporting::PhysicalReportingPage).click_column_checkbox(column, false)
end

When(/^I click the column selection done button$/) do
  on(PhysicalReporting::PhysicalReportingPage).column_selection_done
end

When(/^I create and save a preset$/) do
  on(PhysicalReporting::PhysicalReportingPage) do |page|
    page.save_as_preset
    @preset_name = "My preset #{Time.stamp}"
    page.present_name = @preset_name
    page.save_preset
  end
end

When(/^I open my report preset$/) do
  on(PhysicalReporting::PhysicalReportingPage) do |page|
    page.results_table_element.wait_until(&:present?)
    page.preset_dropdown_element.click
    page.preset_elements.find { |preset| preset.text == @preset_name }.click
    page.wait_for_spinner_to_disappear
  end
end

When(/^I delete my report preset$/) do
  on(PhysicalReporting::PhysicalReportingPage) do |page|
    page.results_table_element.wait_until(&:present?)
    page.preset_dropdown_element.click
    page.preset_elements.find { |preset| preset.span_element.text == "Manage Presets" }.click
    index = page.modal_preset_elements.find_index { |preset| preset.text == @preset_name }
    page.delete_preset_elements[index].scroll.to
    page.delete_preset_elements[index].click
    page.delete_confirm_element.click
    page.preset_done
  end
end

When(/^I click the single product overview link for the first result in the table$/) do
  on(PhysicalReporting::PhysicalReportingPage) do |page|
    index = page.column_index("Product Name")
    cell = page.row_elements[1].div_elements(class: "public_fixedDataTableCell_cellContent")[index]
    cell.scroll.to
    cell.a.click
    page.browser.window.close
  end
end

When(/^I select a start month of (.*)$/) do |month|
  on(PhysicalReporting::PhysicalReportingPage).select_start_month month
end

When(/^I select an end month of (.*)$/) do |month|
  on(PhysicalReporting::PhysicalReportingPage).select_end_month month
end

When(/^I choose to include co-op data$/) do
  on(PhysicalReporting::PhysicalReportingPage) do |page|
    page.include_coop_element.click
    page.wait_for_spinner_to_disappear
  end
end

Then(/^I should (|not )see my preset in the presets list$/) do |truth|
  on(PhysicalReporting::PhysicalReportingPage) do |page|
    expectation = truth == "" ? false : true
    page.preset_dropdown_element.click
    eventually(timeout: 1) do
      expect(page.preset_elements.find { |preset| preset.text == @preset_name }.nil?).to be expectation
    end
  end
end

Then(/^I should (|not )be able to see the co-op columns$/) do |truth|
  on(PhysicalReporting::PhysicalReportingPage).wait_for_spinner_to_disappear
  coop_column_strings = ["Total RTD Co-Op $", "Closed Co-Op $", "Open Co-Op $"]
  coop_column_strings.each { |column| step("I should #{truth}be able to see the #{column} column") }
end

Then(/^I should (|not )be able to see the (.*?) column$/) do |truth, column_name|
  column = on(PhysicalReporting::PhysicalReportingPage).column_name_elements.find do |header|
    header.parent.attribute_value("data-tip") == column_name
  end
  truth == "" ? (expect(!column.nil?).to be true) : (expect(column.nil?).to be true)
end

Then(/^I should see the results table with at least 1 result$/) do
  on(PhysicalReporting::PhysicalReportingPage) do |page|
    expect(page.results_table?).to be true
    expect(page.row_elements.count - 1).to be >= 1
  end
end

Then(/^I should see the results table with (\d+) results?$/) do |results|
  on(PhysicalReporting::PhysicalReportingPage) do |page|
    expect(page.results_table?).to be true
    expect(page.row_elements.count - 1).to eq results.to_i
  end
end

Then(/^the first 12 results for column (.*?) equal (.*?)$/) do |column, result|
  on(PhysicalReporting::PhysicalReportingPage) do |page|
    column_index = page.column_index(column)
    page.row_elements[1, 12].each do |row|
      row_text = page.cell_text(row, column_index)
      expect(row_text).to eq(result)
    end
  end
end

Then(/^the first 12 results for the Product Status column equal (.*?)$/) do |result|
  on(PhysicalReporting::PhysicalReportingPage) do |page|
    # product status tool tip DOES NOT equal column name - it is the following long string
    product_status = "Is this product Active - New Release, Active - Catalog,"\
                        " Cutout Accepting Returns, Cutout Not Accepting Returns, Inactive"
    column_index = page.column_index(product_status)
    page.row_elements[1, 12].each do |row|
      row_text = page.cell_text(row, column_index)
      expect(row_text).to eq(result)
    end
  end
end

Then(/^I should see content in the frontend reporting div?$/) do
  on(PhysicalReporting::PhysicalReportingPage) do |page|
    eventually do
      expect(page.frontend_reporting_app_div_element.inner_html.empty?).to be false
    end
    page.wait_until { page.div_elements(class: "LoadingIndicator").none? }
  end
end

Then(/^my product name filter should be (.*?)$/) do |expected_value|
  expect(on(PhysicalReporting::PhysicalReportingPage).product_name_value).to include expected_value
end

Then(/^my artist name filter should be (.*?)$/) do |expected_value|
  expect(on(PhysicalReporting::PhysicalReportingPage).artist_name_value).to include expected_value
end

Then(/^I should see an artist name of (.*?) on the single product overview page$/) do |artist|
  expect(on(PhysicalReporting::SingleProductOverviewPage).artist_name).to eq artist
end

Then(/^I should see a product name of (.*?) on the single product overview page$/) do |product|
  expect(on(PhysicalReporting::SingleProductOverviewPage).product_name).to eq product
end

Then(/^I should see data in my single product overview tables$/) do
  on(PhysicalReporting::SingleProductOverviewPage) do |page|
    page.product_table_elements.each { |table| expect(table.tbody.rows.count > 0).to be true }
  end
end

And(/^I set the weeks? for the report from (.*?) to (.*?)$/) do |start_date, end_date|
  on(PhysicalReporting::PhysicalReportingPage) do |page|
    page.set_week_date(start_date, "first")
    page.set_week_date(end_date, "last")
    page.wait_until { !page.calendar_grid? }
  end
end

And(/^I filter physical reports by the (.*?): (.*?)$/) do |type, search_val|
  on(PhysicalReporting::PhysicalReportingPage) do |page|
    page.send("select_#{type}", search_val)
    page.wait_for_spinner_to_disappear
  end
end

Then(/^I am able to see physical sales data from only (.*?)$/) do |search_val|
  on(PhysicalReporting::PhysicalReportingPage) do |page|
    page.row_elements[1, 5].each do |row|
      # Can't do row.text here. For some odd reason, it only pulls in text from columns that are
      # currently displayed on the screen.
      row_text = page.execute_script("return arguments[0].textContent", row)
      expect(row_text).to include search_val
    end
  end
end
