class Geocoder::Result::Yandex

def address(_format = :full)

def address(_format = :full)
  @data['GeoObject']['metaDataProperty']['GeocoderMetaData']['text']
end

def admin_locality

def admin_locality
  find_in_hash(@data, *ADMIN_LEVEL, 'Locality')
end

def city

def city
  result =
    if state.empty?
      find_in_hash(@data, *COUNTRY_LEVEL, 'Locality', 'LocalityName')
    elsif sub_state.empty?
      find_in_hash(@data, *ADMIN_LEVEL, 'Locality', 'LocalityName')
    else
      find_in_hash(@data, *SUBADMIN_LEVEL, 'Locality', 'LocalityName')
    end
  result || ""
end

def coordinates

def coordinates
  @data['GeoObject']['Point']['pos'].split(' ').reverse.map(&:to_f)
end

def country

def country
  find_in_hash(@data, *COUNTRY_LEVEL, 'CountryName') || ""
end

def country_code

def country_code
  find_in_hash(@data, *COUNTRY_LEVEL, 'CountryNameCode') || ""
end

def country_level_locality

def country_level_locality
  find_in_hash(@data, *COUNTRY_LEVEL, 'Locality')
end

def dependent_locality

def dependent_locality
  find_in_hash(@data, *DEPENDENT_LOCALITY_1) ||
    find_in_hash(@data, *DEPENDENT_LOCALITY_2)
end

def find_in_hash(source, *keys)

def find_in_hash(source, *keys)
  key = keys.shift
  result = source[key]
  if keys.empty?
    return result
  elsif !result.is_a?(Hash)
    return nil
  end
  find_in_hash(result, *keys)
end

def kind

def kind
  @data['GeoObject']['metaDataProperty']['GeocoderMetaData']['kind']
end

def locality_data

def locality_data
  dependent_locality || subadmin_locality || admin_locality ||
    country_level_locality || top_level_locality
end

def postal_code

def postal_code
  return "" unless premise.is_a?(Hash)
  find_in_hash(premise, 'PostalCode', 'PostalCodeNumber') || ""
end

def precision

def precision
  @data['GeoObject']['metaDataProperty']['GeocoderMetaData']['precision']
end

def premise

def premise
  if thoroughfare_data.is_a?(Hash)
    thoroughfare_data['Premise']
  elsif locality_data.is_a?(Hash)
    locality_data['Premise']
  end
end

def premise_name

def premise_name
  premise.is_a?(Hash) ? premise.fetch('PremiseName', "") : ""
end

def state

def state
  find_in_hash(@data, *ADMIN_LEVEL, 'AdministrativeAreaName') || ""
end

def state_code

def state_code
  ""
end

def street

def street
  thoroughfare_data.is_a?(Hash) ? thoroughfare_data['ThoroughfareName'] : ""
end

def street_number

def street_number
  premise.is_a?(Hash) ? premise.fetch('PremiseNumber', "") : ""
end

def sub_state

def sub_state
  return "" if state.empty?
  find_in_hash(@data, *SUBADMIN_LEVEL, 'SubAdministrativeAreaName') || ""
end

def subadmin_locality

def subadmin_locality
  find_in_hash(@data, *SUBADMIN_LEVEL, 'Locality')
end

def thoroughfare_data

def thoroughfare_data
  locality_data['Thoroughfare'] if locality_data.is_a?(Hash)
end

def top_level_locality

def top_level_locality
  find_in_hash(@data, *ADDRESS_DETAILS, 'Locality')
end

def viewport

def viewport
  envelope = @data['GeoObject']['boundedBy']['Envelope'] || fail
  east, north = envelope['upperCorner'].split(' ').map(&:to_f)
  west, south = envelope['lowerCorner'].split(' ').map(&:to_f)
  [south, west, north, east]
end