Given(/^I am on the OA::ViewReleasePage for UPC (.*)$/) do |desired_upc|
  visit(OA::ViewReleasePage, using_params: { queries: { "upc" => desired_upc } })
end

When(/^I edit the track details$/) do
  on(OA::ViewReleasePage).track_list_table_element["1"]["Edit"].link_element.click
end

When(/^I reset all the mandatory fields for the first track$/) do
  on OA::TrackPage do |page|
    page.first_track_meta_language = "Select Language"
    page.first_track_type = "Select One"
  end
end

And(/^I update the track info$/) do
  on(OA::TrackPage).update_button
end

Then(/^I should see error notification "(.*?)"$/) do |message|
  on OA::TrackPage do |page|
    expect(page.error_notification_element.when_present.element.text).to eq message
  end
end

When(/^I set track type to "(.*?)"$/) do |track_type|
  on(OA::TrackPage).first_track_type = track_type
end

Then(/^I should see success notification "(.*?)"$/) do |message|
  on OA::TrackPage do |page|
    expect(page.green_confirmation_message_element.when_present.element.text).to eq message
  end
end

When(/^I (enter|verify) track data in (default|lnc) fields$/) do |action_type, field_type|
  data = if field_type == "default"
           DataHelpers::TrackData.oa_original_fields
         else
           DataHelpers::TrackData.oa_all_fields
         end

  # appending 'first_track_' to yml file data to match OA::TrackPage's page object elements
  data = Hash[data.map { |k, v| ["first_track_#{k}".to_sym, v] }]

  on(OA::TrackPage) do |page|
    # Wait if Offer Type list is loading slowly
    page.wait_until { page.first_track_offer_type_options.include? "All" }
    if action_type == "enter"
      page.populate_page_with(data)
    else
      page.verify_page_has(data)
    end
  end
end

Given(/^a YouTube release in OA$/) do
  visit(OA::ViewReleasePage, using_params: { queries: { "upc" => "888608286756" } })
end

When(/^I add a new track to that release$/) do
  time = Time.stamp
  track_data = {
    track_type: "Video",
    id: "ID#{time}",
    name: "Name#{time}",
    language: "English",
    country: "American Samoa"
  }
  on(OA::ViewReleasePage).add_track
  on(OA::TrackPage).add_new_track(track_data)
end

When(/^I update the crop information for a film track$/) do
  crops = %i[crop_top crop_bottom crop_left crop_right]
  # Grabs last 8 digits of a timestamp and splits it into a list of four 2-character numeric strings
  crop_vals = Time.stamp.last(8).scan(/.{2}/)
  @crop_info = Hash[crops.zip crop_vals]
  visit(OA::TrackPage, using_params: {
          queries: { "upc" => "889176506765", "tuid" => "19011550" }
        }) do |page|
    @crop_info.each do |k, v|
      puts "Current #{k}: #{page.send k}\n"
      puts "New #{k}: #{v}\n"
      # :to_i first in case string has a lead zero
      page.send("#{k}=", v.to_i.to_s)
    end
    page.update_button
  end
end

Then(/^I verify crop information data$/) do
  on OA::TrackPage do |page|
    revisit = proc do |exception, try|
      sleep 3
      puts "#{exception.class}: '#{exception.message}' - Retry attempt: #{try}"
      page.goto
    end
    Retriable.retriable(on: [RSpec::Expectations::ExpectationNotMetError, JSON::ParserError],
                        tries: 5, on_retry: revisit) do
      @crop_info.each { |k, v| expect(page.crop_on_endpoint[k.to_s]).to eq v.to_i }
      @crop_info.each { |k, v| expect((page.send k)).to eq v.to_i.to_s }
    end
  end
end
