# frozen_string_literal: true

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

module Appium
  module Android
    EDIT_TEXT = 'android.widget.EditText'
    EditText = EDIT_TEXT

    # Find the first EditText that contains value or by index.
    # @param value [String, Integer] the text to match exactly.
    # If int then the EditText at that index is returned.
    # @return [EDIT_TEXT]
    def textfield(value)
      return ele_index EDIT_TEXT, value if value.is_a? Numeric

      complex_find_contains EDIT_TEXT, value
    end

    # Find all EditTexts containing value.
    # If value is omitted, all EditTexts are returned.
    # @param value [String] the value to search for
    # @return [Array<EDIT_TEXT>]
    def textfields(value = false)
      return tags EDIT_TEXT unless value

      complex_finds_contains EDIT_TEXT, value
    end

    # Find the first EditText.
    # @return [EDIT_TEXT]
    def first_textfield
      first_ele EDIT_TEXT
    end

    # Find the last EditText.
    # @return [EDIT_TEXT]
    def last_textfield
      last_ele EDIT_TEXT
    end

    # Find the first EditText that exactly matches value.
    # @param value [String] the value to match exactly
    # @return [EDIT_TEXT]
    def textfield_exact(value)
      complex_find_exact EDIT_TEXT, value
    end

    # Find all EditTexts that exactly match value.
    # @param value [String] the value to match exactly
    # @return [Array<EDIT_TEXT>]
    def textfields_exact(value)
      complex_finds_exact EDIT_TEXT, value
    end
  end # module Android
end # module Appium
