module Project
  class DigitalProductPricingPage < DigitalProductBasePage
    page_url "/project/<%=params[:project_id]%>/product/<%=params[:product_id]%>/pricing"

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

    text_field(:release_date, name: "releaseDate")
    text_field(:sales_date, name: "salesDate")
    text_field(:preorder_date, name: "preorderDate")

    # pre-order options
    button(:immediate_preorder, id: "preorder-immediate-button")
    button(:no_preorder, id: "preorder-none-button")
    button(:specific_date_preorder, id: "preorder-specific-button")
    radio_button_group(:previewable, name: "previewable")
    div(:preorder_buttons_disabled_text, class: "PreorderField-disabled-text")

    react_select(:album_pricing, id: "album_pricing")
    react_select(:track_pricing, id: "track_pricing")

    # individual track options
    button(:add_individual_track_price, class: "TrackIndividualAddButton")
    react_select(:individual_track, class: "is-searchable", index: 2)
    text_field(:individual_track_pricing, class: "Select-control", index: 3)

    # Instant Grats elements
    section(:ig_section, class: "InstantGratsGroup")
    div(:instant_grats_form, class: "InstantGratsForm")
    button(:add_instant_grats_btn, text: "+ Add Instant Grat")
    button(:grats_create_single, id: "instant-grats-create-single-button")
    labels(:ig_header) { instant_grats_form_element.label_elements(class: "FormField-label") }
    button(:delete_instant_grat, id: "instant-grats-delete-button")
    button(:confirm_delete, id: "confirm-submit")
    react_select(:ig_track_dropdown) do
      instant_grats_form_element.div_element(class: "Select")
    end
    react_select(:ig_track_dropdown_2) do
      instant_grats_form_element.div_element(class: "Select", index: 1)
    end
    text_field(:start_date, name: "startDate")
    text_field(:start_date_2, name: "startDate", index: 1)
    spans(:error_message, class: "FormField-errorMessage")

    def check_ig_present
      ig_header_elements.each do |header|
        fail "Couldn't find #{header.text}!" unless ["TRACK", "START DATE"].include?(header.text)
      end
      fail "Couldn't find delete_ig_button!" unless delete_instant_grat_element.element.present?
    end

    def delete_instant_grat
      return unless delete_instant_grat_element.element.present?
      delete_instant_grat_element.element.present?
      delete_instant_grat_element.click
      confirm_delete_element.when_present.click
      confirm_delete_element.when_not_present
    end

    def add_instant_grat(track_name = "abc_1", start_date = DateTime.now.strftime("%F"))
      add_instant_grats_btn
      select_ig_track_dropdown(track_name)
      self.start_date = start_date
      start_date_element.append "\n"
    end

    def populate_individual_track(track)
      add_individual_track_price
      select_individual_track track
    end

    def populate_required_fields(pricing_data)
      # patch to clear out date field and type
      release_date_element.clear_text_and_type pricing_data[:release_date]
      sales_date_element.clear_text_and_type pricing_data[:sales_date]
      # maybe switch back to these when chromedriver is fixed
      # self.release_date = pricing_data[:release_date]
      # self.sales_date = pricing_data[:sales_date]
      # need to press enter to close the datepicker, otherwise it blocks the
      # next form element
      sales_date_element.append "\n"
      select_preorder pricing_data[:preorder], pricing_data[:preorder_date], pricing_data[:previewable]
      select_album_pricing pricing_data[:album_pricing]
      select_track_pricing pricing_data[:track_pricing]
    end

    private

    def select_preorder(preorder_option, preorder_date, previewable)
      previewable = previewable.downcase
      case preorder_option
      when "Immediate"
        immediate_preorder
        select_previewable previewable
      when "No Pre-order"
        no_preorder
      when "Specific Date"
        specific_date_preorder
        # patch to clear out date field and type
        preorder_date_element.clear_text_and_type preorder_date
        # maybe switch back to these when chromedriver is fixed
        # self.preorder_date = preorder_date
        preorder_date_element.append "\n"
        select_previewable previewable
      else
        fail "Unknown preorder option: '#{preorder_option}'!"
      end
    end
  end
end
