package uk.ac.ebi.atlas.experimentpage.baseline.genedistribution;

import uk.ac.ebi.atlas.utils.CsvReaderFactory;
import uk.ac.ebi.atlas.utils.KryoReaderFactory;
import au.com.bytecode.opencsv.CSVReader;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import uk.ac.ebi.atlas.commons.streams.ObjectInputStream;
import uk.ac.ebi.atlas.model.Experiment;
import uk.ac.ebi.atlas.profiles.BaselineExpressionsKryoReader;
import uk.ac.ebi.atlas.profiles.baseline.ExpressionsRowDeserializerBaselineBuilder;
import uk.ac.ebi.atlas.profiles.baseline.ExpressionsRowDeserializerProteomicsBaselineBuilder;
import uk.ac.ebi.atlas.profiles.baseline.ExpressionsRowRawDeserializerBaselineBuilder;
import uk.ac.ebi.atlas.trader.ExperimentTrader;

import javax.inject.Inject;
import javax.inject.Named;
import java.text.MessageFormat;

@Named
@Scope("prototype")
public class BaselineExpressionsInputStreamFactory {

    @Value("#{configuration['experiment.magetab.path.template']}")
    private String baselineExperimentDataFileUrlTemplate;

    @Value("#{configuration['experiment.kryo_expressions.path.template']}")
    private String baselineExperimentSerializedDataFileUrlTemplate;

    private ExpressionsRowRawDeserializerBaselineBuilder expressionsRowRawDeserializerBaselineBuilder;
    private ExpressionsRowDeserializerBaselineBuilder expressionsRowDeserializerBaselineBuilder;
    private ExpressionsRowDeserializerProteomicsBaselineBuilder expressionsRowDeserializerProteomicsBaselineBuilder;
    private CsvReaderFactory csvReaderFactory;
    private KryoReaderFactory kryoReaderFactory;
    private ExperimentTrader experimentTrader;

    private BarChartExperimentAccessKeyTrader barChartExperimentAccessKeyTrader;

    @Inject
    public BaselineExpressionsInputStreamFactory(ExpressionsRowRawDeserializerBaselineBuilder expressionsRowRawDeserializerBaselineBuilder,
                                                 ExpressionsRowDeserializerBaselineBuilder expressionsRowDeserializerBaselineBuilder,
                                                 ExpressionsRowDeserializerProteomicsBaselineBuilder expressionsRowDeserializerProteomicsBaselineBuilder,
                                                 CsvReaderFactory csvReaderFactory,
                                                 KryoReaderFactory kryoReaderFactory,
                                                 ExperimentTrader experimentTrader,
                                                 BarChartExperimentAccessKeyTrader barChartExperimentAccessKeyTrader) {
        this.expressionsRowRawDeserializerBaselineBuilder = expressionsRowRawDeserializerBaselineBuilder;
        this.expressionsRowDeserializerBaselineBuilder = expressionsRowDeserializerBaselineBuilder;
        this.expressionsRowDeserializerProteomicsBaselineBuilder = expressionsRowDeserializerProteomicsBaselineBuilder;
        this.csvReaderFactory = csvReaderFactory;
        this.kryoReaderFactory = kryoReaderFactory;
        this.experimentTrader = experimentTrader;
        this.barChartExperimentAccessKeyTrader = barChartExperimentAccessKeyTrader;
    }

    public ObjectInputStream<BaselineExpressions> createGeneExpressionsInputStream(String experimentAccession) {

        String accessKey = barChartExperimentAccessKeyTrader.getAccessKey(experimentAccession);

        Experiment experiment = experimentTrader.getExperiment(experimentAccession, accessKey);

        if(experiment.getType().isProteomicsBaseline()) {
            String tsvFileURL = MessageFormat.format(baselineExperimentDataFileUrlTemplate, experimentAccession);
            CSVReader csvReader = csvReaderFactory.createTsvReader(tsvFileURL);
            return new BaselineExpressionsTsvInputStream(csvReader, experimentAccession, expressionsRowDeserializerProteomicsBaselineBuilder);
        }
        else {
            String serializedFileURL = MessageFormat.format(baselineExperimentSerializedDataFileUrlTemplate, experimentAccession);
            try {
                BaselineExpressionsKryoReader kryoReader = kryoReaderFactory.createBaselineExpressionsKryoReader(serializedFileURL);
                return new BaselineExpressionsKryoInputStream(kryoReader, experimentAccession, expressionsRowRawDeserializerBaselineBuilder);
            } catch (IllegalArgumentException e) {
                String tsvFileURL = MessageFormat.format(baselineExperimentDataFileUrlTemplate, experimentAccession);
                CSVReader csvReader = csvReaderFactory.createTsvReader(tsvFileURL);
                return new BaselineExpressionsTsvInputStream(csvReader, experimentAccession, expressionsRowDeserializerBaselineBuilder);
            }
        }
    }

}

@@ -60,16 +60,61 @@
 ution;%0A%0A
+import uk.ac.ebi.atlas.resource.DataFileHub;%0A
 import u
@@ -148,24 +148,24 @@
 derFactory;%0A
-
 import uk.ac
@@ -204,50 +204,8 @@
 ry;%0A
-import au.com.bytecode.opencsv.CSVReader;%0A
 impo
@@ -961,134 +961,8 @@
  %7B%0A%0A
-    @Value(%22#%7Bconfiguration%5B'experiment.magetab.path.template'%5D%7D%22)%0A    private String baselineExperimentDataFileUrlTemplate;%0A%0A
     
@@ -1423,55 +1423,8 @@
 er;%0A
-    private CsvReaderFactory csvReaderFactory;%0A
     
@@ -1510,24 +1510,67 @@
 imentTrader;
+%0A    private final DataFileHub dataFileHub;
 %0A%0A    privat
@@ -2085,92 +2085,8 @@
 er,%0A
-                                                 CsvReaderFactory csvReaderFactory,%0A
     
@@ -2367,16 +2367,90 @@
 eyTrader
+,%0A                                                 DataFileHub dataFileHub
 ) %7B%0A    
@@ -2775,58 +2775,8 @@
 er;%0A
-        this.csvReaderFactory = csvReaderFactory;%0A
     
@@ -2953,24 +2953,64 @@
 sKeyTrader;%0A
+        this.dataFileHub = dataFileHub;%0A
     %7D%0A%0A    p
@@ -3386,257 +3386,136 @@
     
-String tsvFileURL = MessageFormat.format(baselineExperimentDataFileUrlTemplate, experimentAccession);%0A            CSVReader csvReader = csvReaderFactory.createTsvReader(tsvFileURL);%0A            return new BaselineExpressionsTsvInputStream(csvReader,
+return new BaselineExpressionsTsvInputStream(dataFileHub.getExperimentFiles(experimentAccession).main.get(),%0A                   
  exp
@@ -3523,32 +3523,52 @@
 rimentAccession,
+%0A                   
  expressionsRowD
@@ -4126,264 +4126,115 @@
     
-String tsvFileURL = MessageFormat.format(baselineExperimentDataFileUrlTemplate, experimentAccession);%0A                CSVReader csvReader = csvReaderFactory.createTsvReader(tsvFileURL);%0A                return new BaselineExpressionsTsvInputStream(csvReader
+return new BaselineExpressionsTsvInputStream(dataFileHub.getExperimentFiles(experimentAccession).main.get()
 , ex

Fix json barchart endpoint