package org.croudtrip.rest;

import com.google.common.base.Optional;

import org.croudtrip.account.UserManager;
import org.croudtrip.api.account.User;
import org.croudtrip.api.gcm.GcmRegistration;
import org.croudtrip.api.gcm.GcmRegistrationDescription;
import org.croudtrip.gcm.GcmManager;

import java.io.IOException;

import javax.inject.Inject;
import javax.validation.Valid;
import javax.ws.rs.Consumes;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;

import io.dropwizard.auth.Auth;
import io.dropwizard.hibernate.UnitOfWork;

/**
 * Resource for managing GCM registrations (and messages for testing).
 */
@Path("/gcm")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class GcmRegistrationResource {

    private final GcmManager gcmManager;
    private final UserManager userManager;

    @Inject
    GcmRegistrationResource(GcmManager gcmManager, UserManager userManager) {
        this.gcmManager = gcmManager;
        this.userManager = userManager;
    }


    @PUT
    @UnitOfWork
    public GcmRegistration register(@Auth User user, @Valid GcmRegistrationDescription description) {
        return gcmManager.register(user, description);
    }


    @GET
    @UnitOfWork
    public GcmRegistration getRegistration(@Auth User user) {
        Optional<GcmRegistration> registration = gcmManager.findRegistrationByUser(user);
        if (registration.isPresent()) return registration.get();
        else throw RestUtils.createNotFoundException();
    }


    @POST
    @UnitOfWork
    public void sendTestMessage(@QueryParam("userId") Long userId, @DefaultValue("hello") @QueryParam("msg") String message) throws IOException {
        if (userId == null) throw RestUtils.createJsonFormattedException("missing query param \"userId\"", 400);

        Optional<User> user = userManager.findUserById(userId);
        if (!user.isPresent()) throw RestUtils.createNotFoundException();
        Optional<GcmRegistration> registration = gcmManager.findRegistrationByUser(user.get());
        if (!registration.isPresent()) throw RestUtils.createNotFoundException();

        gcmManager.sendGcmMessage(registration.get(), message);
    }

}

@@ -398,24 +398,51 @@
 s.Consumes;%0A
+import javax.ws.rs.DELETE;%0A
 import javax
@@ -1388,24 +1388,329 @@
 n);%0A    %7D%0A%0A%0A
+    @DELETE%0A    @UnitOfWork%0A    public void unregister(@Auth User user) %7B%0A        Optional%3CGcmRegistration%3E registration = gcmManager.findRegistrationByUser(user);%0A        if (!registration.isPresent()) throw RestUtils.createNotFoundException();%0A        gcmManager.unregister(registration.get());%0A    %7D%0A%0A%0A
     @GET%0A   

add gcm unregistration to REST API