When(/^I go to the sales sheets section$/) do
  on(OA::ViewReleasePage).sales_sheets
end

Then(/^I should be on the OA::SingleSheetPage$/) do
  on(OA::SingleSheetPage) do |page|
    expect(page.current_url).to include page.page_url_value
    expect(page.generate_sales_sheet?).to be true
  end
end

Given(/^I am on the OA::SingleSheetPage for release (\d+)$/) do |release_id|
  visit(
    OA::SingleSheetPage,
    using_params: {
      queries: {
        "release_id" => release_id
      }
    }
  )
end

When(/^I generate the sales sheet$/) do
  on(OA::SingleSheetPage) do |page|
    time = Support::Helpers::Timezone.current_browser_time(true)
    # Uncomment the proper stuff when PHYS-4432 is fixed.
    # range_start = Time.now - 120
    range_start = Time.parse((time - 120).strftime("%I:%M:%S"))
    # The sales sheet should finish generating within 120 seconds of clicking `generate`
    # range_end = Time.now + 120
    range_end = Time.parse((time + 120).strftime("%I:%M:%S"))
    page.generate_sales_sheet
    eventually do
      # expect(Time.parse(page.last_generated_element.when_present.text[/(\d+):.+[M]\z/])
      expect(Time.parse(page.last_generated_element.when_present.text[/(\d+):\d{2}/]))
        .to be_between(range_start, range_end)
    end
  end
end

Then(/^I can download the sales sheet$/) do
  sales_sheet_url = on(OA::SingleSheetPage).sales_sheet_url
  @download = FileSystemHelpers::Downloader.download(sales_sheet_url).first
end

Then(/^the extension of the downloaded file is (.*)$/) do |extname|
  file_ext = File.extname(@download)
  expect(file_ext).to eql extname
end

Given(/^I have UPCs for multiple in-content physical products$/) do
  @upcs = [
    648_620_330_871,
    648_620_330_888,
    648_620_330_826
  ]
end

When(/^I go to Bulk Sales Sheets Generation$/) do
  on(OA::RelationshipsPage).bulk_sales_sheets_generation
end

Then(/^I should be on the SalesSheets::BulkGeneratePage$/) do
  on(SalesSheets::BulkGeneratePage) do |page|
    page.expect_to_be_on_page
    eventually(timeout: 10) { expect(page.submit?).to be true }
  end
end

When(/^I generate bulk sales sheets as (a combined PDF|multiple PDFs)$/) do |generate_type|
  @pdf_option = case generate_type
                when "a combined PDF"
                  "Combined PDF"
                when "multiple PDFs"
                  "Multiple PDFs"
                end
  on(SalesSheets::BulkGeneratePage) do |page|
    page.pdf_option = @pdf_option
    page.upcs = @upcs.join("\n")
    page.submit
  end
end

Then(/^I see the new report in the saved reports table$/) do
  on(SalesSheets::BulkGeneratePage) do |page|
    page.saved_reports_element.element.wait_until(&:present?)
    page.wait_until(60) { page.first_report_status == "Processing" }
    expect(page.first_report_type).to eql @pdf_option
  end
end

Then(/^I can download the bulk sales sheets file$/) do
  on(SalesSheets::BulkGeneratePage) do |page|
    page.wait_until { page.first_report_status == "Download" }
    # TODO: figure out a way to download from onclick event
  end
end

When(/^I download the sales sheet for my product$/) do
  on(Project::DigitalProductOverviewPage) do |page|
    page.wait_until { page.product_more_actions_element.when_present.present? }
    page.product_more_actions_element.wait_until(&:present?).click
    page.download_sales_sheet_option_element.click
  end
end

Then(/^I should see my download progress and finish$/) do
  on(Project::DigitalProductOverviewPage) do |page|
    page.wait_until { page.sheet_download_spinner_element.present? }
    eventually(timeout: 60) do
      expect(page.sheet_download_spinner_element.exists?).to be false
    end
  end
end
