module Contract
  class ProductByTerritoryArrangement < UniversalPage
    include PageHelpers::ReactHelpers

    page_url "/product-territory-splits"

    div(:search_div, class: "Select-input")
    div(:product_by_territory_level_split, class: "ProductTerritoryLevelSplit")
    div(:product_by_territory_level_split_table, class: "ProductTerritoryLevelSplit-table")
    textarea(:search_input) { search_div_element.text_field_element }
    div(:success_message, class: %w[alert alert-success])
    button(:submit, text: "Submit")
    table(:clickable_table, class: %w[ClickableTable table table-striped])
    div(:country_dropdown, class: "product-territory-drop-down")
    text_field(:split) do
      div_element(class: "product-split-input").text_field(class: "form-control")
    end
    div(:search_results, class: "GlobalSearchResults-option")
    text_field(:split) do
      div_element(class: "product-territory-split-input").text_field(class: "form-control")
    end
    text_field(:country_textfield) { country_dropdown_element.text_field_element }
    div(:selected_country) do
      country_dropdown_element.div(class: %w[Select-option is-focused])
    end
    div(:loading_dots, class: "LoadingIndicator")
    div(:no_products_message, text: "No product by territory arrangements")
    spans(:delete_btn, class: "GlobalDeleteIcon")

    button(:confirm_delete) do
      div_element(class: "ReactModal-content").button(text: "Yes")
    end

    text_field(:split_with_validation_error) do
      div_element(class: "product-territory-split-input-error").text_field(class: "form-control")
    end

    div(:alert, class: %w[alert alert-danger])

    def add_territory_split(upc, split_rate, country)
      search_input_element.send_keys upc
      search_results_element.when_present.click
      split_element.send_keys split_rate
      country_dropdown_element.click
      country_textfield_element.send_keys country
      selected_country_element.click
      submit
      loading_dots_element.wait_while(&:present?)
      product_by_territory_level_split_table_element.wait_until(&:present?)
      loading_dots_element.wait_while(&:present?)
    end

    def delete_all_territory_splits
      delete_btn_elements.each do |del_btn|
        del_btn.when_present.click
        loading_dots_element.wait_while(&:present?)
        confirm_delete
        confirm_delete_element.wait_while(&:present?)
        loading_dots_element.wait_while(&:present?)
      end
    end
  end
end
