When(/^I view (.*?) broken down by (.*?)$/) do |data_channel, filter|
  on(Workstation::AnalyticsOverviewPage) do |page|
    page.wait_for_any_graph_to_load
    page.go_to_breakdown_page(data_channel, filter)
    @filter_plural = if filter == "Country"
                       "Countries"
                     else
                       "#{filter.capitalize}s"
                     end
  end
end

Then(/^I should see selected breakdown highlighted$/) do
  on(Workstation::AnalyticsBreakdownPage) do |page|
    page.wait_for_breakdown_to_load
    highlighted_text = page.active_breakdown_element.text
    expect(highlighted_text.downcase).to eq(@filter_plural.downcase)
  end
end

When(/^I get the album column for the first page$/) do
  on(Workstation::AnalyticsBreakdownPage) do |page|
    page.breakdown_table_element.element.wait_until(&:present?)
    page.wait_until { page.breakdown_table_element.rows > 10 }
    @first_page_album = page.breakdown_title_elements[0..4].map(&:text)
    puts "array from first page: #{@first_page_album}"
  end
end

Then(/^the album column for the second page should not match it$/) do
  on(Workstation::AnalyticsBreakdownPage) do |page|
    page.next_link
    page.breakdown_loading_element.element.wait_while(&:present?)
    page.wait_until { page.breakdown_table_element.rows > 10 }
    second_page_album = page.breakdown_title_elements.find_all { |x| x.element.present? }[0..4].map(&:text)
    puts "array from second page: #{second_page_album}"
    expect(second_page_album).not_to eq(@first_page_album)
  end
end

When(/^I go back to the first page of the breakdown$/) do
  on(Workstation::AnalyticsBreakdownPage) do |page|
    page.previous_link
    page.breakdown_loading_element.element.wait_while(&:present?)
  end
end

Then(/^I should see the previous button is disabled and next button is enabled$/) do
  on(Workstation::AnalyticsBreakdownPage) do |page|
    page.previous_link_element.element.wait_until(&:present?)
    previous_class_name = page.previous_link_element.element.parent.attribute_value("class")
    next_class_name = page.next_link_element.element.parent.attribute_value("class")
    puts "class of previous link's li parent: #{previous_class_name}"
    puts "class of next link's li parent: #{next_class_name}"
    expect(previous_class_name).to include("disable")
    expect(next_class_name).not_to include("disable")
  end
end

Then(/^I should see the original album column$/) do
  on(Workstation::AnalyticsBreakdownPage) do |page|
    current_page_albums = page.breakdown_title_elements[0..4].map(&:text)
    expect(current_page_albums).to eq(@first_page_album)
    puts "array from current page: #{current_page_albums}"
  end
end

When(/^I get the total number of units sold for the time period$/) do
  on(Workstation::AnalyticsBreakdownPage) do |page|
    page.wait_for_breakdown_to_load
    grand_total = page.total_units_for_period_elements[2].text
    @total_units_for_period = grand_total.delete(",").to_i
  end
end

When(/^I select a (.*?) period$/) do |period_of_time|
  on(Workstation::AnalyticsOverviewPage) do |page|
    page.div_element(id: "date-picker").button_element(value: period_of_time).click
  end
end

Then(/^the current period's total units should not equal the previous period's$/) do
  fail "Must have an initial period to compare against!" unless @total_units_for_period
  on(Workstation::AnalyticsBreakdownPage) do |page|
    page.wait_for_breakdown_to_load
    grand_total = page.total_units_for_period_elements[2].text
    current = grand_total.delete(",").to_i
    puts "previous: #{@total_units_for_period}"
    puts "current: #{current}"
    expect(current).not_to eq(@total_units_for_period)
  end
end

Then(/^the total units should add up for both products with data channel$/) do
  on(Workstation::AnalyticsBreakdownPage) do |page|
    page.wait_for_breakdown_to_load
    barchart_units = page.barchart_activities_elements
    expect(barchart_units.count).to eql(2), "only one product listed!"
    product_1_unit = barchart_units[0].text.to_i
    product_2_unit = barchart_units[1].text.to_i
    puts "breakdown total for the first products: #{product_1_unit}"
    puts "breakdown total for the second products: #{product_2_unit}"
    products_total = product_1_unit + product_2_unit
    puts "breakdown total for the 2 products: #{products_total}"
    grand_total = page.total_units_for_period_elements[2].text
    expect(grand_total.to_i).to eq(products_total)
  end
end

Then(/^the total units should add up for both projects with data channel$/) do
  on(Workstation::AnalyticsBreakdownPage) do |page|
    page.wait_for_breakdown_to_load
    barchart_units = page.barchart_activities_elements
    expect(barchart_units.count).to eql(2), "only one project listed!"
    project_1_unit = barchart_units[0].text.to_i
    project_2_unit = barchart_units[1].text.to_i
    puts "breakdown total for the first projects: #{project_1_unit}"
    puts "breakdown total for the second projects: #{project_2_unit}"
    projects_total = project_1_unit + project_2_unit
    puts "breakdown total for the 2 projects: #{projects_total}"
    expect(page.total_units_for_period.to_i).to eq(projects_total)
  end
end

When(/^I select a single (project|release|product)$/) do |_|
  on(Workstation::AnalyticsBreakdownPage) do |page|
    page.wait_for_breakdown_to_load
    page.display_data_for_first_row_only
  end
end
