module Project
  # The territory restrictions page should only have very small differences
  # between physical and digital, so we can use the same page for both. This
  # can be separated later if need be.
  class ProductTerritoryRestrictionsPage < BasePage
    include ProductProgressBar

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

    element(:territory_restrictions_header, class: "TerritoryRestrictions-header")
    div(:territories_list, class: "TerritoriesList")
    button(:physical_save_and_continue, class: "LoadingButton")
    div(:cleared_territory_list, class: "TerritoriesListManager", index: 0)
    div(:restrict_selected_territories) do
      cleared_territory_list_element.div_element(class: "MoveCountryButton")
    end
    div(:restricted_territory_list, class: "TerritoriesListManager", index: 1)
    div(:clear_selected_territories) do
      restricted_territory_list_element.div_element(class: "MoveCountryButton")
    end

    def clear_all_restricted_continents
      continent_list = restricted_territory_list_element.div_elements(class: "ContinentItem")
      return if continent_list.empty?
      # otherwise check doesnt register
      sleep 1
      continent_list.map { |continent_row| continent_row.checkbox_element.check }
      clear_selected_territories_element.click
    end

    def check_cleared_continent(continent)
      continent_row = continent_row_element(status: :cleared, continent: continent)
      fail "Couldn't find #{continent}!" unless continent_row.element.present?
      continent_row.checkbox_element.check
    end

    def check_restricted_country(continent:, country:)
      continent_row = continent_row_element(status: :restricted, continent: continent)
      fail "Couldn't find #{continent}!" unless continent_row.element.present?
      continent_row.svg_element(class: "Icons-chevron").click
      country_row = country_row_element(status: :restricted, country: country)
      fail "Couldn't find #{country}!" unless country_row.element.present?
      country_row.checkbox_element.check
    end

    def number_of_restricted_territories
      counter_text = restricted_territory_list_element.div_element(class: "CountriesCounter").text
      counter_text.match(/(\d+) of/)[1].to_i
    end

    private

    def continent_row_element(status:, continent:)
      territory_list = territory_list_panel(status: status)
      continent_row = territory_list.div_element(class: "ContinentItem", text: /^#{continent}/)
      fail "Couldn't find #{continent}!" unless continent_row.element.present?
      continent_row
    end

    def country_row_element(status:, country:)
      territory_list = territory_list_panel(status: status)
      country_row = territory_list.div_element(class: "ContinentItem-country", text: /^#{country}/)
      fail "Couldn't find #{country}!" unless country_row.element.present?
      country_row
    end

    def territory_list_panel(status:)
      case status
      when :cleared
        cleared_territory_list_element
      when :restricted
        restricted_territory_list_element
      else
        fail "unknown status: '#{status}'!"
      end
    end
  end
end
