package ru.ulstu.strategy.api;

import ru.ulstu.core.model.UserContainer;
import ru.ulstu.user.model.User;

import java.util.List;
import java.util.stream.Collectors;

public abstract class EntityCreateStrategy<T extends UserContainer> {
    protected abstract List<T> getActiveEntities();

    protected abstract void createEntity(User user);

    protected void createDefaultEntityIfNeed(List<User> allUsers, List<? extends UserContainer> entities) {
        allUsers.forEach(user -> {
            if (entities
                    .stream()
                    .filter(entity -> entity.getUsers().contains(user))
                    .collect(Collectors.toSet()).isEmpty()) {
                createEntity(user);
            }
        });
    }

    public void createEntityIfNeed(List<User> allUsers) {
        createDefaultEntityIfNeed(allUsers, getActiveEntities());
    }
}
