When(/^I gather (subaccount|parent) data from each data channel detail page$/) do |label_type|
  data_channels = {}
  channel_names = [
    # subaccount and parent have different channels listed; may need to update
    # this from time to time
    "Download Tracks",
    "Subscription Audio Streams",
    "Ad-Supported Audio Streams",
    "Ad-Enabled Audio Streams",
    "Ad-Disabled Audio Streams"
  ]

  # cant return variables from on blocks, so need to do this
  channel_names.each do |channel_name|
    on(Workstation::AnalyticsOverviewPage) do |page|
      page.wait_for_any_graph_to_load
      page.wait_until { page.h4_elements.any? { |channel_header| channel_header.text == channel_name } }
      channel = page.h4_elements.find { |channel_header| channel_header.text == channel_name }.parent
      count = channel.h3_element.text.delete(",").to_i
      data_channels[channel_name] = count
    end
  end
  puts "data channels for #{label_type}: #{data_channels.inspect}"

  @label_data ||= {}
  @label_data[label_type.to_sym] = data_channels
end

Then(/^The subaccount and parent data should not match$/) do
  maximum_ratio = 0.7 # if 0.7, then 100 counts in parent data means at most 70 counts in subaccount

  subaccount_data = @label_data[:subaccount]
  subaccount_data.each_pair do |data_channel, actual_subaccount_channel|
    maximum_expected_subaccount_channel = @label_data[:parent][data_channel] * maximum_ratio
    expect(actual_subaccount_channel).to(
      be < maximum_expected_subaccount_channel,
      "#{actual_subaccount_channel} is more than #{maximum_ratio} of"\
      " #{maximum_expected_subaccount_channel}! Ensure that subaccount"\
      " analytics does not include data for other subaccounts, and increase"\
      " maximum_ratio if need be."
    )
  end
end

Then(/^I should not see the analytics nav menu$/) do
  on(ReactWorkstation::ReactHomePage) do |page|
    expect(page.top_nav_analytics_link?).to be false
  end
end

Then(/^I should not see the top ten tracks component$/) do
  on(ReactWorkstation::ReactHomePage) do |page|
    expect(page.top_ten_title_element.present?).to be false
  end
end
