Then(/^I see that the audience page works$/) do
  on(ReactWorkstation::ReactAudiencePage) do
    step "in the new Audience page I see all the components in place"
  end
end

#
# Action Step Definitions for Audience Page
#
When(/in the new Audience page I select the (.*?) time filter$/) do |filter|
  on(ReactWorkstation::ReactAudiencePage) do |page|
    page.wait_until { page.graph_loading_indicator_elements.none? }
    page.date_filter
    case filter
    when "year"
      page.year_toggle_element.click
    when "month"
      page.month_toggle_element.click
    when "week"
      page.week_toggle_element.click
    end
  end
end

And(/^in the new Audience page I get the listeners data$/) do
  on(ReactWorkstation::ReactAudiencePage) do |page|
    page.wait_until { page.graph_loading_indicator_elements.none? }
    @start_value = page.streams_value_element.text
    @multiplier = Support::Helpers::Multiplier.parse(@start_value)
    @start_value = @start_value[0..-1] if @start_value.include?(".")
    @start_value = @start_value.to_f * @multiplier.to_f
    puts "Start value equals to : #{@start_value}"
  end
end

#
# Validation Step Definitions for Audience Page
#
Then(/in the new Audience page I should see the filters$/) do
  on(ReactWorkstation::ReactAudiencePage) do |page|
    page.country_toggle_element.wait_until(&:present?)
    page.wait_until { page.graph_loading_indicator_elements.none? }
    expect(page.all_stores_toggle_element.exists?).to be true
    expect(page.apple_music_toggle_element.exists?).to be true
    expect(page.spotify_toggle_element.exists?).to be true
    expect(page.country_toggle_element.exists?).to be true
    expect(page.region_toggle_element.exists?).to be true
    expect(page.date_filter_element.exists?).to be true
  end
end

Then(/in the new Audience page I should see a search bar$/) do
  on(ReactWorkstation::ReactAudiencePage) do |page|
    page.search_input_element.wait_until(&:present?)
    expect(page.search_input_element.exists?).to be true
    expect(page.search_div_element.exists?).to be true
  end
end

Then(/^in the new Audience page I should see the top panels/) do
  on(ReactWorkstation::ReactAudiencePage) do |page|
    page.top_metrics_element.wait_until(&:present?)
    expect(page.streams_element.exists?).to be true
    expect(page.top_audience_element.exists?).to be true
  end
end

Then(/^in the new Audience page I should see a map$/) do
  on(ReactWorkstation::ReactAudiencePage) do |page|
    page.map_container_element.wait_until(&:present?)
    expect(page.map_container_element.exists?).to be true
    page.filled_country_element.hover
    page.filled_country_element.hover
    expect(page.map_tooltip_element.present?).to be true
  end
end

Then(/^in the new Audience page I should see the top countries table$/) do
  on(ReactWorkstation::ReactAudiencePage) do |page|
    page.top_countries_element.wait_until(&:present?)
    expect(page.top_countries_element.exists?).to be true
  end
end

Then(/^in the new Audience page I should see the top regions table$/) do
  on(ReactWorkstation::ReactAudiencePage) do |page|
    page.top_regions_element.wait_until(&:present?)
    expect(page.top_regions_element.exists?).to be true
  end
end

Then(/^in the new Audience page I should see gender donut chart$/) do
  on(ReactWorkstation::ReactAudiencePage) do |page|
    page.donut_chart_element.wait_until(&:present?)
    expect(page.donut_chart_element.exists?).to be true
    eventually(timeout: 30) do
      expect(page.donut_chart).to include("By Gender")
    end
  end
end

Then(/^in the new Audience page I should see an age bar chart$/) do
  on(ReactWorkstation::ReactAudiencePage) do |page|
    page.age_breakdown_all_element.wait_until(&:present?)
    expect(page.age_breakdown_all_element.exists?).to be true
  end
end

Then(/^in the new Audience page I should see the top audiences table$/) do
  on(ReactWorkstation::ReactAudiencePage) do |page|
    page.top_audiences_element.wait_until(&:present?)
    expect(page.top_audiences).to include "AUDIENCE", "STREAMS"
    expect(page.top_audiences_table).to include "Women ages", "Men ages"
  end
end

Then(/^in the new Audience page I should see the table tooltip$/) do
  on(ReactWorkstation::ReactAudiencePage) do |page|
    page.table_tooltip_icon_element.hover
    page.table_tooltip_element.wait_until(&:present?)
    expect(page.table_tooltip_element.present?).to be true
  end
end

Then(/^in the new Audience page I see all the components in place$/) do
  on(ReactWorkstation::ReactAudiencePage) do
    step "in the new Audience page I should see the filters"
    step "in the new Audience page I should see a search bar"
    step "in the new Audience page I should see the top panels"
    step "in the new Audience page I should see a map"
    step "in the new Audience page I should see the top countries table"
    step "in the new Audience page I should see the top regions table"
    step "in the new Audience page I should see gender donut chart"
    step "in the new Audience page I should see an age bar chart"
    step "in the new Audience page I should see the top audiences table"
  end
end

Then(/^listeners in the new audience page are (increased|decreased|the same)$/) do |condition|
  on(ReactWorkstation::ReactAudiencePage) do |page|
    page.wait_until { page.graph_loading_indicator_elements.none? }
    @end_value = page.streams_value_element.text
    @multiplier = Support::Helpers::Multiplier.parse(@end_value)
    @end_value = @end_value[0..-1] if @end_value.include?(".")
    @end_value = @end_value.to_f * @multiplier.to_f
    puts "End value equals to : #{@end_value}"

    case condition
    when "increased"
      expect(@start_value).to be < @end_value
    when "decreased"
      expect(@start_value).to be > @end_value
    when "the same"
      expect(@start_value).to be == @end_value
    else
      raise "There is no #{condition} comparison"
    end
  end
end

When(/^I toggle the new audience page search for (track|artist)$/) do |search_type|
  on(ReactWorkstation::ReactAudiencePage) do |page|
    page.wait_until { page.graph_loading_indicator_elements.none? }
    return if page.artist_track_search_toggle_element.text.split("\n").first.downcase == search_type
    page.artist_track_search_toggle_element.click
    if search_type == "track"
      page.tracks_search_toggle_element.click
    elsif search_type == "artist"
      page.artist_search_toggle_element.click
    end
  end
end

When(/^I search the new audience page for the (.*?) (track|artist)$/) do |search_term, search_type|
  on(ReactWorkstation::ReactAudiencePage) do |page|
    page.wait_until_loaded
    step("I toggle the new audience page search for #{search_type}")
    page.throttled_input(page.search_input_element, search_term)
    page.search_input_element.send_keys(:enter)
  end
end

When(/^I filter the new audience page for the (.*?) (country|region)$/) do |search_term, search_type|
  on(ReactWorkstation::ReactAudiencePage) do |page|
    page.wait_until { page.graph_loading_indicator_elements.none? }

    page.send("#{search_type}_toggle_element").click
    search_box = page.location_input_elements.find { |x| x.present? }
    search_box.send_keys(search_term)
    page.link_element(text: search_term).click
  end
end

When(/^I remove a search term from the new Audience page/) do
  on(ReactWorkstation::ReactAudiencePage).search_input_element.send_keys(:backspace)
end

Then(/^I should see (.*?) in the new audience page search field$/) do |query|
  expect(on(ReactWorkstation::ReactAudiencePage).search_container).to include query
end

Then(/^I should not see (.*?) in the new audience page search field$/) do |query|
  expect(on(ReactWorkstation::ReactAudiencePage).search_container).not_to include query
end

Then(/^in the new Audience page I should see a country level map of (.*?)$/) do |country|
  on(ReactWorkstation::ReactAudiencePage) do |page|
    page.wait_until { page.graph_loading_indicator_elements.none? }
    expect(page.map_element.present?).to be true
    expect(page.breadcrumbs_element.link_element.present?).to be true
    expect(page.breadcrumbs_element.span_element.text).to include country.upcase
    expect(page.city_label_elements.count).to be > 0
  end
end

When(/^in the new Audience page I click on worldwide link on breadcrumb$/) do
  on(ReactWorkstation::ReactAudiencePage) do |page|
    page.wait_until { page.graph_loading_indicator_elements.none? }
    page.breadcrumbs_element.link_element.click
  end
end

Then(/^in the new Audience page I should see a worldwide level map$/) do
  on(ReactWorkstation::ReactAudiencePage) do |page|
    page.wait_until { page.graph_loading_indicator_elements.none? }
    expect(page.map_element.present?).to be true
    expect(page.breadcrumbs_element.link_element.present?).to be false
    expect(page.city_label_elements.count.zero?).to be true
  end
end

When(/^in the new Audience page I select Brazil from top countries table$/) do
  on(ReactWorkstation::ReactAudiencePage) do |page|
    page.wait_until { page.graph_loading_indicator_elements.none? }
    page.top_countries_element.tr.click
  end
end

When(/^in the new Audience page I click on Brazil on the map$/) do
  on(ReactWorkstation::ReactAudiencePage) do |page|
    page.wait_until { page.graph_loading_indicator_elements.none? }
    page.filled_country_element.click
  end
end

Then(/^in the new Audience page search bar text should be "(.*?)"$/) do |expected_text|
  on(ReactWorkstation::ReactAudiencePage) do |page|
    expect(page.search_placeholder_text_element.text).to include(expected_text)
  end
end

When(/I filter the new audience page for (Spotify|Apple Music|all stores)$/) do |stores|
  on(ReactWorkstation::ReactAudiencePage) do |page|
    case stores
    when "Spotify"
      page.spotify_toggle_element.parent.click
    when "Apple Music"
      page.apple_music_toggle_element.parent.click
    when "all stores"
      page.all_stores_toggle_element.parent.click
    end
    page.wait_until { page.graph_loading_indicator_elements.none? }
  end
end
