package uk.ac.ebi.atlas.experimentpage.qc;

import com.google.common.base.Function;
import com.google.common.base.Functions;
import com.google.common.collect.FluentIterable;
import org.apache.commons.lang3.tuple.Pair;
import uk.ac.ebi.atlas.commons.readers.TsvReader;
import uk.ac.ebi.atlas.experimentpage.differential.download.CanStreamSupplier;
import uk.ac.ebi.atlas.model.download.ExternallyAvailableContent;
import uk.ac.ebi.atlas.model.experiment.differential.DifferentialExperiment;
import uk.ac.ebi.atlas.model.resource.AtlasResource;
import uk.ac.ebi.atlas.resource.DataFileHub;

import javax.annotation.Nullable;
import javax.inject.Inject;
import javax.inject.Named;
import java.io.Writer;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;

@Named
public class RnaSeqQCReport extends CanStreamSupplier<DifferentialExperiment> {

    @Override
    public ExternallyAvailableContent.ContentType contentType() {
        return ExternallyAvailableContent.ContentType.SUPPLEMENTARY_INFORMATION;
    }

    private final DataFileHub dataFileHub;

    @Inject
    public RnaSeqQCReport(DataFileHub dataFileHub){
        this.dataFileHub = dataFileHub;
    }

    @Override
    public Collection<ExternallyAvailableContent> get(DifferentialExperiment experiment) {

        List<Pair<String, Function<Writer, Void>>> documents = FluentIterable.from(
                new RnaSeqQCFiles(dataFileHub.getExperimentFiles(experiment.getAccession()).qcFolder).get().entrySet()).transform(
                new Function<Map.Entry<String,AtlasResource<TsvReader>>, Pair<String, Function<Writer, Void>>>() {
                    @Nullable
                    @Override
                    public Pair<String, Function<Writer, Void>> apply(@Nullable Map.Entry<String, AtlasResource<TsvReader>> stringAtlasResourceEntry) {
                        return Pair.of(
                                stringAtlasResourceEntry.getKey(),
                                readFromResourceAndWriteTsv(stringAtlasResourceEntry.getValue(), Functions.<String[]>identity())
                        );
                    }
                }).toList();


        if(! documents.isEmpty()){
            return Collections.singleton(new ExternallyAvailableContent(
                    makeUri("qc"),
                    ExternallyAvailableContent.Description.create("link",
                            "Quality check report (generated by iRAP)"
                    ),
                    documents.size() == 1 ? streamFile(documents.get(0)) : streamFolder(experiment.getAccession()+"-qc", documents)
            ));
        } else {
            return Collections.emptySet();
        }
    }
}

@@ -41,138 +41,8 @@
 c;%0A%0A
-import com.google.common.base.Function;%0Aimport com.google.common.base.Functions;%0Aimport com.google.common.collect.FluentIterable;%0A
 impo
@@ -81,16 +81,16 @@
 e.Pair;%0A
+
 import u
@@ -667,16 +667,88 @@
 til.Map;
+%0Aimport java.util.function.Function;%0Aimport java.util.stream.Collectors;
 %0A%0A@Named
@@ -1324,45 +1324,8 @@
 ts =
- FluentIterable.from(%0A               
  new
@@ -1427,41 +1427,45 @@
 et()
-).transform(%0A                new 
+.stream().map(new java.util.function.
 Func
@@ -1486,16 +1486,17 @@
 %3CString,
+ 
 AtlasRes
@@ -1519,32 +1519,51 @@
 %3E, Pair%3CString, 
+java.util.function.
 Function%3CWriter,
@@ -1571,32 +1571,24 @@
 Void%3E%3E%3E() %7B%0A
-        
             
@@ -1609,24 +1609,16 @@
         
-        
 @Overrid
@@ -1619,24 +1619,16 @@
 verride%0A
-        
         
@@ -1643,32 +1643,51 @@
 ic Pair%3CString, 
+java.util.function.
 Function%3CWriter,
@@ -1794,32 +1794,24 @@
             
-        
 return Pair.
@@ -1814,24 +1814,16 @@
 air.of(%0A
-        
         
@@ -1897,24 +1897,16 @@
         
-      
-  
 readFrom
@@ -1974,20 +1974,9 @@
 tion
-s.%3CString%5B%5D%3E
+.
 iden
@@ -2003,27 +2003,11 @@
     
-   
-     );%0A        
+);%0A
     
@@ -2028,18 +2028,29 @@
     
-        %7D)
+%7D).collect(Collectors
 .toL
@@ -2054,16 +2054,17 @@
 toList()
+)
 ;%0A%0A%0A    

Fix return values to match Java 8 Function