package stpolicy

// FilterByID returns ListStoragePolicies with specified ID.
func (lsp ListStoragePolicies) FilterByID(id uint64) ListStoragePolicies {
	predicate := func(isp ItemStoragePolicy) bool {
		return isp.ID == id
	}

	return lsp.FilterFunc(predicate)
}

// FilterByName returns ListStoragePolicies with specified Name.
func (lsp ListStoragePolicies) FilterByName(name string) ListStoragePolicies {
	predicate := func(isp ItemStoragePolicy) bool {
		return isp.Name == name
	}

	return lsp.FilterFunc(predicate)
}

// FilterByStatus returns ListStoragePolicies with specified Status.
func (lsp ListStoragePolicies) FilterByStatus(status string) ListStoragePolicies {
	predicate := func(isp ItemStoragePolicy) bool {
		return isp.Status == status
	}

	return lsp.FilterFunc(predicate)
}

// FilterByStatus returns ListStoragePolicies with specified Desc.
func (lsp ListStoragePolicies) FilterByDesc(desc string) ListStoragePolicies {
	predicate := func(isp ItemStoragePolicy) bool {
		return isp.Description == desc
	}

	return lsp.FilterFunc(predicate)
}

// FilterByLimitIOPS returns ListStoragePolicies with specified IOPS.
func (lsp ListStoragePolicies) FilterByLimitIOPS(limitIOPS uint64) ListStoragePolicies {
	predicate := func(isp ItemStoragePolicy) bool {
		return isp.LimitIOPS == limitIOPS
	}

	return lsp.FilterFunc(predicate)
}

// FilterFunc allows filtering ListStoragePolicies based on a user-specified predicate.
func (lsp ListStoragePolicies) FilterFunc(predicate func(ItemStoragePolicy) bool) ListStoragePolicies {
	var result ListStoragePolicies

	for _, item := range lsp.Data {
		if predicate(item) {
			result.Data = append(result.Data, item)
		}
	}

	result.EntryCount = uint64(len(result.Data))

	return result
}

// FindOne returns first found ItemStoragePolicy
// If none was found, returns an empty struct.
func (lsp ListStoragePolicies) FindOne() ItemStoragePolicy {
	if len(lsp.Data) == 0 {
		return ItemStoragePolicy{}
	}

	return lsp.Data[0]
}
