module Project
  class CreatePhysicalProductPage < BasePage
    include ProductSidebar

    page_url "/project/<%=params[:project_id]%>/product/create/physical"

    text_field(:product_name, id: "product-name")
    text_field(:product_code, id: "product-code")
    react_select(:genre, id: "genre-select")
    react_select(:subgenre, id: "subgenre-select")

    radio_button_group(:upc_assigned_by, class: "UpcField-radio")
    radio_button(:orchard_generated_upc, name: "upc-assignment", value: "ORCHARD")
    radio_button(:use_own_upc, name: "upc-assignment", value: "OWN")
    text_field(:upc_value, name: "upc")

    react_select(:configuration_product_type) { label_element(text: "Product Type*").parent.div_element }
    # TODO: if we upgrade cheezy, we can use following_sibling, even better would be use ID here
    react_select(:orchard_pricing) { h3_element(text: "Pricing").parent.div_element(class: "Select") }
    react_select(:format) { label_element(text: "FORMAT*").parent.div_element }
    text_field(:units_per_set, id: "units-per-set")

    text_field(:version, id: "version")
    react_select(:packaging, id: "packaging-select")
    text_field(:orchard_artist, id: "artist")
    text_field(:primary_artist, id: "primary-artist")
    radio_button_group(:artist_is_individual, name: "artist-is-individual")
    react_select(:imprint, id: "imprint-select")
    react_select(:product_type, id: "product-type-select")
    text_field(:sales_date, name: "salesDate")
    text_field(:release_date, name: "releaseDate")
    text_field(:suggested_retail_price, id: "suggested-retail-price")
    text_field(:exclusive_for, id: "exclusive-for")
    text_field(:initial_stock, id: "initial-stock")
    text_field(:box_lot, id: "box-lot")
    button(:save, text: "SAVE AND CONTINUE")
    div(:error, class: ["frc-alert-inner"])
    paragraph(:success, class: ["alert", "alert-success"])
    text_field(:wholesale_price, id: "wholesale-price")
    text_field(:cline, id: "cline")
    text_field(:pline, id: "pline")
    textarea(:discount_policy, id: "discount-policy")
    radio_button_group(:parental_advisory, name: "explicit")
    div(:us_pricing_code) do
      div_element(class: "PhysicalProductForm-country-price-code", index: 0).div_element(index: 1)
    end
    react_select(:country_of_origin, id: "country-of-origin")

    def populate_physical_product_fields(data, supply_upc = true)
      populate_basics(data, supply_upc)
      populate_release_details(data)
      populate_configuration(data)
      populate_packaging(data)
      populate_pricing(data)
      populate_rights(data)
      populate_notes(data)
    end

    private

    def populate_product_basics(data, supply_upc)
      self.product_name = data[:product_name]
      self.product_code = data[:product_code]
      if supply_upc
        self.upc_value = data[:upc]
      else
        scroll_to :bottom
        select_orchard_generated_upc
      end
      select_genre data[:genre]
      select_subgenre data[:subgenre]
    end

    def populate_basics(data, supply_upc)
      populate_product_basics(data, supply_upc)
      self.primary_artist = data[:primary_artist]
      select_artist_is_individual data[:artist_is_individual]
      select_imprint data[:imprint]
      self.version = data[:version]
      select_parental_advisory data[:parental_advisory]
    end

    def populate_configuration(data)
      select_configuration_product_type data[:configuration_product_type]
      select_format data[:format]
      self.units_per_set = data[:units_per_set]
    end

    def populate_release_details(data)
      select_product_type data[:product_type]
      self.sales_date = data[:sales_date]
      self.release_date = data[:release_date]
    end

    def populate_packaging(data)
      select_packaging data[:packaging]
      self.initial_stock = data[:initial_stock]
      self.box_lot = data[:box_lot]
    end

    def populate_pricing(data)
      select_orchard_pricing data[:orchard_pricing]
      self.discount_policy = data[:discount_policy]
    end

    def populate_rights(data)
      self.pline = data[:pline]
      self.cline = data[:cline]
      self.exclusive_for = data[:exclusive_for]
    end

    def populate_notes(data)
      select_country_of_origin data[:country_of_origin]
    end
  end
end
