package uk.ac.ebi.biosamples.ena;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.Callable;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.hateoas.Resource;

import uk.ac.ebi.biosamples.client.BioSamplesClient;
import uk.ac.ebi.biosamples.model.Attribute;
import uk.ac.ebi.biosamples.model.Curation;
import uk.ac.ebi.biosamples.model.ExternalReference;
import uk.ac.ebi.biosamples.model.Sample;

public class NcbiCurationCallable implements Callable<Void> {
    private static final String SUPPRESSED = "suppressed";

    private Logger log = LoggerFactory.getLogger(getClass());

    private final String accession;
    private final BioSamplesClient bioSamplesClient;
    private final String domain;
    private boolean suppressionHandler;

    public NcbiCurationCallable(String accession, BioSamplesClient bioSamplesClient, String domain) {
        this.accession = accession;
        this.bioSamplesClient = bioSamplesClient;
        this.domain = domain;
    }

    /**
     * Construction for SUPPRESSED samples
     *
     * @param accession
     * @param bioSamplesClient
     * @param domain
     * @param suppressionHandler
     */
    public NcbiCurationCallable(String accession, BioSamplesClient bioSamplesClient, String domain,
                                boolean suppressionHandler) {
        this.accession = accession;
        this.bioSamplesClient = bioSamplesClient;
        this.domain = domain;
        this.suppressionHandler = suppressionHandler;
    }

    @Override
    public Void call() throws Exception {
        log.trace("HANDLING " + this.accession);
        ExternalReference exRef = ExternalReference.build("https://www.ebi.ac.uk/ena/data/view/" + this.accession);
        Curation curation = Curation.build(null, null, null, Collections.singleton(exRef));

        if (suppressionHandler) {
            checkAndUpdateSuppressedSample();
        } else {
            // get the sample to make sure it exists first
            if (bioSamplesClient.fetchSampleResource(this.accession, Optional.of(new ArrayList<String>())).isPresent()) {
                bioSamplesClient.persistCuration(this.accession, curation, domain);
            } else {
                log.warn("Unable to find " + this.accession);
            }
        }

        log.trace("HANDLED " + this.accession);

        return null;
    }

    /**
     * Checks if sample status is not SUPPRESSED in BioSamples, if yes then persists the sample with SUPPRESSED status
     */
    private void checkAndUpdateSuppressedSample() {
        final List<String> curationDomainBlankList = new ArrayList<>();
        curationDomainBlankList.add("");

        final Optional<Resource<Sample>> optionalSampleResource = bioSamplesClient.fetchSampleResource(this.accession, Optional.of(curationDomainBlankList));

        if (optionalSampleResource.isPresent()) {
            final Sample sample = optionalSampleResource.get().getContent();
            boolean persistRequired = true;

            for (Attribute attribute : sample.getAttributes()) {
                if (attribute.getType().equals("INSDC status") && attribute.getValue().equals(SUPPRESSED)) {
                    persistRequired = false;
                    break;
                }
            }

            if (persistRequired) {
                sample.getAttributes().removeIf(attr -> attr.getType().contains("INSDC status"));
                sample.getAttributes().add(Attribute.build("INSDC status", SUPPRESSED));
                log.info("Updating status to suppressed of sample: " + this.accession);
                bioSamplesClient.persistSampleResource(sample);
            }
        }
    }
}

@@ -1913,24 +1913,42 @@
 n(exRef));%0A%0A
+        try %7B%0A    
         if (
@@ -1961,32 +1961,36 @@
 ssionHandler) %7B%0A
+    
             chec
@@ -2019,32 +2019,36 @@
 mple();%0A        
+    
 %7D else %7B%0A       
@@ -2052,16 +2052,20 @@
         
+ 
+   
 // get t
@@ -2103,16 +2103,20 @@
 s first%0A
+    
         
@@ -2202,22 +2202,16 @@
 rayList%3C
-String
 %3E())).is
@@ -2231,32 +2231,36 @@
                 
+    
 bioSamplesClient
@@ -2315,32 +2315,36 @@
 n);%0A            
+   
+ 
 %7D else %7B%0A       
@@ -2328,32 +2328,36 @@
        %7D else %7B%0A
+    
                 
@@ -2414,26 +2414,33 @@
         
+ 
-%7D%0A
+   %7D%0A   
         
 %7D%0A%0A     
@@ -2423,35 +2423,40 @@
    %7D%0A           
+ 
 %7D%0A%0A
+    
         log.trac
@@ -2486,16 +2486,145 @@
 ession);
+%0A        %7D catch (final Exception e) %7B%0A            log.info(%22Failed to curate NCBI sample with ENA link %22 + accession);%0A        %7D
 %0A%0A      
@@ -2942,24 +2942,42 @@
 t.add(%22%22);%0A%0A
+        try %7B%0A    
         fina
@@ -3127,24 +3127,28 @@
 );%0A%0A        
+    
 if (optional
@@ -3169,32 +3169,36 @@
 .isPresent()) %7B%0A
+    
             fina
@@ -3254,24 +3254,28 @@
 tContent();%0A
+    
             
@@ -3319,16 +3319,20 @@
         
+    
 for (Att
@@ -3372,24 +3372,28 @@
 ibutes()) %7B%0A
+    
             
@@ -3509,16 +3509,20 @@
         
+    
 persistR
@@ -3562,15 +3562,23 @@
     
+ 
+   
 break;%0A
+    
     
@@ -3595,32 +3595,36 @@
   %7D%0A            
+    
 %7D%0A%0A            i
@@ -3618,24 +3618,28 @@
             
+  
+  
 if (persistR
@@ -3645,24 +3645,28 @@
 Required) %7B%0A
+    
             
@@ -3759,32 +3759,36 @@
                 
+    
 sample.getAttrib
@@ -3852,32 +3852,36 @@
                 
+   
+ 
 log.info(%22Updati
@@ -3944,32 +3944,36 @@
                 
+    
 bioSamplesClient
@@ -4008,33 +4008,184 @@
 e);%0A            
-%7D
+    %7D%0A            %7D%0A        %7D catch (final Exception e) %7B%0A            log.error(%22Failed to update sample status to suppressed for sample %22 + accession);
 %0A        %7D%0A    %7D

Fix bug while suppression of existing suppressed ENA samples