package com.telephoners.krakyournet.ctf.resources;

import com.telephoners.krakyournet.ctf.beans.Team;
import com.telephoners.krakyournet.ctf.beans.User;
import com.telephoners.krakyournet.ctf.repositories.Repository;

import javax.inject.Inject;
import javax.inject.Singleton;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;

@Singleton
@Path(value = "/register")
@Produces(MediaType.APPLICATION_JSON)
public class RegistrationResource
{
    private final Repository<Team> teamsRepository;
    private final Repository<User> userRepository;

    @Inject
    public RegistrationResource(final Repository<User> userRepository,
                                final Repository<Team> teamsRepository)
    {
        //todo: checkNotNUll
        this.teamsRepository = teamsRepository;
        this.userRepository = userRepository;
    }

    @GET
    @Path("/team")
    public String registerTeam(@QueryParam("teamName") final String teamName)
    {
        Team team = new Team();
        team.setName(teamName);
        team.setDescription("descr");

        teamsRepository.add(team);
        return team.getId();
    }

    @GET
    @Path("/user")
    public void register(@QueryParam("userName") final String userName,
                         @QueryParam("teamId") final String teamId)
    {
        if (!teamsRepository.contains(teamId))
        {
            throw new RuntimeException("Team does not exist");
        }

        User user = new User();
        user.setEmail("gpietrusza@gmail.com");
        user.setName(userName);
        user.setPassword("password");
        user.setAdmin(false);
        user.setTeamId(teamId);
    }
}
