package function.cohort.base;

import function.variant.base.RegionManager;
import function.variant.base.Variant;
import global.Data;
import global.Index;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import utils.CommonCommand;
import utils.DBManager;
import utils.ErrorManager;

/**
 * @author nick, qwang
 */
public class DPBinBlockManager {

    public static final int DP_BIN_BLOCK_SIZE = 1000;

    private static ArrayList<SampleDPBin> currentBlockList = new ArrayList<>();
    private static int currentBlockId = Data.INTEGER_NA;

    private static HashMap<Character, Short> dpBin = new HashMap<>();
    private static HashMap<Short, Byte> dpBinIndex = new HashMap<>();

    private static final HashMap<String, PreparedStatement> preparedStatement4BlockMap = new HashMap<>();
    
    public static void init() {
        if (CommonCommand.isNonDBAnalysis) {
            return;
        }
        
        dpBin.put('b', Data.SHORT_NA);
        dpBin.put('c', (short) 10);
        dpBin.put('d', (short) 10);
        dpBin.put('e', (short) 10);
        dpBin.put('f', (short) 10);
        dpBin.put('g', (short) 10);

        dpBinIndex.put(Data.SHORT_NA, Data.BYTE_NA);
        dpBinIndex.put((short) 10, Index.DP_BIN_10);
        
        for (String chr : RegionManager.ALL_CHR) {
            String sql = "SELECT sample_id, DP_string FROM DP_bins_chr" + chr + "," + SampleManager.TMP_SAMPLE_ID_TABLE
                    + " WHERE block_id=? AND sample_id = input_sample_id";

            preparedStatement4BlockMap.put(chr, DBManager.initPreparedStatement(sql));
        }
    }

    public static void add(SampleDPBin sampleDPBin) {
        currentBlockList.add(sampleDPBin);
    }

    public static void initCarrierAndNonCarrierByDPBin(Variant var,
            HashMap<Integer, Carrier> carrierMap,
            HashMap<Integer, NonCarrier> noncarrierMap) {
        int posIndex = var.getStartPosition() % DP_BIN_BLOCK_SIZE;

        int blockId = Math.floorDiv(var.getStartPosition(), DP_BIN_BLOCK_SIZE);

        if (blockId == currentBlockId) {
            for (SampleDPBin sampleDPBin : currentBlockList) {
                Carrier carrier = carrierMap.get(sampleDPBin.getSampleId());

                if (carrier != null) {
                    if (carrier.isValid()) {
                        carrier.setDPBin(sampleDPBin.getDPBin(posIndex));
                        carrier.applyCoverageFilter();
                        carrier.applyQualityFilter(var.isSNV());
                    }
                } else {
                    NonCarrier noncarrier = new NonCarrier(sampleDPBin.getSampleId(),
                            sampleDPBin.getDPBin(posIndex));

                    noncarrier.applyCoverageFilter();

                    if (noncarrier.isValid()) {
                        noncarrierMap.put(noncarrier.getSampleId(), noncarrier);
                    }
                }
            }
        } else {
            currentBlockId = blockId;
            currentBlockList.clear();

            initBlockDPBin(carrierMap, noncarrierMap, var, posIndex, blockId);
        }
    }

    public static void initBlockDPBin(
            HashMap<Integer, Carrier> carrierMap,
            HashMap<Integer, NonCarrier> noncarrierMap,
            Variant var,
            int posIndex,
            int blockId) {
        try {           
            PreparedStatement preparedStatement = preparedStatement4BlockMap.get(var.getChrStr());
            preparedStatement.setInt(1, blockId);
            ResultSet rs = preparedStatement.executeQuery();
            while (rs.next()) {
                NonCarrier noncarrier = new NonCarrier(rs.getInt("sample_id"), rs.getString("DP_string"), posIndex);

                Carrier carrier = carrierMap.get(noncarrier.getSampleId());

                if (carrier != null) {
                    if (carrier.isValid()) {
                        carrier.setDPBin(noncarrier.getDPBin());
                        carrier.applyCoverageFilter();
                        carrier.applyQualityFilter(var.isSNV());
                    }
                } else {
                    noncarrier.applyCoverageFilter();

                    if (noncarrier.isValid()) {
                        noncarrierMap.put(noncarrier.getSampleId(), noncarrier);
                    }
                }
            }

            rs.close();
        } catch (Exception e) {
            ErrorManager.send(e);
        }
    }

    public static short getCoverageByBin(Character bin) {
        return dpBin.get(bin);
    }

    public static byte getCoverageByBinIndex(short bin) {
        return dpBinIndex.get(bin);
    }

    public static HashMap<Character, Short> getCoverageBin() {
        return dpBin;
    }
}

@@ -848,21 +848,17 @@
 ap%3C%3E();%0A
-    
 %0A
+
     publ
@@ -956,25 +956,17 @@
       %7D%0A
-        
 %0A
+
         
@@ -1283,25 +1283,17 @@
 IN_10);%0A
-        
 %0A
+
         
@@ -2289,57 +2289,8 @@
 ) %7B%0A
-                    if (carrier.isValid()) %7B%0A    
     
@@ -2367,36 +2367,32 @@
                 
- 
-   
 carrier.applyCov
@@ -2398,36 +2398,32 @@
 verageFilter();%0A
-    
                 
@@ -2467,38 +2467,16 @@
 SNV());%0A
-                    %7D%0A
         
@@ -3304,27 +3304,16 @@
    try %7B
-           
 %0A       
@@ -3785,57 +3785,8 @@
 ) %7B%0A
-                    if (carrier.isValid()) %7B%0A    
     
@@ -3854,36 +3854,32 @@
                 
- 
-   
 carrier.applyCov
@@ -3885,36 +3885,32 @@
 verageFilter();%0A
-    
                 
@@ -3954,38 +3954,16 @@
 SNV());%0A
-                    %7D%0A
         

check covered sample (include qc failed carriers) - issue 8194