package com.telephoners.krakyournet.ctf.resources;

import com.telephoners.krakyournet.ctf.beans.Flag;
import com.telephoners.krakyournet.ctf.beans.Solution;
import com.telephoners.krakyournet.ctf.beans.Team;
import com.telephoners.krakyournet.ctf.beans.User;
import com.telephoners.krakyournet.ctf.beans.tasks.Task;
import com.telephoners.krakyournet.ctf.repositories.Repository;
import io.dropwizard.auth.Auth;

import javax.inject.Inject;
import javax.inject.Singleton;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.List;
import java.util.Map;

@Singleton
@Path(value = "/solutions")
@Produces(MediaType.APPLICATION_JSON)
public class SolutionsResource
{
    private final Repository<Team> teamsRepository;
    private final Repository<Solution> solutionsRepository;
    private final Repository<Task> tasksRepository;

    @Inject
    public SolutionsResource(Repository<Solution> solutionsRepository,
                             Repository<Task> tasksRepository,
                             Repository<Team> teamsRepository)
    {
        this.solutionsRepository = solutionsRepository;
        this.teamsRepository = teamsRepository;
        this.tasksRepository = tasksRepository;
    }

    @POST
    @Path("/{task_level}")
    public Response submitSolution(@Auth User user,
                                   @PathParam("task_level") int taskLevel,
                                   String userFlag) throws Exception
    {
        return null;
        //todo: this should be by id
        /*
        Task task = tasksRepository.getByLevel(taskLevel);
        Flag flag = task.getFlags()
                .stream()
                .filter(taskFlag -> taskFlag.getValue().equals(userFlag))
                .findFirst()
                .orElseThrow(InvalidSolutionException::new);

        solutionsRepository.submitSolution(new Solution(teamsRepository.getTeamByUser(user), task, flag));
        return Response.ok().entity(flag).build();*/
    }

    @GET
    @Path("/my/completed")
    public List<Integer> getTeamCompletedTasks(@Auth User user)
    {
return null;
        /*
        return solutionsRepository.getCompletedTasks(teamsRepository.getTeamByUser(user));*/
    }

    @GET
    @Path("/all/completed")
    public Map<String, List<Integer>> getAllTeamsCompletedTasks(@Auth User user)
    {
        return null;
        /*
        return teamsRepository.getAll()
                .stream()
                .collect(Collectors.toMap(
                        Team::getName,
                        solutionsRepository::getCompletedTasks
                ));*/
    }

    @GET
    @Path("/my")
    public Map<Integer, List<Flag>> getTeamSummittedFlags(@Auth User user) {
        return null;
        /*
        Map<Integer, List<Flag>> teamSolutions = solutionsRepository.getTeamSolutions(teamsRepository.getTeamByUser(user));
        return teamSolutions;*/
    }

    @GET
    @Path("/all")
    public Map<String, Map<Integer, Integer>> getTeamsSolutions()
    {
        return null;
        /*
        return teamsRepository.getAll()
                .stream()
                .collect(Collectors.toMap(
                        Team::getName,
                        solutionsRepository::getPointsSum
                ));*/
    }
}
