package uk.ac.ebi.quickgo.client.service.loader.presets.evidence;

import uk.ac.ebi.quickgo.client.model.presets.PresetItem;
import uk.ac.ebi.quickgo.client.model.presets.PresetType;
import uk.ac.ebi.quickgo.client.model.presets.impl.CompositePresetImpl;
import uk.ac.ebi.quickgo.client.service.loader.presets.LogStepListener;
import uk.ac.ebi.quickgo.client.service.loader.presets.PresetsCommonConfig;

import java.util.Optional;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.file.FlatFileItemReader;
import org.springframework.batch.item.file.mapping.FieldSetMapper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.core.io.Resource;

import static uk.ac.ebi.quickgo.client.service.loader.presets.PresetsConfig.SKIP_LIMIT;
import static uk.ac.ebi.quickgo.client.service.loader.presets.PresetsConfigHelper.fileReader;
import static uk.ac.ebi.quickgo.client.service.loader.presets.PresetsConfigHelper.rawPresetMultiFileReader;

/**
 * Exposes the {@link Step} bean that is used to read and populate information relating to the evidence preset data.
 *
 * Created 01/09/16
 * @author Edd
 */
@Configuration
@Import({PresetsCommonConfig.class})
public class EvidencePresetsConfig {
    public static final String EVIDENCE_LOADING_STEP_NAME = "EvidenceReadingStep";

    @Value("#{'${evidence.preset.source:}'.split(',')}")
    private Resource[] resources;
    @Value("${evidence.preset.header.lines:1}")
    private int headerLines;

    @Bean
    public Step evidenceStep(
            StepBuilderFactory stepBuilderFactory,
            Integer chunkSize,
            CompositePresetImpl presets) {
        FlatFileItemReader<RawEvidenceNamedPreset> itemReader = fileReader(rawPresetFieldSetMapper());
        itemReader.setLinesToSkip(headerLines);

        return stepBuilderFactory.get(EVIDENCE_LOADING_STEP_NAME)
                .<RawEvidenceNamedPreset, RawEvidenceNamedPreset>chunk(chunkSize)
                .faultTolerant()
                .skipLimit(SKIP_LIMIT)
                .<RawEvidenceNamedPreset>reader(rawPresetMultiFileReader(resources, itemReader, Optional::of))
                .processor(rawPresetValidator())
                .writer(rawPresetWriter(presets))
                .listener(new LogStepListener())
                .build();
    }

    /**
     * Write the list of {@link RawEvidenceNamedPreset}s to the {@link CompositePresetImpl}
     * @param presets the presets to write to
     * @return the corresponding {@link ItemWriter}
     */
    private ItemWriter<RawEvidenceNamedPreset> rawPresetWriter(CompositePresetImpl presets) {
        return rawItemList -> rawItemList.forEach(rawItem ->
            presets.addPreset(PresetType.EVIDENCES,
                    PresetItem.createWithName(rawItem.name)
                            .withProperty(PresetItem.Property.ID.getKey(), rawItem.id)
                            .withRelevancy(rawItem.relevancy)
                            .withProperty(PresetItem.Property.GO_EVIDENCE.getKey(), rawItem
                                    .goEvidence)
                            .build())
        );
    }

    private FieldSetMapper<RawEvidenceNamedPreset> rawPresetFieldSetMapper() {
        return new StringToRawEvidenceNamedPresetMapper(getColumns());
    }

    private ItemProcessor<RawEvidenceNamedPreset, RawEvidenceNamedPreset> rawPresetValidator() {
        return new RawEvidenceNamedPresetValidator();
    }

    private RawEvidenceNamedPresetColumnsBuilder.RawEvidenceNamedPresetColumnsImpl getColumns() {
        return (RawEvidenceNamedPresetColumnsBuilder.RawEvidenceNamedPresetColumnsImpl)
                RawEvidenceNamedPresetColumnsBuilder
                .createWithNamePosition(1)
                .withGoEvidence(2)
                .withIdPosition(0)
                .withRelevancyPosition(3)
                .build();
    }

}

@@ -395,16 +395,161 @@
 nConfig;
+%0Aimport uk.ac.ebi.quickgo.client.service.loader.presets.evidence.RawEvidenceNamedPresetColumnsBuilder%0A        .RawEvidenceNamedPresetColumnsImpl;
 %0A%0Aimport
@@ -3199,16 +3199,20 @@
         
+    
 presets.
@@ -3243,16 +3243,20 @@
 DENCES,%0A
+    
         
@@ -3327,32 +3327,36 @@
                 
+ 
+   
 .withProperty(Pr
@@ -3418,32 +3418,36 @@
                 
+    
 .withRelevancy(r
@@ -3484,32 +3484,36 @@
                 
+  
+  
 .withProperty(Pr
@@ -3596,16 +3596,20 @@
         
+    
 .goEvide
@@ -3613,16 +3613,20 @@
 idence)%0A
+    
         
@@ -4004,45 +4004,8 @@
 ate 
-RawEvidenceNamedPresetColumnsBuilder.
 RawE
@@ -4069,45 +4069,8 @@
 rn (
-RawEvidenceNamedPresetColumnsBuilder.
 RawE
@@ -4169,16 +4169,24 @@
         
+     
+   
 .createW
@@ -4216,24 +4216,32 @@
             
+        
 .withGoEvide
@@ -4259,24 +4259,32 @@
             
+       
+ 
 .withIdPosit
@@ -4290,16 +4290,24 @@
 tion(0)%0A
+        
         
@@ -4340,16 +4340,24 @@
 tion(3)%0A
+        
         

Add import to clean up code.