module Workstation
  module DataChannel
    include PageObject
    div(:graphs_container, id: "graphs_list")
    divs(:channel_graph) { div_elements(class: "graph") }
    div(:new_graphs_loading_modal, class: "LoadingIndicator-outer")
    div(:loading_section, id: "loading_sections")
    divs(:analytics_loader, class: "analytics-loader")
    div(:first_drag_handle) { graphs_container_element.div_element(class: "drag-handle") }
    div(:second_drag_handle) { graphs_container_element.div_element(class: "drag-handle", index: 1) }
    div(:download_albums_container, id: "graph_23")
    links(:unhide_channel_link) { hidden_channels_container_element.link_elements(class: "display-channel") }
    unordered_list(:hidden_channels_container, id: "hidden-channels")
    div(:youtube_audio_and_video_streams, class: "youtube-audio-and-video-streams")
    div(:youtube_audio_and_video_streams_section, class: "graphs-list-youtube-audio-and-video-streams")
    divs(:youtube_audio_and_video_streams_graphs) do
      youtube_audio_and_video_streams_section_element.div_elements(class: "graph")
    end
    div(:audio_streams, class: "audio-streams")
    div(:audio_streams_section, class: "graphs-list-audio-streams")
    divs(:audio_streams_graphs) do
      audio_streams_section_element.div_elements(class: "graph")
    end

    # rubocop:disable Metrics/AbcSize
    def find_graph_by_name(name)
      page_refresh = proc do |exception, try|
        Kernel.puts "#{exception.class}: '#{exception.message}' - Attempt: #{try}"
        refresh
        wait_for_any_graph_to_load
      end
      Retriable.retriable(on: Watir::Wait::TimeoutError, tries: 3, on_retry: page_refresh) do
        wait_until(10) do
          channel_graph_elements.find_all { |x| x.present? }.any? { |channel| channel.text.include?(name) }
        end
      end
      channel_graph_elements.find { |channel| !channel.text.empty? && channel.h4_element.text == name }
    end
    # rubocop:enable Metrics/AbcSize

    def get_channel_total(name)
      total_text = find_graph_by_name(name).h3_element(class: "grand-total").text
      total_text.delete(",").to_i
    end

    def wait_for_any_graph_to_load
      # Increased on 3/22/2018 while systems does stuff for their 3/30 deadline
      loader_wait(element: new_graphs_loading_modal_element, initial_wait: 1, timeout: 300)
      # TODO: Find a way to reduce this wait
      # Increased on 3/22/2018 while systems does stuff for their 3/30 deadline
      wait_until(300) { !channel_graph_elements.empty? }
    end

    def point_in_analytics_graph(which_graph, chart_point)
      selected_graph_element = find_graph_by_name(which_graph)
      wait_until { selected_graph_element.element.elements(class: "dot").size > chart_point }
      # There is a delay in the graph transition between two different date
      # ranges. Waiting for visibility doesn't work.
      sleep 2
      selected_graph_element.element.element(class: "dot", index: chart_point).fixed_hover
      date = selected_graph_element.h4_element(class: "active-date").text
      sales = selected_graph_element.element.element(class: "graph-point-sales").text.delete ","
      { date: date, sales: sales }
    end

    def hide_channel(name)
      find_graph_by_name(name).link_element(class: "hide-channel").click
    end

    def unhide_channel(name)
      Retriable.retriable(on: RSpec::Expectations::ExpectationNotMetError, tries: 3, base_interval: 5) do
        channels = hidden_channels_container_element.list_item_elements.find_all do |el|
          el.text.include? name
        end
        expect(channels.empty?).to be false
        fail "more than one hidden channel matching #{name}!" if channels.size > 1
        channels.first.link_element(class: "display-channel").click
      end
    end

    def youtube_audio_and_video_streams_channel_list
      [
        "Total YouTube Audio Streams",
        "Total YouTube Video Streams",
        "Ad-Enabled Audio Streams",
        "Ad-Enabled Video Streams",
        "Ad-Disabled Audio Streams",
        "Ad-Disabled Video Streams"
      ]
    end

    def audio_streams_channel_list
      [
        "Mid-Tier Subscription Audio Streams",
        "Subscription Audio Streams",
        "Total Audio Streams",
        "Ad-Supported Audio Streams"
      ]
    end

    def channel_list(section_name)
      send("#{section_name}_channel_list")
    end
  end
end
