module Workstation
  class AnalyticsTopChartsPage < Workstation::BasePage
    page_url "/analytics/index"

    table(:top_artists_table) do
      div_element(css: "div#topChartArtist[style='display: block;']").table_element
    end
    table(:top_releases_table) do
      div_element(css: "div#topChartCatalog[style='display: block;']").table_element
    end
    table(:top_projects_table) do
      div_element(css: "div#topChartCatalog[style='display: block;']").table_element
    end
    table(:top_tracks_table) { div_element(css: "div#topChartIsrc[style='display: block;']").table_element }
    div(:top_artists_sources_helptip) { div_element(id: "topChartArtist").div_element(class: "helptip") }
    div(:top_releases_sources_helptip) { div_element(id: "topChartCatalog").div_element(class: "helptip") }
    div(:top_projects_sources_helptip) { div_element(id: "topChartCatalog").div_element(class: "helptip") }
    div(:top_tracks_sources_helptip) { div_element(id: "topChartIsrc").div_element(class: "helptip") }
    link(:active_top_charts, text: "Top Charts")
    button(:subscription_streams, text: "Subscription Streams")
    button(:ad_supported_streams, text: "Ad Supported Streams")
    button(:album_downloads, text: "Album Downloads")
    button(:track_downloads, text: "Track Downloads")

    link(:go_to_overview_button, text: "Overview")
    div(:top_artists_sources_popup) { div_element(id: "topChartArtist").div_element(class: "tooltip") }
    div(:top_releases_sources_popup) { div_element(id: "topChartCatalog").div_element(class: "tooltip") }
    div(:top_projects_sources_popup) { div_element(id: "topChartCatalog").div_element(class: "tooltip") }
    div(:top_tracks_sources_popup) { div_element(id: "topChartIsrc").div_element(class: "tooltip") }

    # Selects first artist from the top_artist table
    # Returns an Array of Strings containing the artist and corresponding downloads for that
    # artist in the table
    def click_first_artist
      cell_values = click_first_row_in :top_artists_table
      # Looks like: ["", "<artist_name>", "<downloads>"]
      # because first cell is image
      cell_values.drop(1)
    end

    # Same as #click_first_artist except for a release, and returns an Array with
    # release name, artist name, and downloads
    # Delete for project_manager teardown
    def click_first_release
      cell_values = click_first_row_in :top_releases_table
      # Looks like: ["", "<release_name>\n<artist_name>", "<downloads>"]
      cell_values[1].split("\n") << cell_values[2]
    end

    def click_first_project
      cell_values = click_first_row_in :top_projects_table
      # Looks like: ["", "<project_name>\n<artist_name>", "<downloads>"]
      cell_values[1].split("\n") << cell_values[2]
    end

    # Same as #click_first_artist except for a track, and returns an Array with
    # track name, artist name, and downloads
    def click_first_track
      cell_values = click_first_row_in :top_tracks_table
      # Looks like: ["", "<track_name>\n<artist_name>", "<downloads>"]
      cell_values[1].split("\n") << cell_values[2]
    end

    def select_channel(channel)
      button_element = send("#{channel}_element")
      Retriable.retriable(on: Watir::Wait::TimeoutError, tries: 3) do
        button_element.click
        wait_until(5) { button_element.class_name.include? "active" }
      end
    end

    def get_data_sources(sales_type, top_chart)
      top_chart_element_prefix = top_chart.to_snake_case
      table_element = send("#{top_chart_element_prefix}_table_element")
      table_element.first_row.element.wait_until(60, &:present?)
      send("#{top_chart_element_prefix}_sources_helptip_element").element.fixed_hover
      popup_text = send("#{top_chart_element_prefix}_sources_popup_element").when_present(5).text
      popup_text =~ /#{sales_type.capitalize}.*?\:\s?(.+)/
      fail "Did not find any #{sales_type.capitalize} data sources, with associated list of stores, "\
      "within text: #{popup_text}" unless Regexp.last_match[1]
      Regexp.last_match[1]
    end

    private

    def click_first_row_in(table_accessor)
      wait_until { send("#{table_accessor}_element").present? }
      wait_until(60) { send("#{table_accessor}_element").rows > 0 }
      first_row = send("#{table_accessor}_element").first
      cell_values = first_row.map(&:text)
      first_row.click
      cell_values
    end
  end
end
