When(/^I am on OA::ViewVendorPage for a label with a YouTube channel/) do
  visit OA::ViewVendorPage, using_params: { queries: { "vendor_id" => "22700" } }
end

Then(/^I view the expanded track list/) do
  on OA::ViewReleasePage do |page|
    page.view_expanded_link_element.click
    expect(page.current_url).to include "expanded=1"
  end
end

Then(/^I should see the Video ID fields for tracks/) do
  expect(on(OA::ViewReleasePage).first_expanded_track_element.element.text).to include "Video ID"
  on(OA::ManageYouTubeChannelsPage).close_youtube_channel_child_page
end

Then(/^I should get a confirmation message$/) do
  expect(on(OA::ViewReleasePage).green_confirmation_message).to include "Track Successfully Added."
end

Then(/^I am able to delete the track I just created$/) do
  on(OA::ViewReleasePage).track_list_table_element[-1]["Edit"].link_element.click
  on(OA::TrackPage).delete_button
  expect(on(OA::ViewReleasePage).green_confirmation_message).to include "Track successfully deleted."
  on(OA::ManageYouTubeChannelsPage).close_youtube_channel_child_page
end

Then(/^I should see YouTube Partnered Channels information$/) do
  on OA::ViewVendorPage do |page|
    fail "Youtube Partnered Channels box doesn't exist" unless page.youtube_details_header_element.exists?
    puts "Account Manager: " + page.account_manager_name_element.text
    puts "Manager's Youtube Channel: " + page.youtube_channel_name_element.text
  end
  on(OA::ManageYouTubeChannelsPage).close_youtube_channel_child_page
end

When(/^I edit a YouTube Channel$/) do
  visit OA::EditYoutubeChannelPage, using_params: { channel_id: "136" }
end

Then(/^I get a validation message when I submit the form without channel information/) do
  on OA::EditYoutubeChannelPage do |page|
    page.submit_button
    puts "Error message: #{page.channel_error_message_element.when_present.element.text}"
    expect(page.channel_error_message).to eq "Auto Claim is required."
  end
end

Then(/^I get a validation message when I enter an invalid channel/) do
  on OA::EditYoutubeChannelPage do |page|
    page.mock_invalid_channel_response
    page.youtube_channel_address = "NonExistentChannel"
    puts page.validate_youtube_channel
    expect(page.channel_error_message).to eq("YouTube reports that channel does not exist.")
  end
end

Then(/^I enter a valid YouTube Channel/) do
  on OA::EditYoutubeChannelPage do |page|
    page.select_auto_claim_yes
    page.mock_valid_channel_response
    page.youtube_channel_address = "orcharddemotest"
    puts page.validate_youtube_channel
    expect(page.channel_verified_alert).to eq("Channel address Verified.")
  end
end

# TODO: remove randomness from this. tests should be as deterministic as possible.
Then(/^I fill out and submit the form to add a new channel/) do
  on OA::EditYoutubeChannelPage do |page|
    page.youtube_channel_current_upc = "888831261094"
    page.youtube_channel_artist_id = "671412"
    page.wait_for_artist_validation
    page.service_tier_dropdown = "Premium"
    page.select_random_account_manager
    page.select_initiated_date
    page.select_connected_date
    page.notes = "Woot \nWooot \nWoooot \nWooooot \nGian \ntotally \nrules"
    page.channel_country = "USA"
    page.primary_language = "English"
    page.genre_category = "Metal"
    # page.submit_button
  end
end

Then(/^I am able to change the current Account Manager$/) do
  on OA::EditYoutubeChannelPage do |page|
    puts "Prepopulated Account Manager field: #{page.account_manager_dropdown_element.selected_options[0]}"
    puts "Randomly selected Account Manager: #{page.select_random_account_manager}"
    @new_account_manager = page.account_manager_dropdown_element.selected_options[0]
    @channel_id = page.current_url.split("/").last
    page.submit_button
  end
  visit(OA::ViewYoutubeChannelPage, using_params: { channel_id: @channel_id }) do |page|
    refresh = proc do |exception, try|
      puts "#{exception.class}: '#{exception.message}' - Retries: #{try}"
      page.refresh
    end
    visit(OA::ViewYoutubeChannelPage,
          using_params: { channel_id: @channel_id }) unless @browser.url.include?("/viewchannel/")
    Retriable.retriable(on: RSpec::Expectations::ExpectationNotMetError, tries: 3, on_retry: refresh) do
      puts page.account_manager
      expect(page.account_manager).to include @new_account_manager
    end
  end
  on(OA::ManageYouTubeChannelsPage).close_youtube_channel_child_page
end

When(/^I add a YouTube channel$/) do
  on(OA::ManageYouTubeChannelsPage).add_channel_link
end

# TODO: remove randomness from this. tests should be as deterministic as possible.
Then(/^I can select an associated Account Manager$/) do
  on OA::EditYoutubeChannelPage do |page|
    puts "Default Account Manager field: #{page.account_manager_dropdown_element.selected_options[0]}"
    puts "Randomly selected Account Manager: #{page.select_random_account_manager}"
    puts "Account Manager set to: #{page.account_manager_dropdown_element.selected_options[0]}"
  end
end
