package com.revature.controllers;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyEmitterReturnValueHandler;

import com.revature.domain.Batch;
import com.revature.domain.Person;
import com.revature.domain.PersonRole;
import com.revature.services.BatchLogic;
import com.revature.services.PersonLogic;
import com.revature.services.PersonRoleLogic;

@CrossOrigin(origins = "http://localhost:8080/api/v1/persons", maxAge = 3600)
@RestController
@RequestMapping(value = "/api/v1/")
public class PersonController {
	
	@Autowired
	private PersonLogic personLogic;
	
	@Autowired
	private PersonRoleLogic personRoleLogic;
	
	@Autowired
	private BatchLogic batchLogic;
	
	
	@RequestMapping(method = RequestMethod.GET, value = "persons/{id}")
		public ResponseEntity<Person> getPersonById(@PathVariable("id") Integer id ){

			Person person = personLogic.getPersonById(id);
		
			
			if (person != null) {
				
				return ResponseEntity.ok(person);
				
			} else {
				
				System.out.println("ERROR!");
				return new ResponseEntity<>(HttpStatus.NOT_FOUND);
			}
			
			
	}
		
	
	@RequestMapping(method = RequestMethod.GET, value = "persons")
	public ResponseEntity<Page<Person>> getPerson(Pageable pageable, @RequestParam(defaultValue="", required=false) String firstname,
			@RequestParam(defaultValue="", required=false) String lastname,
			@RequestParam(defaultValue="0", required=false) Integer role){
		
		 if(role==0){
			 			 
			 if(!"".equals(firstname)){
				 return ResponseEntity.ok(personLogic.getPersonByFirstName(pageable, firstname));
				 
			 } else if (!("".equals(lastname))){
				 return ResponseEntity.ok(personLogic.getPersonByLastName(pageable, lastname));
				 
			 } else{
				 return  ResponseEntity.ok(personLogic.getAllPersons(pageable));
			 }
			 
		 } else {
			 if(role==1){
				
					return ResponseEntity.ok(personLogic.getAllTrainees(pageable));
					
					} else if (role == 2) {
						return ResponseEntity.ok(personLogic.getAllTrainers(pageable));
						
					} else {
						return ResponseEntity.ok(null);
					}
		 }
		 
	}
	
	
	@RequestMapping(method = RequestMethod.POST, value = "persons")
	public ResponseEntity<Person> createPerson(@RequestBody Person newPerson){
		System.out.println(newPerson);	
		personLogic.createPerson(newPerson);
		return ResponseEntity.ok(newPerson);
	}
	

	
	@RequestMapping(method = RequestMethod.PUT, value = "persons")
	public ResponseEntity<Person> modifyPerson(@RequestBody Person p){
	
		Person person = personLogic.updatePerson(p);
		return ResponseEntity.ok(person);

		
	}
	
	@RequestMapping(method = RequestMethod.DELETE, value = "persons")
	public ResponseEntity<String> deletePerson(int pId){
		
			Person pers = personLogic.getPersonById(pId);
			String message = pers.getFirstName() + " " + pers.getLastName() + " was deleted.";
			personLogic.deletePerson(pers);
			return ResponseEntity.ok(message);
	}
	
	
	

}

@@ -1259,111 +1259,8 @@
 gic;
-%0A%09%0A%09@Autowired%0A%09private PersonRoleLogic personRoleLogic;%0A%09%0A%09@Autowired%0A%09private BatchLogic batchLogic;%0A
 %09%0A%09%0A
@@ -1589,16 +1589,20 @@
 RROR!%22);
+%09%09%09%09
 %0A%09%09%09%09ret
@@ -3050,17 +3050,29 @@
  Person 
-p
+updatedPerson
 )%7B%0A%09%0A%09%09P
@@ -3102,25 +3102,120 @@
 gic.
+getPersonById(
 update
+d
 Person
-(p
+.getId());%0A%09%09%0A%09%09if(person != null) %7B%09%09%09%0A%09%09%09personLogic.updatePerson(updatedPerson
 );%0A
+%09
 %09%09re
@@ -3237,24 +3237,116 @@
 tity.ok(
-person);
+updatedPerson);%09%09%09%0A%09%09%7D else %7B%09%09%09%0A%09%09%09return new ResponseEntity%3C%3E(HttpStatus.BAD_REQUEST);%0A%09%09%7D%0A%09%09%09%09%0A%09%09
 %0A%0A%09%09%0A%09%7D%0A
@@ -3469,17 +3469,16 @@
  pId)%7B%0A%09
-%09
 %0A%09%09%09Pers
@@ -3522,16 +3522,80 @@
 Id);%0A%09%09%09
+%0A%09%09%09if (pers != null) %7B%0A%09%09%09%09personLogic.deletePerson(pers);%0A%09%09%09%09
 String m
@@ -3676,40 +3676,57 @@
 %0A%09%09%09
-personLogic.deletePerson(pers);%0A
+%09return ResponseEntity.ok(message);%0A%09%09%09%7D else %7B%0A%09
 %09%09%09r
@@ -3723,32 +3723,36 @@
 se %7B%0A%09%09%09%09return 
+new 
 ResponseEntity.o
@@ -3749,29 +3749,60 @@
 seEntity
-.ok(message);
+%3C%3E(HttpStatus.BAD_REQUEST);%0A%09%09%09%7D%0A%09%09%09%0A%09%09%09%0A%09%09%09
 %0A%09%7D%0A%09%0A%09%0A

add Rest exception handling in PersonController