package rest;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import facade.Facade;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.Produces;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.SecurityContext;

@Path("timers")
public class TimerResource {

    Facade facade = new Facade();
    Gson gson = new GsonBuilder().setPrettyPrinting().create();

    @Context
    private UriInfo context;

    @Context
    SecurityContext securityContext;

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Path("leaderboard/{iterator}")
    public String retrieveLeaderBoard(@PathParam("iterator") int iterator) {
        facade.checkTimeElapse();
        return gson.toJson(facade.getleaderBoard(iterator));
    }
    
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Path("leaderboard/minified/{iterator}")
    public String retrieveLeaderBoardMinified(@PathParam("iterator") int iterator) {
        facade.checkTimeElapse();
        return gson.toJson(facade.getleaderBoardMinified(iterator));
    }

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Path("allmaps")
    public String getAllMaps() {
        facade.checkTimeElapse();
        return gson.toJson(facade.getAllMaps());
    }

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Path("mapsizecache/{mapname}/{stage}")
    public String getSpecifcMapSize(@PathParam("mapname") String mapname, @PathParam("stage") int stage) {
        facade.checkTimeElapse();
        return gson.toJson(facade.getSpecificFetchMap(mapname, stage));
    }

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Path("player/{steamid}")
    public String retrievePlayer(@PathParam("steamid") String steamid) {
        facade.checkTimeElapse();
        return gson.toJson(facade.getPlayer(steamid));
    }

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Path("player/badges/{steamid}")
    public String retrievePlayerBadges(@PathParam("steamid") String steamid) {
        facade.checkTimeElapse();
        return gson.toJson(facade.getPlayerBadges(steamid));
    }

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Path("player/maps/{steamid}/{iterator}")
    public String retrievePlayerMaps(@PathParam("steamid") String steamid, @PathParam("iterator") int iterator) {
        facade.checkTimeElapse();
        return gson.toJson(facade.getPlayerMaps(steamid, iterator));
    }

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Path("map/{mapname}/{number}/{iterator}")
    public String retrieveSpecificMapStages(@PathParam("mapname") String mapname, @PathParam("number") int stage,
            @PathParam("iterator") int iterator) {
        facade.checkTimeElapse();
        return gson.toJson(facade.getSpecificMap(mapname, stage, iterator));
    }

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Path("searchplayers/{identifier}")
    public String searchPlayers(@PathParam("identifier") String identifier) {
        facade.checkTimeElapse();
        return gson.toJson(facade.getSearchPlayerResults(identifier));
    }

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Path("searchmaps/{identifier}")
    public String searchMaps(@PathParam("identifier") String identifier) {
        facade.checkTimeElapse();
        return gson.toJson(facade.getSearchMapsResults(identifier));
    }
}
