module Workstation
  class AnalyticsOverviewPage < Workstation::BasePage
    # TODO: remove unused elements in this class
    include Analytics::MarketableEvents
    include Workstation::DataChannel
    include PageHelpers::Drag

    page_url "/analytics/overview"

    link(:overview_header, text: "Overview")

    # VIEW BY section
    div(:view_by_container, id: "btn_wrap")
    button(:active_view_by_button) { view_by_container_element.button_element(class: "active") }
    button(:all_button) { view_by_container_element.button_element(text: "All") }
    button(:imprints_button) { view_by_container_element.button_element(text: "Imprints") }
    button(:artists_button) { view_by_container_element.button_element(text: "Artists") }
    button(:tracks_button) { view_by_container_element.button_element(text: "Tracks") }
    button(:genres_button) { view_by_container_element.button_element(text: "Genres") }
    button(:products_button) { view_by_container_element.button_element(text: "Products") }
    button(:projects_button) { view_by_container_element.button_element(text: "Projects") }

    # VIEW BY cards
    h4(:applied_vendor) { div_element(id: "vendor_wrap").h4_element }
    h4s(:applied_imprint) { div_element(id: "imprint_ids_wrap").h4_elements }
    h4s(:applied_artist) { div_element(id: "artist_ids_wrap").h4_elements }
    h4s(:applied_product) { div_element(id: "product_ids_wrap").h4_elements }
    h4s(:applied_project) { div_element(id: "catalog_ids_wrap").h4_elements }
    h4s(:applied_track) { div_element(id: "isrc_ids_wrap").h4_elements }
    h4s(:applied_genre) { div_element(id: "genre_ids_wrap").h4_elements }

    # Card-level elements
    div(:current_card, class: %w[card open])
    button(:add_something) do
      button_elements(class: "dropdown__toggle").find { |current| current.element.present? }
    end
    text_field(:card_search_box) { current_card_element.text_field_element(class: "dropdown__typeahead") }
    unordered_list(:result_container) do
      current_card_element.unordered_list_element(class: "dropdown__list")
    end
    list_items(:result) { result_container_element.list_item_elements }

    # DATE section
    text_field(:start_date, id: "from_date") # click :active_date_on_calendar after setting this
    text_field(:end_date, id: "to_date") # click :active_date_on_calendar after setting this
    button(:active_period) { div_element(id: "date-picker").button_element(class: "active") }
    # Need to click this to fire the right event:
    link(:active_date_on_calendar, class: "ui-state-active") # works for all date-pickers on the page

    # FILTER BY section
    div(:sources_filter, id: "source_type_ids_wrap")
    button(:sources_button) { sources_filter_element.button_element }
    list_items(:sources_list) { sources_filter_element.list_item_elements }
    div(:countries_filter, id: "country_ids_wrap")
    button(:regions_button) { countries_filter_element.button_element }
    divs(:regions_list, class: "dropdown_group_view")
    button(:apply, id: "update")

    # Button reset channel order to a default state
    button(:reset_channels, id: "reset")
    button(:confirm_reset) { restore_modal_element.link_element(text: "Yes") }
    div(:restore_modal, id: "cnfrm")

    def filter_by(filter_type)
      send "#{filter_type}_button"
    end

    def wait_for_results
      wait_until { result_elements.any? { |autofill_result| autofill_result.element.present? } }
    end

    def go_to_breakdown_page(channel_name, breakdown_type)
      channel = find_graph_by_name(channel_name)
      channel.button_element(class: "btn-view-details").click
      channel.unordered_list_element(class: "dropdown-menu").link_element(text: breakdown_type).click
    end

    def select_filters(filter_type, *selected_filters)
      selected_filters.each do |filter|
        filter_li = send("#{filter_type}_list_elements").find { |x| x.text.eql? filter.to_s }
        filter_li.label_element.click
      end
    end

    def unhide_all_channels
      wait_until { hidden_channels_container? }
      return unless unhide_channel_link_elements.any?
      ids = unhide_channel_link_elements.map(&:id)
      ids.each { |id| link_element(id: id).click }
      wait_for_ajax_scripts_to_finish
    end

    def drag_second_graph_to_third_position
      wait_until { channel_graph_elements.size >= 2 }
      drag_with_additional_movement(
        first_drag_handle_element,
        second_drag_handle_element,
        0,
        0.1 # 10% additional movement in the y-direction
      )
      wait_for_ajax_scripts_to_finish
    end

    private

    def all_present_graphs
      channel_graph_elements.find_all { |graph| graph.element.present? }
    end
  end
end
