module Project
  class DigitalProductBasics2Page < BasePage
    include DigitalProductClassicalBasics

    page_url "/project/<%=params[:project_id]%>/product/<%=params[:product_id]%>/basics"

    div(:error, id: "product-error")
    div(:missing_information_alert, class: "DismissibleAlert-danger")
    divs(:field_error, class: "FormField-group-has-error")

    text_field(:product_name, id: "product-name")
    text_field(:product_code, id: "product-code")
    text_field(:product_version, id: "product-version")
    text_field(:version_notes, id: "version")
    text_field(:upc, id: "upc")
    react_select(:format, id: "format-select")
    react_select(:meta_language) { label_element(text: "Metadata Language*").parent.div_element }
    div(:primary_artists_container, id: "primary-artist")
    div(:featured_artists_container, id: "featuring")
    div(:create_new_artist, class: "AsyncDropdown__option-new")
    div(:add_new_artist_modal, css: ".ArtistProfilesList:not(.ArtistProfilesList-hidden")
    div(:artist_not_on_platform) do
      add_new_artist_modal_element.div_element(class: "ArtistProfileCheckable-item-check")
    end
    button(:add_new_artist_next, class: "ArtistProfilesPagination-next")
    button(:add_new_artist_done, class: "ArtistProfilesPagination-done")
    span(:apple_music, class: "AddNewArtist-content-tab-title-store-name", text: "Apple Music")
    button(:multiple_solo_artists_confirm, class: %w[TagsInputWithConfirm-confirm-button btn-primary])
    react_bubble_input(:remixer_artists, id: "remixer")
    react_select(:genre, id: "genre-select")
    react_select(:subgenre, id: "subgenre-select")
    react_select_or_create(:imprint) { div_element(id: "imprint") }
    text_field(:c_line, id: "cline")
    text_area(:special_instructions, id: "special-instructions")
    div(:success_alert, class: "DismissibleAlert-success")
    div(:warning_alert, class: "DismissibleAlert-warning")

    button(:submit, class: "DigitalProductFooter-button")
    button(:discard_changes, class: "DismissibleAlert-warning-button")

    def populate_required_fields(data)
      populate_meta_language data[:meta_language]
      populate_primary_artist(data[:primary_artists])
      populate_genre(data)
      self.c_line = data[:c_line]
    end

    def populate_meta_language(meta_language)
      meta_language_element.click
      div_element(class: "Select-option", text: meta_language).when_present.click
    end

    def populate_primary_artist(artist)
      populate_artist_field(artist, primary_artists_container_element)
    end

    def populate_featured_artist(artist)
      populate_artist_field(artist, featured_artists_container_element)
    end

    def populate_artist_field(artists, container_element)
      text_field = container_element.text_field_element
      text_field.send_keys :backspace
      artists.each do |artist|
        text_field.send_keys artist
        create_new_artist_element.click
        artist_not_on_platform_element.click
        add_new_artist_next
        wait_until { apple_music_element.present? }
        artist_not_on_platform_element.click
        add_new_artist_done
        wait_until { text_field.value == "" }
      end
    end

    def populate_genre(data)
      select_genre data[:genre]
      select_subgenre data[:subgenre]
    end

    def populate_optional_fields(data)
      self.product_code = data[:product_code]
      self.product_version = data[:product_version]
      self.version_notes = data[:version_notes]
      populate_featured_artist(data[:featuring_artists])
      self.remixer_artists = data[:remixer_artists]
      self.special_instructions = data[:special_instructions]
    end

    def select_existing_dropdown_options(data)
      select_imprint data[:imprint]
    end

    def create_new_dropdown_options(data)
      create_imprint data[:imprint]
    end
  end
end
