package resources;

import objects.Solution;
import repositories.SolutionsRepository;

import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * Created by gpietrus on 16.02.16.
 */
@Path(value = "/solutions")
@Produces(MediaType.APPLICATION_JSON)
public class SolutionsResource {

    private SolutionsRepository solutionsRepository;

    @Inject
    public SolutionsResource(SolutionsRepository solutionsRepository) {
        this.solutionsRepository = solutionsRepository;
    }

    @GET
    @Path("/all")
    public Map<String, List<String>> getTeamsSolutions() {
        return solutionsRepository.getAll()
                .stream()
                .collect(Collectors.groupingBy(new Function<Solution, String>() {
                    @Override
                    public String apply(Solution solution) {
                        return solution.getTaskName();
                    }
                }))
                .entrySet()
                .stream()
                .collect(Collectors.toMap(
                        Map.Entry::getKey,
                        entry -> entry.getValue().stream()
                                .map(solution -> solution.getTeam().getName())
                                .collect(Collectors.toList())
                ));

    }

}
