package uk.ac.ebi.biosamples.curation;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.Sample;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.SortedSet;
import java.util.concurrent.Callable;

public class SampleCuramiCallable implements Callable<Void> {
    private static final Logger LOG = LoggerFactory.getLogger(CuramiApplicationRunner.class);
    private final Sample sample;
    private final BioSamplesClient bioSamplesClient;
    private final String domain;
    private final Map<String, String> curationRules;

    public SampleCuramiCallable(BioSamplesClient bioSamplesClient, Sample sample, String domain,
                                Map<String, String> curationRules) {
        this.bioSamplesClient = bioSamplesClient;
        this.sample = sample;
        this.domain = domain;
        this.curationRules = curationRules;
    }

    @Override
    public Void call() {
        getRuleBasedCurations(sample);
        return null;
    }

    private List<Curation> getRuleBasedCurations(Sample sample) {
        SortedSet<Attribute> attributes = sample.getAttributes();
        List<Curation> curations = new ArrayList<>();
        for (Attribute a : attributes) {
            if (curationRules.containsKey(a.getType())) {
                Curation curation = Curation.build(
                        Attribute.build(a.getType(), a.getValue(), a.getIri(), a.getUnit()),
                        Attribute.build(curationRules.get(a.getType()), a.getValue(), a.getIri(), a.getUnit()));
                bioSamplesClient.persistCuration(sample.getAccession(), curation, domain);
                LOG.info("New curation found {}", curation);
                curations.add(curation);
            }
        }

        return curations;
    }
}

@@ -1117,16 +1117,43 @@
 %0A       
+ List%3CCuration%3E curations =
  getRule
@@ -1176,16 +1176,111 @@
 ample);%0A
+        LOG.info(%22%7B%7D curations added for sample %7B%7D%22, curations.size(), sample.getAccession());%0A
         
@@ -1522,24 +1522,98 @@
 tributes) %7B%0A
+            String processedAttribute = getCleanedAttribute(a.getType());%0A
             
@@ -1642,27 +1642,34 @@
 ainsKey(
-a.getType()
+processedAttribute
 )) %7B%0A   
@@ -1868,27 +1868,34 @@
 les.get(
-a.getType()
+processedAttribute
 ), a.get
@@ -2144,24 +2144,24 @@
 %0A        %7D%0A%0A
-
         retu
@@ -2179,11 +2179,297 @@
 s;%0A    %7D
+%0A%0A    //todo add further processing identified in the analysis%0A    private String getCleanedAttribute(String attribute) %7B%0A        String cleanedAttribute = attribute.toLowerCase();%0A        cleanedAttribute = cleanedAttribute.replaceAll(%22-%22, %22 %22);%0A%0A        return cleanedAttribute;%0A    %7D
 %0A%7D%0A

add attribute preprocessing method