Given(/^I search for release (.*)$/) do |search_term|
  visit(OA::RelationshipsPage).search_with_option("Release", search_term)
end

When(/^I click the Edit Release button$/) do
  on(OA::ViewReleasePage).edit_release
end

When(/^I clear all required release field$/) do
  on(OA::ViewReleasePage).edit_release
  on(OA::EditReleasePage) do |page|
    page.clear_digital_release_required_info
    page.submit
  end
end

Then(/^I should get release required field missing error$/) do
  on(OA::EditReleasePage) do |page|
    error_message = "Please select release meta language.\n"\
    "Please enter C-Line.\n"\
    "Release name is missing.\n"\
    "Subgenre is missing."
    expect(page.red_err_message).to eql error_message
  end
end

When(/^I (added|removed) carveouts for gras and proper to (.*)/) do |carveout_case, upc|
  visit(OA::AddEditCarveoutPage, using_params: { queries: { "upc" => upc } }) do |page|
    case carveout_case
    when "added"
      page.check_gras
      page.check_proper
      page.submit
    when "removed"
      page.uncheck_gras
      page.uncheck_proper
      page.submit
    end
  end
end

Then(/^I should see the carveouts (added|removed)$/) do |carveout_case|
  on(OA::AddEditCarveoutPage) do |page|
    case carveout_case
    when "added"
      expect(page.confirmation).to include("3 records have been inserted!")
      expect(page.gras_checked?).to be true
      expect(page.proper_checked?).to be true
    when "removed"
      expect(page.gras_checked?).to be false
      expect(page.proper_checked?).to be false
    end
  end
end

When(/^I set all the required field$/) do
  on(OA::EditReleasePage) do |page|
    page.set_digital_release_required_info
    page.submit
  end
end

Then(/^I should be able to save my changes successfully$/) do
  on(OA::ViewReleasePage) do |page|
    expect(page.green_confirmation_message?).to be true
  end
end

Then(/^I should be on the edit release page for UPC (.*)$/) do |upc|
  on(OA::EditReleasePage, using_params: { queries: { "upc" => upc } }).expect_to_be_on_page
end

When(/^I visit the edit release page for UPC (.*)$/) do |upc|
  visit(OA::EditReleasePage, using_params: { queries: { "upc" => upc } })
end

When(/^I visit the edit release page for my product$/) do
  # Implies UPC is dynamic and known
  visit(OA::EditReleasePage, using_params: { queries: { "upc" => @upc } })
end

When(/^I visit the sales goals page for my release$/) do
  visit(OA::SalesGoalsPage, using_params: { queries: { "release_id" => @product_id } })
  on(OA::SalesGoalsPage).oa_goals_div_element.when_present(90)
end

When(/^I delete any existing goals in OA$/) do
  on(OA::SalesGoalsPage).delete_all_goals
end

When(/^I enter my edits$/) do
  on(OA::SalesGoalsPage) do |page|
    page.edit_goal_elements[0].click
    page.first_week_estimate_element.clear
    page.first_week_estimate = "11"
    page.goal_input_elements[0].clear
    page.goal_input_elements[0].send_keys "2"
  end
end

When(/^I save my sales goal and save and continue$/) do
  on(OA::SalesGoalsPage) do |page|
    page.save
    page.save_and_continue_element.when_present.click
  end
  eventually { expect(on(OA::EditLabelCopyReleasePage).label_release_meta_language?).to be true }
end

Then(/^I should see no goal values on the page in OA$/) do
  eventually(timeout: 10) do
    expect(on(OA::SalesGoalsPage).goal_value_elements.any?).to be false
  end
end

Then(/^I should see expected values for my sales goals$/) do
  on(OA::SalesGoalsPage) do |page|
    page.edit_goal_elements[0].click
    expect(page.first_week_estimate).to eql "11"
    expect(page.goal_input_elements[-1].value).to eql "2"
  end
end

And(/^I save "(.*?)" ratings$/) do |country|
  @timestamp = Time.stamp
  on(OA::EditReleasePage) do |page|
    page.country_rating[country]
    page.add_country_rating(country, @timestamp)
    page.submit
  end
end

Then(/^I should see that ratings for "(.*?)" saved successfully$/) do |arg1|
  on(OA::EditReleasePage) do |page|
    page.get_rating_values(arg1, @timestamp)
  end
end

And(/^I have an existing release to copy to$/) do
  visit(OA::CopyTracksPage) do |page|
    @new_upc = "887396363298" # 1 track on prod
    page.lookup_upc("new", @new_upc)
  end
end

When(/^I copy tracks from a release to that existing release$/) do
  on(OA::CopyTracksPage) do |page|
    page_tracks = page.new_tracks_count
    if page_tracks == "1 track(s)"
      old_release = "887396356238"
      @new_track_count = "2"
    elsif page_tracks == "2 track(s)"
      old_release = "887396356160"
      @new_track_count = "1"
    else
      fail "problem with upc lookup! found number of tracks: #{page_tracks}"
    end

    puts "using release #{old_release}"
    page.lookup_upc("original", old_release)

    expect(page.original_tracks_count).to include @new_track_count
    page.copy_tracks_submit_button
  end
end

Then(/^I should see the tracks from the original release copied to the existing one$/) do
  on(OA::EditReleasePage) do |page|
    page.wait_until { page.green_confirmation_message? }
    fail "Tracks copied to the wrong UPC" unless page.orchard_upc == @new_upc
    # green_confirmation_message will have the text '_ track(s) have been copied to this release'
    fail "Wrong number of tracks copied" unless page.green_confirmation_message.include? @new_track_count
  end
end

When(/^I add localizations for the film release (.*?)$/) do |upc|
  languages = ["English (Ireland)", "Cantonese (Script)", "Spanish (Latin America)"]
  @film_localization_data = DataHelpers::FilmLocalizations.new(languages)
  visit(OA::ViewReleasePage, using_params: { queries: { "upc" => upc } }).manage_localization
  on(OA::LocalizationsPage) do |page|
    page.delete_film_localizations
    page.add_film_localizations(@film_localization_data.data)
    page.save_changes
  end
end

And(/^there are no localizations for UPC (\d+)$/) do |upc|
  query = "SELECT release_id FROM releases WHERE display_upc = '#{upc}'"
  release_id = Support::DbClient.query_art_relations(query).first["release_id"]
  visit(OA::LocalizationsPage, using_params: { release_id: release_id }) do |page|
    page.delete_film_localizations
    page.save_changes
  end
end

When(/^I attempt to add a film localization (without_required|with_duplicated) fields$/) do |action|
  case action
  when "without_required"
    on(OA::LocalizationsPage) do |page|
      page.add_localization
      page.save_changes
    end

  when "with_duplicated"
    on(OA::LocalizationsPage) do |page|
      @add_release_language = "English (Australia)"
      page.add_localization
      page.release_language_id_elements[0].select(@add_release_language)
      page.add_localization
      page.release_language_id_elements[1].select(@add_release_language)
      page.save_changes
    end
  end
end

Then(/^I should see the validation messages for the (required|duplicate) localization fields$/) do |action|
  validation_messages = on(OA::LocalizationsPage).validation_messages
  case action
  when "without_required"
    expect(validation_messages).to include "Release name localization is required."
    expect(validation_messages).to include "Long synopsis localization is required."
  when "with_duplicated"
    expect(validation_messages).to include "Duplicate languages found for Release Name"
  end
end

When(/^I set values for new localization and save$/) do
  on(OA::LocalizationsPage) do |page|
    page.add_localization
    @film_localization = {
      language: "English (Australia)",
      name: "Test",
      long_synopsis: "Test Long Synopsis.Test Long Synopsis.Test Long Synopsis.Test Long Synopsis.",
      short_synopsis: "Test Short Synopsis."
    }
    page.populate_first_localization(@film_localization)
    page.save_changes
  end
end

When(/^I update values and save$/) do
  on(OA::LocalizationsPage) do |page|
    @film_localization = {
      language: "Portuguese",
      name: "Test Localization",
      long_synopsis: "Test Long Synopsis.Test Long Synopsis.Test Long Synopsis.Test Long Synopsis.",
      short_synopsis: "Test Short Synopsis."
    }
    page.populate_first_localization(@film_localization)
    page.save_changes
  end
end

Then(/^I should be able to see the values of localization$/) do
  on(OA::LocalizationsPage) do |page|
    message = "Release localizations saved successfully."
    expect(page.green_confirmation_message_element.text).to eql message
    page.check_localization_saved(@film_localization)
  end
end

When(/^I remove the newly added localization$/) do
  on(OA::LocalizationsPage) do |page|
    page.delete_film_localizations
    page.save_changes
  end
end

Then(/^the localization should no longer exist$/) do
  on(OA::LocalizationsPage) do |page|
    expect(page.release_name_localization_element.element.present?).to be false
  end
end

Given(/^I am on the OA::ManagePricingPage for the created release$/) do
  @product_id = "2132465"
  visit(OA::ManagePricingPage, using_params: { release_id: @product_id })
end

And(/^and I change the newly created releases price$/) do
  on(OA::ManagePricingPage) do |page|
    page.edit_default_pricing_element.when_present.click
    prices = page.default_price_select_options
    prices.delete(page.default_price_select)
    @price = prices.sample
    page.default_price_select = @price
    page.wait_until { page.default_price_select == @price }
    page.submit
  end
end

Then(/^I should see the updated release pricing$/) do
  on(OA::ManagePricingPage) do |page|
    page.refresh
    page.edit_default_pricing_element.when_present.click
    page.wait_until { page.default_price_select == @price }
    expect(page.default_price_select).to eql(@price)
  end
end

And(/^I generate metadata update queue for (.*?)$/) do |upc|
  visit(OA::CreateMetaUpdate, using_params: { queries: { "upc" => upc } }) do |page|
    page.check_check_all
    page.submit_update
  end
  on(OA::MetaUpdateQueueSummary) do |page|
    page.wait_until do
      page.confirmation_div.downcase.include? "request has been added to the queue successfully."
    end
  end
end

Then(/^I can add timed release for (.*?) at (.*?)$/) do |store, time|
  on(OA::EditReleasePage) do |page|
    page.page_loader_element.when_not_present
    page.timed_release_link_element.click
    page.release_time_element.send_keys :enter
    page.select_dms(store)
    page.select_date
    page.release_time_element.send_keys time
    page.release_time_element.send_keys :enter
    page.add_to_schedule
    page.save_and_close
  end
end

And(/^I should be able to see the new Timed Release at (.*?)$/) do |time|
  on(OA::EditReleasePage) do |page|
    page.release_schedule_table_element.when_present
    sale_times = page.sale_time_cell
    expect(sale_times).to include("#{time} AM GMT")
  end
end
