module Project
  class DigitalProductBasicsPage < BasePage # rubocop:disable Metrics/ClassLength
    include DigitalProductClassicalBasics
    include ProductFooter

    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(:version, 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 }
    react_bubble_input(:primary_artists, id: "primary-artist")
    text_field(:select_artist, id: "react-select-2-input")
    react_bubble_input(:featuring_artists, id: "featuring")
    react_bubble_input(:remixer_artists, id: "remixer")
    react_select(:genre, id: "genre-select")
    react_select(:subgenre, id: "subgenre-select")
    react_select_or_create(:imprint) { label_element(text: "imprint*").parent }
    text_field(:c_line, id: "cline")
    text_field(:release_date, name: "releaseDate")
    text_field(:sales_date, name: "salesDate")
    react_select(:itunes_pricing) { span_element(text: "iTunes").parent.div_element }
    react_select(:amazon_pricing) { span_element(text: "Amazon").parent.div_element }
    react_button_row(:preorder, options: {
                       immediate: { id: "preorder-immediate-button" },
                       none: { id: "preorder-none-button" },
                       specific_date: { id: "preorder-specific-button" }
                     }) do
      div_element(class: "PreorderField").div_element(class: "ButtonsRow")
    end
    button(:immediate_preorder, id: "preorder-immediate-button")
    button(:no_preorder, id: "preorder-none-button")
    button(:specific_date_preorder, id: "preorder-specific-button")
    text_area(:special_instructions, id: "special-instructions")

    button(:submit, class: "DigitalProductFooter-button")

    def populate_required_fields(data)
      select_meta_language data[:meta_language]
      self.primary_artists = data[:primary_artists]
      populate_genre(data)
      self.c_line = data[:c_line]
      self.release_date = data[:release_date]
      self.sales_date = data[:sales_date]
    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.version = data[:version]
      self.featuring_artists = data[:featuring_artists]
      self.remixer_artists = data[:remixer_artists]
      select_itunes_pricing data[:itunes_pricing]
      select_amazon_pricing data[:amazon_pricing]
      select_preorder data[:preorder]
      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

    def release_basics_values(only_for:)
      values = {
        meta_language: meta_language,
        product_name: product_name,
        product_code: product_code,
        primary_artists: primary_artists,
        featuring_artists: featuring_artists,
        remixer_artists: remixer_artists,
        version: version
      }.merge(release_details_values).merge(release_pricing_values)
      values.merge!(classical_values) if composers?
      values.select! { |k, _v| only_for.include? k }
      values
    end

    def populate_primary_artist_dropdown(artist)
      throttled_input(select_artist_element, artist, 2, false)
      dropdown_element = div_element(id: /react-select-2-option/, text: artist)
      dropdown_element.when_present.click
    end

    private

    def release_details_values
      {
        genre: genre,
        subgenre: subgenre,
        format: format,
        imprint: imprint,
        c_line: c_line
      }
    end

    def release_pricing_values
      {
        release_date: release_date,
        sales_date: sales_date,
        itunes_pricing: itunes_pricing,
        amazon_pricing: amazon_pricing,
        preorder: preorder,
        special_instructions: special_instructions
      }
    end
  end
end
