module MastersRegistry
  class MastersEditorPage < BasePage
    page_url "/mastersregistry/editor"

    div(:ownership_panel, class: "RegistryEditor-ownership")
    div(:success_message, class: "DismissibleAlert-success")
    text_field(:isrc_field, id: "search-by-isrc")
    button(:search_button) { form_element(class: "SearchBox").button_element }
    div(:add_new_rightsholder, text: "ADD RIGHTSHOLDER +")
    text_field(:new_rightsholder_tuid) do
      rightsholders_section_element.text_field_element(class: "AddForm-input")
    end
    radio_button_group(:claim_or_not, class: "RadioButton-radio")
    text_area(:new_rightsholder_territories) { rightsholders_section_element.text_area_element }
    button(:submit_rightsholder) do
      rightsholders_section_element.button_element(class: "AddForm-confirm-button")
    end
    div(:rightsholders_section) do
      div_element(text: /^Rightsholders/, class: "OwnershipHeaderInternalConflicts").when_present.parent
    end
    div(:locked_territories_section) do
      div_element(visible_text: /^Locked Territories/, class: "OwnershipHeaderInternalConflicts")
        .when_present.parent
    end
    div(:unclaimed_territories_section, class: "OwnershipDetailInternalConflicts-unclaimed-territories")
    divs(:territory_section, class: "OwnershipDetailInternalConflicts-territories")
    divs(:owner, class: "TerritoriesLinks")
    links(:show_all, text: "Show All")

    def convert_territory(territory)
      all_territories = Support::Services::OwsTerritories.iso_spec("ISO_3166_1_2016")
      if territory.size == 2
        all_territories.find { |x| x["territory_code_a2"] == territory }["territory_name"]
      else
        all_territories.find { |x| x["territory_name"] == territory }["territory_code_a2"]
      end
    end

    def modify_territories(add_or_remove, territories, ownership)
      territories_section_element = owner_section(ownership)
      modify_territories_for_section(add_or_remove, territories, territories_section_element)
    end

    def remove_single_territory(territory)
      show_all_elements.last.click until show_all_elements.empty?
      targeted_section = territory_section_elements.find { |x| x.text.include? territory }
      modify_territories_for_section(:remove, territory, targeted_section)
    end

    def section_territories(ownership)
      section = owner_section(ownership)
      if ownership == "Unclaimed"
        territories = section.ps.last
      else
        territories = section.div_element(class: "OwnershipDetailInternalConflicts-territories")
                             .p
      end
      territories.text.split(",").map(&:strip).uniq
    end

    def unclaim_all_territories
      until owner_elements.empty?
        first_owner = owner_elements[0].parent.parent
        modify_territories_for_section(:remove, "ww", first_owner.parent)
      end
      expect(owner_elements).to be_empty
    end

    private

    def owner_section(ownership)
      case ownership
      when "Rightsholders"
        rightsholders_section_element
      when "Locked Territories"
        locked_territories_section_element
      when "Unclaimed"
        unclaimed_territories_section_element
      else
        fail "#{ownership} isn't an option"
      end
    end

    def add_or_remove(action)
      case action
      when :add
        "Add these territories"
      when :lock
        "Lock these territories"
      when :remove
        "Remove these territories"
      else
        fail "unknown operation: '#{action}'!"
      end
    end

    def modify_territories_for_section(action, territories, territories_section_element)
      action = add_or_remove(action)
      territories_section_element.link_element(class: "TerritoriesLinks-edit-link").click
      territories_section_element.div_element(class: "Select-value").when_present.click
      territories_section_element.div_element(class: "Select-option", text: action).when_present.click
      territories_section_element.text_area_element.when_present.element.set territories
      territories_section_element.button_element(text: "Done").click
      wait_until { !territories_section_element.text_area_element.element.present? }
    end
  end
end
