package secgroup

// FilterByID returns ListSecurityGroups with specified ID.
func (lsg ListSecurityGroups) FilterByID(id uint64) ListSecurityGroups {
	predicate := func(isg ItemSecurityGroup) bool {
		return isg.ID == id
	}

	return lsg.FilterFunc(predicate)
}

// FilterByID returns ListSecurityGroups with specified Name.
func (lsg ListSecurityGroups) FilterByName(name string) ListSecurityGroups {
	predicate := func(isg ItemSecurityGroup) bool {
		return isg.Name == name
	}

	return lsg.FilterFunc(predicate)
}

// FilterByCreatedBy returns ListSecurityGroups with specified CreatedBy.
func (lsg ListSecurityGroups) FilterByCreatedBy(createdBy string) ListSecurityGroups {
	predicate := func(isg ItemSecurityGroup) bool {
		return isg.CreatedBy == createdBy
	}

	return lsg.FilterFunc(predicate)
}

// FilterByDescription returns ListSecurityGroups with specified Description.
func (lsg ListSecurityGroups) FilterByDescription(description string) ListSecurityGroups {
	predicate := func(isg ItemSecurityGroup) bool {
		return isg.Description == description
	}

	return lsg.FilterFunc(predicate)
}

// FilterByUpdatedBy returns ListSecurityGroups with specified UpdatedBy.
func (lsg ListSecurityGroups) FilterByUpdatedBy(updatedBy string) ListSecurityGroups {
	predicate := func(isg ItemSecurityGroup) bool {
		return isg.UpdatedBy == updatedBy
	}

	return lsg.FilterFunc(predicate)
}

// FilterByAccountID returns ListSecurityGroups with specified AccountID.
func (lsg ListSecurityGroups) FilterByAccountID(accountID uint64) ListSecurityGroups {
	predicate := func(isg ItemSecurityGroup) bool {
		return isg.AccountID == accountID
	}

	return lsg.FilterFunc(predicate)
}

// FilterFunc allows filtering ListSecurityGroups based on a user-specified predicate.
func (lsg ListSecurityGroups) FilterFunc(predicate func(ItemSecurityGroup) bool) ListSecurityGroups {
	var result ListSecurityGroups

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

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

	return result
}

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

	return lsg.Data[0]
}
