/*
 * Copyright (c) 2014-2015, Bolotin Dmitry, Chudakov Dmitry, Shugay Mikhail
 * (here and after addressed as Inventors)
 * All Rights Reserved
 *
 * Permission to use, copy, modify and distribute any part of this program for
 * educational, research and non-profit purposes, by non-profit institutions
 * only, without fee, and without a written agreement is hereby granted,
 * provided that the above copyright notice, this paragraph and the following
 * three paragraphs appear in all copies.
 *
 * Those desiring to incorporate this work into commercial products or use for
 * commercial purposes should contact the Inventors using one of the following
 * email addresses: chudakovdm@mail.ru, chudakovdm@gmail.com
 *
 * IN NO EVENT SHALL THE INVENTORS BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
 * SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS,
 * ARISING OUT OF THE USE OF THIS SOFTWARE, EVEN IF THE INVENTORS HAS BEEN
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * THE SOFTWARE PROVIDED HEREIN IS ON AN "AS IS" BASIS, AND THE INVENTORS HAS
 * NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR
 * MODIFICATIONS. THE INVENTORS MAKES NO REPRESENTATIONS AND EXTENDS NO
 * WARRANTIES OF ANY KIND, EITHER IMPLIED OR EXPRESS, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A
 * PARTICULAR PURPOSE, OR THAT THE USE OF THE SOFTWARE WILL NOT INFRINGE ANY
 * PATENT, TRADEMARK OR OTHER RIGHTS.
 */

package com.milaboratory.mageri.pipeline.analysis;

import com.milaboratory.mageri.core.PipelineBlock;
import com.milaboratory.mageri.core.input.PreprocessorParameters;
import com.milaboratory.mageri.pipeline.RuntimeParameters;
import com.milaboratory.mageri.pipeline.input.Input;
import com.milaboratory.mageri.preprocessing.DemultiplexParameters;
import com.milaboratory.mageri.preprocessing.CheckoutProcessor;
import com.milaboratory.mageri.preprocessing.PAdapterExtractor;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class PreprocessorFactory extends PipelineBlock {
    private final DemultiplexParameters demultiplexParameters;
    private final PreprocessorParameters preprocessorParameters;
    private final Map<SampleGroup, Preprocessor> preprocessorBySampleGroup = new HashMap<>();

    public PreprocessorFactory(DemultiplexParameters demultiplexParameters, PreprocessorParameters preprocessorParameters) {
        super("checkout");
        this.demultiplexParameters = demultiplexParameters;
        this.preprocessorParameters = preprocessorParameters;
    }

    public Preprocessor create(Input input, SampleGroup sampleGroup) throws IOException, InterruptedException {
        return create(input, sampleGroup, RuntimeParameters.DEFAULT);
    }

    public Preprocessor create(Input input, SampleGroup sampleGroup, RuntimeParameters runtimeParameters) throws IOException, InterruptedException {
        Preprocessor preprocessor = new Preprocessor(input, sampleGroup,
                demultiplexParameters,
                preprocessorParameters,
                runtimeParameters);

        preprocessorBySampleGroup.put(sampleGroup, preprocessor);

        return preprocessor;
    }
    
    public Preprocessor getPreprocessor(SampleGroup sampleGroup){
        return preprocessorBySampleGroup.get(sampleGroup);
    }

    @Override
    public String getHeader() {
        return "sample.group\tsample.name\tmaster.found\tslave.found\tmaster.first\tmig.size.threshold";
    }

    @Override
    public String getBody() {
        StringBuilder stringBuilder = new StringBuilder();
        for (Preprocessor preprocessor : preprocessorBySampleGroup.values()) {
            for (Sample sample : preprocessor.getSampleGroup().getSamples()) {
                CheckoutProcessor checkoutProcessor = preprocessor.getCheckoutProcessor();
                String sampleName = sample.getName();
                stringBuilder.append(preprocessor.getSampleGroup().getName()).append("\t").
                        append(sampleName).append("\t").
                        append(checkoutProcessor.getMasterCounter(sampleName)).append("\t").
                        append(checkoutProcessor instanceof PAdapterExtractor ?
                                ((PAdapterExtractor) checkoutProcessor).getSlaveCounter(sampleName) : "0").append("\t").
                        append(checkoutProcessor.getMasterFirstRatio()).append("\t").
                        append(preprocessor.getOverSeq(sampleName)).append("\n");
            }
        }
        return stringBuilder.toString();
    }
}

@@ -1812,37 +1812,33 @@
 cessing.
-DemultiplexParameters
+CheckoutProcessor
 ;%0Aimport
@@ -1876,33 +1876,37 @@
 cessing.
-CheckoutProcessor
+DemultiplexParameters
 ;%0Aimport
@@ -1945,30 +1945,31 @@
 essing.P
-AdapterExtract
+CheckoutProcess
 or;%0A%0Aimp
@@ -3232,20 +3232,16 @@
 ;%0A    %7D%0A
-    
 %0A    pub
@@ -3297,16 +3297,17 @@
 leGroup)
+ 
 %7B%0A      
@@ -3926,24 +3926,106 @@
 .getName();%0A
+                boolean paired = checkoutProcessor instanceof PCheckoutProcessor;%0A
             
@@ -4289,56 +4289,119 @@
 end(
-checkoutProcessor instanceof PAdapterExtractor ?
+paired ?%0A                                ((PCheckoutProcessor) checkoutProcessor).getSlaveCounter(sampleName) :
 %0A   
@@ -4432,29 +4432,8 @@
     
- ((PAdapterExtractor)
  che
@@ -4442,34 +4442,34 @@
 outProcessor
-)
 .get
-Slave
+Master
 Counter(samp
@@ -4479,14 +4479,8 @@
 ame)
- : %220%22
 ).ap
@@ -4514,32 +4514,95 @@
          append(
+paired ?%0A                                ((PCheckoutProcessor) 
 checkoutProcesso
@@ -4594,32 +4594,33 @@
 heckoutProcessor
+)
 .getMasterFirstR
@@ -4625,16 +4625,54 @@
 tRatio()
+ :%0A                                %221%22
 ).append

Fix preprocessor factory output