package be.ac.ulb.code.wit.resources;

import java.io.Reader;

import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;

import org.apache.abdera.util.Constants;

import be.ac.ulb.code.wit.ServiceConfiguration;
import be.ac.ulb.code.wit.contacts.Contacts;
import be.ac.ulb.code.wit.resources.serializers.GroupDeserializer;
import be.ac.ulb.code.wit.resources.serializers.GroupSerializer;

@Path(ServicePaths.GROUP)
public class Group {
	private final Contacts contacts = ServiceConfiguration.getContacts();
	
	@GET
    @Produces(Constants.ENTRY_MEDIA_TYPE)
    public Response getGroup(@PathParam("group") final String group) {
		GroupSerializer serializer = new GroupSerializer(contacts);
		return Response.ok(serializer.makeEntry(group)).build();
	}
	
	@PUT
    @Consumes(Constants.ENTRY_MEDIA_TYPE)
    @Produces(Constants.ENTRY_MEDIA_TYPE)
    public Response updateGroup(@PathParam("group") final String group, final Reader data) {
		GroupDeserializer deserializer = new GroupDeserializer(contacts, group);
		GroupSerializer serializer = new GroupSerializer(contacts);
		deserializer.updateGroup(data);
		
		return Response.ok(
			serializer.makeEntry(deserializer.getGroup()))
			.build();
	}
	
	@DELETE
    public Response destroyGroup(@PathParam("group") final String group) {
		GroupDeserializer deserializer = new GroupDeserializer(contacts, group);
		deserializer.destroyGroup();
		return Response.ok().build();
	}
}
