package no.javazone.api.sessions;

import io.dropwizard.jersey.caching.CacheControl;
import no.javazone.api.sessions.dto.SessionDTOMapper;
import no.javazone.api.sessions.dto.SessionDetaljerDTOMapper;
import no.javazone.devnull.DevNullUriCreator;
import no.javazone.http.PathResolver;
import no.javazone.sessions.Event;
import no.javazone.sessions.Session;
import no.javazone.sessions.SessionId;
import no.javazone.sessions.SessionRepository;

import javax.ws.rs.*;
import javax.ws.rs.core.*;
import java.net.URI;
import java.util.Optional;
import java.util.concurrent.TimeUnit;

@Path("/events/{eventId}/sessions")
@Produces(MediaType.APPLICATION_JSON)
public class SessionResource {

    private PathResolver pathResolver;
    private SessionRepository sessionRepository;
    private DevNullUriCreator devNullUriCreator;

    @Context
    private UriInfo uriInfo;

    public SessionResource(
            PathResolver pathResolver,
            SessionRepository sessionRepository,
            DevNullUriCreator devNullUriCreator) {
        this.pathResolver = pathResolver;
        this.sessionRepository = sessionRepository;
        this.devNullUriCreator = devNullUriCreator;
    }

    @GET
    @CacheControl(maxAge = 5, maxAgeUnit = TimeUnit.MINUTES)
    public Response getSessions(@PathParam("eventId") String eventSlug, @HeaderParam("X-Forwarded-Proto") String forwardedProto) {
        Optional<Event> eventOptional = sessionRepository.getSessions(eventSlug);
        URI contextRoot = resolveContextRoot(forwardedProto);

        return eventOptional
            .map(x -> SessionDTOMapper.toSessionDTOs(
                    x,
                    pathResolver.path(uriInfo),
                    contextRoot,
                    devNullUriCreator))
            .map(x -> Response.ok().entity(x).build())
            .orElse(Response.status(503).build());
    }


    @GET
    @Path("/{sessionId}")
    @CacheControl(maxAge = 5, maxAgeUnit = TimeUnit.MINUTES)
    public Response getSession(
            @PathParam("eventId") String eventSlug,
            @PathParam("sessionId") String sessionId,
            @HeaderParam("X-Forwarded-Proto") String forwardedProto
    ) {
        Optional<Session> sessionOptional = sessionRepository.getSession(eventSlug, new SessionId(sessionId));
        URI contextRoot = resolveContextRoot(forwardedProto);
        return sessionOptional
                .map(session -> SessionDetaljerDTOMapper
                        .toSessionDetaljerDTO(session, contextRoot, devNullUriCreator))
                .map(x -> Response.ok().entity(x).build())
                .orElse(Response.status(503).build());

    }

    private URI resolveContextRoot(@HeaderParam("X-Forwarded-Proto") String forwardedProto) {
        return UriBuilder.fromUri(pathResolver.getContextRoot())
                .scheme(forwardedProto != null ? forwardedProto : uriInfo.getRequestUri().getScheme()).build();
    }


}

@@ -1824,32 +1824,37 @@
     .orElse(
+error
 Response
 .status(503)
@@ -1841,35 +1841,34 @@
 Response
-.status(503).build(
+(%22Event not found%22
 ));%0A    
@@ -1870,17 +1870,16 @@
 %0A    %7D%0A%0A
-%0A
     @GET
@@ -2616,27 +2616,232 @@
 lse(
-Response.status(503
+errorResponse(%22Session not found%22));%0A    %7D%0A%0A    private Response errorResponse(String errorMessage) %7B%0A        return Response.status(404).type(MediaType.APPLICATION_JSON_TYPE).entity(%22%7B%5C%22message%5C%22: %5C%22%22 + errorMessage + %22%5C%22%7D%22
 ).bu
@@ -2849,19 +2849,17 @@
 ld()
-);%0A
+;
 %0A    %7D%0A%0A
     
@@ -2846,32 +2846,33 @@
 build();%0A    %7D%0A%0A
+%0A
     private URI 

Return 404 instead of 503 if we can't find session/event.