Given(/^I have a digital product that requires attention$/) do
  @product_id = 2_079_182
  Support::Helpers::ScenarioVariables.add_scenario_variable("Product id: ", @product_id)
end

Given(/^I have a digital product that requires attention I wish to resubmit$/) do
  @product_id = 2_128_392
  Support::Helpers::ScenarioVariables.add_scenario_variable("Product id: ", @product_id)
end

Given(/^I have a digital product that I wish to reject$/) do
  @product_id = 2_142_940
  Support::Helpers::ScenarioVariables.add_scenario_variable("Product id: ", @product_id)
end

Given(/^the release with upc (\d+) has been submitted$/) do |upc|
  @upc = upc
  step "I have a digital product that I wish to reject"
  puts "Release status at beginning of test: #{Support::DbClient.current_release_status(@product_id)}"
  if Support::DbClient.current_release_status(@product_id) != "transfer_to_content"
    session_token = Support::Services::GrassAuth.instance.workstation_session_token("label_7123")
    Support::Services::OwsProductDigital.submit_product(@product_id, session_token)
  end
  puts "Release status after resubmit: #{Support::DbClient.current_release_status(@product_id)}"
end

Then(/^I should see a fix errors button and product status should be action required$/) do
  on(Project::DigitalProductActionRequiredPage) do |page|
    eventually(timeout: 60) do
      expect(page.fix_errors_btn?).to be true
      expect(page.product_status).to eql "ACTION REQUIRED"
    end
  end
end

When(/^I check to see rejection reasons$/) do
  on(Project::DigitalProductActionRequiredPage) do |page|
    page.wait_until { page.see_reasons_button? }
    page.see_reasons_button
  end
end

When(/^I replace track's name$/) do
  on(Project::DigitalProductTracksPage) do |page|
    page.track_elements.first.click
    page.track_name_element.clear_text_and_type "A #{Time.stamp}"
    # Occasionally primary performers field is cleared. Below lines are to populate it when this happens
    if page.primary_performers_element.text == "-"
      step "I add a Primary performer with the Wind Instruments - Didgeridoo role"
      page.done
      page.wait_until { !page.performers_list? }
    end
    page.digital_product_next
  end
end

When(/^I save my artwork$/) do
  on(Project::ProductArtworkPage).save
end

When(/^I resubmit my product$/) do
  on(Project::DigitalProductActionRequiredPage) do |page|
    page.resubmit_element.wait_until(80, &:present?)
    page.resubmit
  end
end

Then(/^I should see the resubmit success modal$/) do
  on(Project::SubmitSuccessModal) do |modal|
    page_refresh = proc do |exception, try|
      puts "#{exception.class}: '#{exception.message}' - Retry attempt: #{try}"
      step "I resubmit my product"
    end
    Retriable.retriable(on: RSpec::Expectations::ExpectationNotMetError, tries: 3, on_retry: page_refresh) do
      eventually(timeout: 120) { expect(modal.submit_success_modal?).to be true }
      modal.submit_success_modal_close_element.click
    end
  end
end

Then(/^I should see comments for all fields$/) do
  on(Project::DigitalProductActionRequiredPage) do |page|
    expect(page.rejected_general_comments).to eql "Action Required test"
    expect(page.rejected_release_name_reason).to eql "bad product name"
    expect(page.rejected_artwork_reason).to eql "bad artwork"
    expect(page.rejected_track_name_reason).to eql "bad track"
    expect(page.rejected_release_date_reason).to eql "bad date"
  end
end

Then(/^I should see a rejected alert on all fields$/) do
  on(Project::DigitalProductActionRequiredPage) do |page|
    expect(page.item_has_alert(page.item_product_name_element)).to be true
    expect(page.item_has_alert(page.item_release_date_element)).to be true
    expect(page.item_has_alert(page.item_track_element)).to be true
    expect(page.rejected_artwork_element.exists?).to be true
  end
end

Then(/^I should see corrections on my expected fields$/) do
  on(Project::DigitalProductActionRequiredPage) do |page|
    page_refresh = proc do |exception, try|
      puts "#{exception.class}: '#{exception.message}' - Retry attempt: #{try}"
      page.refresh(@session_token)
    end
    Retriable.retriable(on: RSpec::Expectations::ExpectationNotMetError, tries: 2, on_retry: page_refresh) do
      expect(page.item_has_correction(page.item_product_name_element)).to be true
      expect(page.item_has_correction(page.item_release_date_element)).to be true
      expect(page.item_has_correction(page.artwork_element.parent)).to be true
      expect(page.item_has_correction(page.item_track_element)).to be true
    end
  end
end
