Then(/^I see that the physical reporting page works$/) do
  on(ReactWorkstation::ReactAudiencePage) do |page|
    page.div_element(class: "reporting").wait_until(&:present?)
  end
end

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

When(/^in new react reporting page I begin to generate a (.*?) report type/) do |view_type|
  on(ReactWorkstation::ReactPhysicalReportingPage) 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(/^in new react reporting page I generate the report/) do
  on(ReactWorkstation::ReactPhysicalReportingPage) 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(/^in new react reporting page I select (.*) in my territory dropdown$/) do |territory|
  on(ReactWorkstation::ReactPhysicalReportingPage).select_territory territory
  # selecting territory generates report re: PHYS-3400
  on(ReactWorkstation::ReactPhysicalReportingPage).wait_for_spinner_to_disappear
end

When(/^in new react reporting page I select artists? (.*?)$/) do |value|
  on(ReactWorkstation::ReactPhysicalReportingPage) do |page|
    artists = value.split(", ")
    artists.each do |artist|
      page.select_artist artist
    end
  end
end

When(/^in new react reporting page I select product types (.*)/) do |value|
  on(ReactWorkstation::ReactPhysicalReportingPage) do |page|
    product_types = value.split(", ")
    product_types.each do |product_type|
      page.select_product_type product_type
    end
  end
end

When(/^in new react reporting page I remove filters (.*)/) do |value|
  on(ReactWorkstation::ReactPhysicalReportingPage) do |page|
    product_types = value.split(", ")
    product_types.each do |product_type|
      page.remove_filter(product_type)
    end
  end
end

Then(
  /^in new react reporting page the first 12 results for column (.*?) are either (.*?) or (.*?)$/
) do |column, result1, result2|
  on(ReactWorkstation::ReactPhysicalReportingPage) 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(/^in new react reporting page I generate report for product (.*)/) do |value|
  on(ReactWorkstation::ReactPhysicalReportingPage) 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(/^in new react reporting page I generate report with type (.*)/) do |value|
  on(ReactWorkstation::ReactPhysicalReportingPage) 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(/^in new react reporting page I generate report with status (.*)/) do |value|
  on(ReactWorkstation::ReactPhysicalReportingPage) 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(/^in new react reporting page I generate report with artist (.*)/) do |value|
  on(ReactWorkstation::ReactPhysicalReportingPage) 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(/^in new react reporting page I click the metadata selection edit button$/) do
  on(ReactWorkstation::ReactPhysicalReportingPage).metadata_selection_element.click
end

When(/^in new react reporting page I click the (.*?) column checkbox in the modal$/) do |column|
  on(ReactWorkstation::ReactPhysicalReportingPage).click_column_checkbox column
end

When(/^in new react reporting page I click the (.*?) column checkbox$/) do |column|
  on(ReactWorkstation::ReactPhysicalReportingPage).click_column_checkbox(column, false)
end

When(/^in new react reporting page I click the column selection done button$/) do
  on(ReactWorkstation::ReactPhysicalReportingPage).column_selection_done
end

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

When(/^in new react reporting page I open my report preset$/) do
  on(ReactWorkstation::ReactPhysicalReportingPage) 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(/^in new react reporting page I delete my report preset$/) do
  on(ReactWorkstation::ReactPhysicalReportingPage) 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(
  /^in new react reporting page I click the single product overview link for the first result in the table$/
) do
  on(ReactWorkstation::ReactPhysicalReportingPage) 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(/^in new react reporting page I select a start month of (.*)$/) do |month|
  on(ReactWorkstation::ReactPhysicalReportingPage).select_start_month month
end

When(/^in new react reporting page I select an end month of (.*)$/) do |month|
  on(ReactWorkstation::ReactPhysicalReportingPage).select_end_month month
end

When(/^in new react reporting page I choose to include co-op data$/) do
  on(ReactWorkstation::ReactPhysicalReportingPage) do |page|
    page.include_coop_element.click
    page.wait_for_spinner_to_disappear
  end
end

Then(/^in new react reporting page I should (|not )see my preset in the presets list$/) do |truth|
  on(ReactWorkstation::ReactPhysicalReportingPage) 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(/^in new react reporting page I should (|not )be able to see the co-op columns$/) do |truth|
  on(ReactWorkstation::ReactPhysicalReportingPage).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(/^in new react reporting page I should (|not )be able to see the (.*?) column$/) do |truth, column_name|
  column = on(ReactWorkstation::ReactPhysicalReportingPage).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(/^in new react reporting page I should see the results table with at least 1 result$/) do
  on(ReactWorkstation::ReactPhysicalReportingPage) do |page|
    expect(page.results_table?).to be true
    expect(page.row_elements.count - 1).to be >= 1
  end
end

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

Then(/^in new react reporting page the first 12 results for column (.*?) equal (.*?)$/) do |column, result|
  on(ReactWorkstation::ReactPhysicalReportingPage) 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(
  /^in new react reporting page the first 12 results for the Product Status column equal (.*?)$/
) do |result|
  on(ReactWorkstation::ReactPhysicalReportingPage) 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(/^in new react reporting page I should see content in the frontend reporting div?$/) do
  on(ReactWorkstation::ReactPhysicalReportingPage) 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(/^in new react reporting page my product name filter should be (.*?)$/) do |expected_value|
  expect(on(ReactWorkstation::ReactPhysicalReportingPage).product_name_value).to include expected_value
end

Then(/^in new react reporting page my artist name filter should be (.*?)$/) do |expected_value|
  expect(on(ReactWorkstation::ReactPhysicalReportingPage).artist_name_value).to include expected_value
end

Then(
  /^in new react reporting page I should see an artist name of (.*?) on the single product overview page$/
) do |artist|
  expect(on(ReactWorkstation::ReactSingleProductOverviewPage).artist_name).to eq artist
end

Then(
  /^in new react reporting page I should see a product name of (.*?) on the single product overview page$/
) do |product|
  expect(on(ReactWorkstation::ReactSingleProductOverviewPage).product_name).to eq product
end

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

And(
  /^in new react reporting page I set the weeks? for the report from (.*?) to (.*?)$/
) do |start_date, end_date|
  on(ReactWorkstation::ReactPhysicalReportingPage) 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(/^in new react reporting page I filter physical reports by the (.*?): (.*?)$/) do |type, search_val|
  on(ReactWorkstation::ReactPhysicalReportingPage) do |page|
    page.send("select_#{type}", search_val)
    page.wait_for_spinner_to_disappear
  end
end

Then(/^in new react reporting page I am able to see physical sales data from only (.*?)$/) do |search_val|
  on(ReactWorkstation::ReactPhysicalReportingPage) 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
