package org.endeavourhealth.core.fhirStorage.statistics;

import org.endeavourhealth.core.data.ehr.ResourceMetadataIterator;
import org.endeavourhealth.core.data.ehr.ResourceRepository;
import org.endeavourhealth.core.fhirStorage.metadata.PatientMetadata;

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

public class StorageStatisticsService {
    private static final String PATIENT_RESOURCE_TYPE_NAME = "Patient";

    private final ResourceRepository repository;

    public StorageStatisticsService() {
        this.repository = new ResourceRepository();
    }

    public PatientStatistics getPatientStatistics(UUID serviceId, UUID systemId) {
        return createPatientStatistics(serviceId, systemId);
    }

    public List<ResourceStatistics> getResourceStatistics(UUID serviceId, UUID systemId, List<String> resourceTypes) {
        List<ResourceStatistics> results = new ArrayList<>();

        for (String resourceType :resourceTypes) {
            results.add(createResourceStatistics(serviceId, systemId, resourceType));
        }

        return results;
    }

    private PatientStatistics createPatientStatistics(UUID serviceId, UUID systemId) {
        long totalCount = 0;
        long activeCount = 0;
        long deceasedCount = 0;

        ResourceMetadataIterator<PatientMetadata> patientMetadataIterator = repository.getMetadataByService(serviceId,
                systemId,
                PATIENT_RESOURCE_TYPE_NAME,
                PatientMetadata.class);

        while(patientMetadataIterator.hasNext()) {
            PatientMetadata patientMetadata = patientMetadataIterator.next();

            if (patientMetadata.isDeceased()) {
                deceasedCount++;
            }
            else if (patientMetadata.isActive()) {
                activeCount++;
            }
            totalCount++;
        }

        PatientStatistics statistics = new PatientStatistics();
        statistics.setTotalCount(totalCount);
        statistics.setActiveCount(activeCount);
        statistics.setDeceasedCount(deceasedCount);
        return statistics;
    }

    private ResourceStatistics createResourceStatistics(UUID serviceId, UUID systemId, String resourceType) {
        ResourceStatistics statistics = new ResourceStatistics(resourceType);
        statistics.setTotalCount(repository.getResourceCountByService(serviceId, systemId, resourceType));
        return statistics;
    }
}

@@ -1634,24 +1634,71 @@
 or.next();%0A%0A
+            if (patientMetadata != null) %7B%0A    
             
@@ -1729,24 +1729,28 @@
 ceased()) %7B%0A
+    
             
@@ -1774,34 +1774,42 @@
 ++;%0A            
+   
+ 
 %7D%0A
+    
             else
@@ -1859,16 +1859,20 @@
         
+    
 activeCo
@@ -1882,34 +1882,42 @@
 ++;%0A            
+  
-%7D%0A
+  %7D%0A    
             tota
@@ -1918,32 +1918,47 @@
   totalCount++;%0A
+            %7D%0A%0A
         %7D%0A%0A     

fix to stats module