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

import java.io.Reader;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
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.AtomHelpers;
import be.ac.ulb.code.wit.resources.serializers.GroupDeserializer;
import be.ac.ulb.code.wit.resources.serializers.GroupSerializer;

@Path(ServicePaths.GROUPS)
public class Groups {
	private final Contacts contacts = ServiceConfiguration.getContacts();
	
	@GET
    @Produces(Constants.FEED_MEDIA_TYPE)
    public Response getGroups() {
		GroupSerializer serializer = new GroupSerializer(contacts);
		return Response
			.ok(AtomHelpers.stringify(serializer.makeFeed())).build();
	}
	
	@POST
    @Produces(Constants.ENTRY_MEDIA_TYPE)
    @Consumes(Constants.ENTRY_MEDIA_TYPE)
    public Response createGroup(final Reader data) {
		final GroupDeserializer deserializer = new GroupDeserializer(contacts);
		final GroupSerializer serializer = new GroupSerializer(contacts);
		deserializer.updateGroup(data);
		return Response
				.created(serializer.getURI(deserializer.getGroup()))
				.entity(serializer.makeEntry(deserializer.getGroup()))
				.build();
	}
}
