package org.openmrs.projectbuendia.webservices.rest;

import org.openmrs.Patient;
import org.openmrs.api.PatientService;
import org.openmrs.api.context.Context;
import org.openmrs.module.webservices.rest.SimpleObject;
import org.openmrs.module.webservices.rest.web.RequestContext;
import org.openmrs.module.webservices.rest.web.RestConstants;
import org.openmrs.module.webservices.rest.web.annotation.Resource;
import org.openmrs.module.webservices.rest.web.resource.api.Listable;
import org.openmrs.module.webservices.rest.web.resource.api.Searchable;
import org.openmrs.module.webservices.rest.web.response.ResponseException;
import org.projectbuendia.openmrs.webservices.rest.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * Resource for xform templates (i.e. forms without data).
 * Note: this is under org.openmrs as otherwise the resource annotation isn't picked up.
 */
@Resource(name = RestController.REST_VERSION_1_AND_NAMESPACE + "/patient", supportedClass = Patient.class, supportedOpenmrsVersions = "1.10.*")
public class PatientResource implements Listable, Searchable {

    private final PatientService patientService;

    public PatientResource() {
        patientService = Context.getPatientService();
    }

    @Override
    public SimpleObject getAll(RequestContext requestContext) throws ResponseException {
        List<SimpleObject> jsonResults = new ArrayList<>();
        List<Patient> patients = patientService.getAllPatients();
        for (Patient patient : patients) {
            SimpleObject jsonForm = new SimpleObject();
            jsonForm.add("id", patient.getUuid() /*TODO(nfortescue): patient.getPatientIdentifier().getIdentifier()*/);
            jsonForm.add("given_name", patient.getGivenName());
            jsonForm.add("family_name", patient.getFamilyName());
            jsonForm.add("status", "probable" /* TODO(nfortescue): work out how to store this */);
            jsonForm.add("gender", patient.getGender());
            jsonResults.add(jsonForm);
        }
        SimpleObject list = new SimpleObject();
        list.add("results", jsonResults);
        return list;
    }

    @Override
    public String getUri(Object instance) {
        Patient patient = (Patient) instance;
        Resource res = getClass().getAnnotation(Resource.class);
        return RestConstants.URI_PREFIX + res.name() + "/" + patient.getUuid();
    }

    @Override
    public SimpleObject search(RequestContext requestContext) throws ResponseException {
        throw new UnsupportedOperationException();
    }
}

@@ -457,23 +457,174 @@
 b.re
-source.api.List
+presentation.Representation;%0Aimport org.openmrs.module.webservices.rest.web.resource.api.Listable;%0Aimport org.openmrs.module.webservices.rest.web.resource.api.Retriev
 able
@@ -868,16 +868,41 @@
 ayList;%0A
+import java.util.Arrays;%0A
 import j
@@ -1277,16 +1277,29 @@
 archable
+, Retrievable
  %7B%0A%0A    
@@ -1744,16 +1744,297 @@
 nForm = 
+patientToJson(patient);%0A            jsonResults.add(jsonForm);%0A        %7D%0A        SimpleObject list = new SimpleObject();%0A        list.add(%22results%22, jsonResults);%0A        return list;%0A    %7D%0A%0A    private SimpleObject patientToJson(Patient patient) %7B%0A        SimpleObject jsonForm = 
 new Simp
@@ -2041,28 +2041,24 @@
 leObject();%0A
-    
         json
@@ -2161,36 +2161,32 @@
 r()*/);%0A        
- 
-   
 jsonForm.add(%22gi
@@ -2213,36 +2213,32 @@
 etGivenName());%0A
-    
         jsonForm
@@ -2283,36 +2283,32 @@
 ame());%0A        
-    
 jsonForm.add(%22st
@@ -2382,28 +2382,24 @@
 /);%0A        
-  
-  
 jsonForm.add
@@ -2443,136 +2443,80 @@
     
-    jsonResults.add(jsonForm);%0A        %7D%0A        SimpleObject list = new SimpleObject();%0A        list.add(%22results%22, jsonResults
+jsonForm.add(%22created_timestamp_utc%22, patient.getDateCreated().getTime()
 );%0A 
@@ -2521,36 +2521,40 @@
 %0A        return 
-list
+jsonForm
 ;%0A    %7D%0A%0A    @Ov
@@ -2961,11 +2961,373 @@
 );%0A    %7D
+%0A%0A    @Override%0A    public Object retrieve(String uuid, RequestContext requestContext) throws ResponseException %7B%0A        Patient patient = patientService.getPatientByUuid(uuid);%0A        return patientToJson(patient);%0A    %7D%0A%0A    @Override%0A    public List%3CRepresentation%3E getAvailableRepresentations() %7B%0A        return Arrays.asList(Representation.DEFAULT);%0A    %7D
 %0A%7D%0A

Add getting an individual patient via UUID to the Patient Resource.