package extnet

// FilterByID returns ListExtNet with specified ID.
func (eList ListExtNet) FilterByID(id string) ListExtNet {
	predicate := func(extNet ExternalNetworkResponse) bool {
		return extNet.ID == id
	}

	return eList.FilterFunc(predicate)
}

// FilterByName returns ListExtNet with specified Bridge network name.
func (eList ListExtNet) FilterByName(name string) ListExtNet {
	predicate := func(extNet ExternalNetworkResponse) bool {
		return extNet.BridgeNetworkName == name
	}

	return eList.FilterFunc(predicate)
}

// FilterByIPv4 returns ListExtNet with specified default gateway IPv4.
func (eList ListExtNet) FilterByIPv4(IPv4 string) ListExtNet {
	predicate := func(extNet ExternalNetworkResponse) bool {
		return extNet.DefaultGatewayIPv4 == IPv4
	}

	return eList.FilterFunc(predicate)
}

// FilterByIPv6 returns ListExtNet with specified default gateway IPv6.
func (eList ListExtNet) FilterByIPv6(IPv6 string) ListExtNet {
	predicate := func(extNet ExternalNetworkResponse) bool {
		return extNet.DefaultGatewayIPv6 == IPv6
	}

	return eList.FilterFunc(predicate)
}

// FilterFunc allows filtering ListExtNet based on a user-specified predicate.
func (eList ListExtNet) FilterFunc(predicate func(response ExternalNetworkResponse) bool) ListExtNet {
	var result ListExtNet

	for _, item := range eList {
		if predicate(item) {
			result = append(result, item)
		}
	}

	return result
}

// FindOne returns first element.
// If none was found, returns an empty struct.
func (eList ListExtNet) FindOne() ExternalNetworkResponse {
	if len(eList) == 0 {
		return ExternalNetworkResponse{}
	}

	return eList[0]
}
