package com.example.pc_spring_rest_jersey;

import org.springframework.beans.factory.annotation.Autowired;

import java.net.URI;
import java.util.NoSuchElementException;
import java.util.UUID;

import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
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.Context;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;

@Path("/clip")
@Produces("text/plain")
@Consumes("text/plain")
public class RClip {
	@Autowired
	private Clipboard clipboard;
	
	@GET
	public String getInfo() {
		return clipboard.toString();
	}
	
	@GET
	@Path("/{key}")
	public String getValue(@PathParam("key") String key) throws NoSuchElementException {
		return clipboard.get(key);
	}
	
	@PUT
	@Path("/{key}")
	public void setValue(@PathParam("key") String key,
			String value) throws NoSuchElementException {
		clipboard.set(key, value);
	}
	
	@DELETE
	@Path("/{key}")
	public void delValue(@PathParam("key") String key) throws NoSuchElementException {
		clipboard.remove(key);
	}
	
	@POST
	public Response setValue(String value,
			@Context UriInfo uriInfo) throws NoSuchElementException {
		UUID uuid = UUID.randomUUID();
		String key = uuid.toString();
		clipboard.set(key, value);
		URI location = uriInfo.getAbsolutePathBuilder()
				.path("/{key}")
				.build(key);
		return Response.created(location).build();
	}
}
