package org.people.service.restful;

import java.util.List;

import org.people.model.Family;
import org.people.model.Person;
import org.people.service.FamilyExistsException;
import org.people.service.PeopleService;
import org.people.service.PersonExistsException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class PeopleServiceRestController {

	@Autowired
	private PeopleService peopleService;

	@RequestMapping(value = "/family", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
	public Family addFamily(Family family) throws FamilyExistsException {
		return peopleService.addFamily(family);
	}

	@RequestMapping(value = "/family/update", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
	public Family updateFamily(Family family) throws FamilyExistsException {
		return peopleService.updateFamily(family);
	}

	@RequestMapping(value = "/family/find/{name}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public List<Family> findFamily(@PathVariable String name) {
		return peopleService.findFamily(name);
	}

	@RequestMapping(value = "/family/{fid}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public Family getFamily(@PathVariable Long fid) {
		return peopleService.getFamily(fid);
	}

	@RequestMapping(value = "/person", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
	public Person addPerson(Person person) throws PersonExistsException {
		return peopleService.addPerson(person);
	}

	@RequestMapping(value = "/person/update", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
	public Person updatePerson(Person person) throws PersonExistsException {
		return peopleService.updatePerson(person);
	}

	@RequestMapping(value = "/person/find/{name}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public List<Person> findPerson(@PathVariable String name) {
		return peopleService.findPerson(name);
	}

	@RequestMapping(value = "/person/{pid}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public Person getPerson(@PathVariable Long pid) {
		return peopleService.getPerson(pid);
	}

	@RequestMapping(value = "/person/{pid}/family/{fid}", method = RequestMethod.GET, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
	public void addPersonToFamily(@PathVariable Long pid, @PathVariable Long fid) {
		peopleService.addPersonToFamily(pid, fid);
	}

	@RequestMapping(value = "/person/{pid}/family/{fid}/delete", method = RequestMethod.GET, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
	public void removePersonFromFamily(Long pid, Long fid) {
		peopleService.removePersonFromFamily(pid, fid);
	}

	@RequestMapping(value = "/family/{fid}/people", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public List<Person> getFamilyPeople(Long fid) {
		return peopleService.getFamilyPeople(fid);
	}

	@ExceptionHandler({ FamilyExistsException.class })
	@ResponseStatus(HttpStatus.BAD_REQUEST)
	public ResponseEntity<RestError> handleFamilyExistsException(
			FamilyExistsException exe) {
		RestError restError = new RestError(HttpStatus.BAD_REQUEST,
				exe.getMessage());
		HttpHeaders httpHeaders = new HttpHeaders();
		httpHeaders.setContentType(MediaType.APPLICATION_JSON);
		return new ResponseEntity<RestError>(restError, httpHeaders,
				HttpStatus.BAD_REQUEST);
	}

	@ExceptionHandler({ PersonExistsException.class })
	@ResponseStatus(HttpStatus.BAD_REQUEST)
	public ResponseEntity<RestError> handlePersonExistsException(
			PersonExistsException exe) {
		RestError restError = new RestError(HttpStatus.BAD_REQUEST,
				exe.getMessage());
		HttpHeaders httpHeaders = new HttpHeaders();
		httpHeaders.setContentType(MediaType.APPLICATION_JSON);
		return new ResponseEntity<RestError>(restError, httpHeaders,
				HttpStatus.BAD_REQUEST);
	}
}

@@ -626,16 +626,76 @@
 riable;%0A
+import org.springframework.web.bind.annotation.RequestBody;%0A
 import o
@@ -1225,24 +1225,37 @@
 y addFamily(
+@RequestBody 
 Family famil
@@ -1248,33 +1248,36 @@
 y Family family)
- 
+%0A%09%09%09
 throws FamilyExi
@@ -1527,16 +1527,29 @@
 eFamily(
+@RequestBody 
 Family f
@@ -1554,17 +1554,20 @@
  family)
- 
+%0A%09%09%09
 throws F
@@ -1630,32 +1630,225 @@
 ly(family);%0A%09%7D%0A%0A
+%09@RequestMapping(value = %22/family/find%22, method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)%0A%09public List%3CFamily%3E findFamily() %7B%0A%09%09return peopleService.findFamily(%22%22);%0A%09%7D%0A%0A
 %09@RequestMapping
@@ -2443,24 +2443,37 @@
 n addPerson(
+@RequestBody 
 Person perso
@@ -2466,33 +2466,36 @@
 y Person person)
- 
+%0A%09%09%09
 throws PersonExi
@@ -2745,16 +2745,29 @@
 ePerson(
+@RequestBody 
 Person p
@@ -2772,17 +2772,20 @@
  person)
- 
+%0A%09%09%09
 throws P
@@ -2848,32 +2848,225 @@
 on(person);%0A%09%7D%0A%0A
+%09@RequestMapping(value = %22/person/find%22, method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)%0A%09public List%3CPerson%3E findPerson() %7B%0A%09%09return peopleService.findPerson(%22%22);%0A%09%7D%0A%0A
 %09@RequestMapping

Add find methods with no path variables