package flips

// FilterByID returns FloatingIPsList with specified ID.
func (fil FloatingIPsList) FilterByID(id string) FloatingIPsList {
	predicate := func(fi RecordFloatingIP) bool {
		return fi.ID == id
	}

	return fil.FilterFunc(predicate)
}

// FilterByName returns FloatingIPsList with specified AccessGroupName.
func (fil FloatingIPsList) FilterByAccessGroupName(name string) FloatingIPsList {
	predicate := func(fi RecordFloatingIP) bool {
		return fi.AccessGroupName == name
	}

	return fil.FilterFunc(predicate)
}

// FilterFunc allows filtering FloatingIPsList based on a user-specified predicate.
func (fil FloatingIPsList) FilterFunc(predicate func(fi RecordFloatingIP) bool) FloatingIPsList {
	var result FloatingIPsList

	for _, acc := range fil.Objects {
		if predicate(acc) {
			result.Objects = append(result.Objects, acc)
		}
	}

	return result
}

// FindOne returns first element.
// If none was found, returns an empty struct.
func (fil FloatingIPsList) FindOne() RecordFloatingIP {
	if len(fil.Objects) == 0 {
		return RecordFloatingIP{}
	}

	return fil.Objects[0]
}
