package com.example.pc_spring_rest;

import org.springframework.web.bind.annotation.*;

import java.util.NoSuchElementException;
import java.util.UUID;

@RestController
@RequestMapping("/clip")
public class RClip {
	private final Clipboard clipboard;

	public RClip(Clipboard clipboard) {
		this.clipboard = clipboard;
	}

	@GetMapping(produces="text/plain")
	public String getInfo() {
		return clipboard.toString();
	}
	
	@GetMapping(path="/{key}", produces="text/plain")
	public String getValue(@PathVariable("key") String key) throws NoSuchElementException {
		return clipboard.get(key);
	}
	
	@PutMapping(path="/{key}", consumes="text/plain")
	public void setValue(@PathVariable("key") String key,
			@RequestBody String value) throws NoSuchElementException {
		clipboard.set(key, value);
	}
	
	@DeleteMapping(path="/{key}")
	public void delValue(@PathVariable("key") String key) throws NoSuchElementException {
		clipboard.remove(key);
	}
	
	@PostMapping(consumes="text/plain", produces="text/plain")
	public String addValue(@RequestBody String value) {
		UUID uuid = UUID.randomUUID();
		String key = uuid.toString();
		clipboard.set(key, value);
		// TODO created
		return key;
	}
}