module Workstation
  class HomePage < Workstation::BasePage
    include Workstation::CreateRelease

    page_url("/")

    # flagged by workstation_index_redesign
    button(:create_project, id: "create-new-project")

    # Create A New Release section
    div(:create_dashboard, id: "create-dash")

    button(:create_dropdown, id: "newReleaseButtons")
    link(:create_audio, id: "newAudioRelease")
    link(:create_music_video, text: "Music Video") # TODO: add an ID to this element
    link(:create_film, text: "Film") # TODO: add an ID to this element
    div(:user_header) { div_element(class: %w[dropdown account]) }
    button(:user_drop_down) { user_header_element.button_element(class: "dropdown-toggle") }
    link(:admin_link) { user_header_element.link_element(href: "/account/admin") }
    link(:account_link) { user_header_element.link_element(href: "/account/myprofile") }

    def create_release_of_type(type)
      create_dropdown
      case type
      when "audio"
        create_audio
      when "music video"
        create_music_video
      when "film"
        create_film
      else
        fail "#{type} is not an option! must use audio, music video, film"
      end
    end

    link(:bulk_upload_link) { span_element(id: "bulk_upload_link").link_element }

    # Accounting Snapshot section
    links(:accounting_period_link, class: "RecentRevenueList-link")
    headers(:accounting_period_name, class: "RecentRevenueList-interval")
    text_field(:global_search_releases_input_box) do
      div_element(class: "GlobalSearchResults").text_field_element
    end
    div(:global_search_loader, class: "SearchResultsLoadingIndicator")
    divs(:search_results, class: "GlobalSearchResults-option")
    svg(:global_search_releases_button, class: "Icons-search")
    h3s(:section_header, class: "container_header")
    h3(:releases_in_progress_section, class: "container_header", text: "Releases In Progress")
    h3(:releases_in_catalog_section, class: "container_header", text: "Releases In Catalog")
    div(:video_top_ten_toggle, id: "top10Toggles-FilmTV")
    div(:loading_dots, class: "LoadingIndicator-dots")

    # There are 4 icon on this page with this locator
    div(:release_status, class: "release-status")
    link(:marketing_icon) { release_status_element.image_element(title: "Market this release") }
    link(:delivery_icon) { release_status_element.image_element(title: "View delivery report") }
    link(:analyze_sales_icon) { release_status_element.image_element(title: "Analyze sales activity") }
    link(:accounting_icon) { release_status_element.image_element(title: "View accounting data ") }

    table(:release_in_catalog_table, id: "in-catalog")
    link(:delivered_release_link) { release_in_catalog_table_element.link_element(class: "albumName") }

    # Top 10 performer for Artists, Releases & Tracks
    div(:top_ten_item_container, id: "modSmallTop4")
    link(:top_ten_artists) { top_ten_item_container_element.link_element(id: "top_ten_artists") }
    link(:top_ten_products) { top_ten_item_container_element.link_element(id: "top_ten_releases") }
    link(:top_ten_tracks) { top_ten_item_container_element.link_element(id: "top_ten_tracks") }
    spans(:top_ten_item, class: "artistName")
    section(:top_ten_loader, class: "top-ten-loader")

    def go_to_admin_page
      user_drop_down
      admin_link
    end

    def select_top_ten(selection)
      send "top_ten_#{selection}s"
    end

    def verify_home_page_section_contents(title1, title2, title3)
      expect(create_dashboard).to include title1
      expect(releases_in_progress_section).to include title2
      expect(releases_in_catalog_section).to include title3
    end

    # make sure the modal isn't opening and that we're on the right page
    def on_fresh_page?
      (page_url_value == current_url) && !create_release_modal?
    end
  end
end
