package com.projectbolek.web;

import com.projectbolek.domain.entity.ContactDetailsEntity;
import com.projectbolek.domain.entity.PatientEntity;
import com.projectbolek.domain.model.dto.PatientDTO;
import com.projectbolek.domain.model.exception.ApplicationException;
import com.projectbolek.service.PatientService;
import com.projectbolek.service.converter.PatientConverter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.List;

/**
 * Created by rogalsp1 on 30.05.16.
 */
@RestController
@RequestMapping(path = "/patients/")
@Slf4j
public class PatientController implements Serializable{

    private static final long serialVersionUID = -3606314241856173634L;

    private PatientService patientService;
    private PatientConverter converter;

    @Autowired
    public PatientController(PatientService patientService, PatientConverter patientConverter) {
        this.patientService = patientService;
        this.converter = patientConverter;
    }

    @RequestMapping(path = "/active", method = RequestMethod.GET)
    public List<PatientDTO> getActivePatients(){
        List<PatientEntity> patientList = patientService.findActivePatients();
        return converter.fromEntity(patientList);
    }

    @RequestMapping(path = "/new", method = RequestMethod.POST)
    public ResponseEntity<?> addNewPatient(@RequestBody PatientDTO patientDTO){
        PatientEntity patient = converter.fromDTO(patientDTO);
        try {
            patientService.addNewPatient(patient);
        } catch (ApplicationException e) {
            return new ResponseEntity<Object>(e.getMessage(),HttpStatus.INTERNAL_SERVER_ERROR);
        }
        return new ResponseEntity<Object>(null, HttpStatus.OK);
    }

    @RequestMapping(path = "/deactivate", method = RequestMethod.POST)
    public ResponseEntity<?> deactivatePatient(@RequestParam("id") Long patientId){
        patientService.deactivatePatient(patientId);
        return new ResponseEntity<Object>(null,HttpStatus.OK);
    }

    @RequestMapping(path = "/contactdetails", method = RequestMethod.GET)
    public ContactDetailsEntity getContactDetails(@RequestParam("id") Long patientId){
        return patientService.findContactDetailsByPatientId(patientId);
    }

    @RequestMapping(path = "/update", method = RequestMethod.POST)
    public ResponseEntity<?> updatePatient(@RequestBody PatientDTO patientDTO){
        PatientEntity patient = converter.fromDTO(patientDTO);
        patientService.save(patient);
        return new ResponseEntity<Object>(null, HttpStatus.OK);
    }

    @RequestMapping(path= "/saveContact", method = RequestMethod.POST)
    public ResponseEntity<?> saveContactDetails(@RequestBody ContactDetailsEntity contactDetailsEntity, @RequestParam("patient_id") Long id){
        patientService.saveContactDetails(contactDetailsEntity,id);
        return new ResponseEntity<Object>(null, HttpStatus.OK);
    }
}

@@ -736,23 +736,16 @@
 Mapping(
-path = 
 %22/patien
@@ -746,17 +746,16 @@
 patients
-/
 %22)%0A@Slf4
@@ -810,16 +810,17 @@
 alizable
+ 
 %7B%0A%0A    p
@@ -1292,16 +1292,17 @@
 tients()
+ 
 %7B%0A      
@@ -1455,23 +1455,8 @@
 ing(
-path = %22/new%22, 
 meth
@@ -1550,32 +1550,33 @@
 tDTO patientDTO)
+ 
 %7B%0A        Patien
@@ -1793,16 +1793,17 @@
 ssage(),
+ 
 HttpStat
@@ -1934,30 +1934,8 @@
 ing(
-path = %22/deactivate%22, 
 meth
@@ -1946,34 +1946,33 @@
  RequestMethod.P
-OS
+U
 T)%0A    public Re
@@ -1987,23 +1987,19 @@
 tity%3C?%3E 
-deactiv
+upd
 atePatie
@@ -2013,32 +2013,31 @@
 uest
-Param(%22id%22) Long
+Body PatientDTO
  patient
 Id)%7B
@@ -2024,35 +2024,37 @@
 tientDTO patient
-Id)
+DTO) 
 %7B%0A        patien
@@ -2051,48 +2051,98 @@
     
-patientService.deactivatePatient
+PatientEntity patient = converter.fromDTO(patientDTO);%0A        patientService.save
 (patient
 Id);
@@ -2133,26 +2133,24 @@
 save(patient
-Id
 );%0A        r
@@ -2187,16 +2187,17 @@
 t%3E(null,
+ 
 HttpStat
@@ -2244,22 +2244,30 @@
 = %22/
-contactdetails
+%7BpatientId%7D/deactivate
 %22, m
@@ -2280,34 +2280,35 @@
 = RequestMethod.
-GE
+POS
 T)%0A    public Co
@@ -2309,66 +2309,57 @@
 lic 
-ContactDetailsEntity getContactDetails(@RequestParam(%22id%22)
+ResponseEntity%3C?%3E deactivatePatient(@PathVariable
  Lon
@@ -2370,16 +2370,17 @@
 tientId)
+ 
 %7B%0A      
@@ -2381,23 +2381,16 @@
         
-return 
 patientS
@@ -2400,37 +2400,25 @@
 ice.
-findContactDetailsBy
+deactivate
 Patient
-Id
 (pat
@@ -2422,24 +2422,88 @@
 patientId);%0A
+        return new ResponseEntity%3CObject%3E(null, HttpStatus.OK);%0A
     %7D%0A%0A    @
@@ -2526,22 +2526,42 @@
 ath = %22/
-update
+%7BpatientId%7D/contactdetails
 %22, metho
@@ -2570,35 +2570,34 @@
 = RequestMethod.
-POS
+GE
 T)%0A    public Re
@@ -2598,71 +2598,73 @@
 lic 
-ResponseEntity%3C?%3E updatePatient(@RequestBody PatientDTO
+ContactDetailsEntity getContactDetails(@PathVariable Long
  patient
 DTO)
@@ -2659,20 +2659,20 @@
  patient
-DTO)
+Id) 
 %7B%0A      
@@ -2677,162 +2677,69 @@
     
-PatientEntity patient = converter.fromDTO(patientDTO);%0A        patientService.save(patient);%0A        return new ResponseEntity%3CObject%3E(null, HttpStatus.OK
+return patientService.findContactDetailsByPatientId(patientId
 );%0A 
@@ -2772,23 +2772,39 @@
 path
+ 
 = %22/
-saveContact
+%7BpatientId%7D/contactdetails
 %22, m
@@ -2940,43 +2940,37 @@
 y, @
-RequestParam(%22patient_id%22) Long id)
+PathVariable Long patientId) 
 %7B%0A  
@@ -3030,17 +3030,25 @@
 sEntity,
-i
+ patientI
 d);%0A    

Change paths to proper REST paths in patient controller