import logging


def WriteToPoolCount(pool_d, rcbc_list,
                    counts, nSamples, out_prefix, indexes):

    """
    Inputs:
        pool_d (dict)
            rcbarcode (s) => [barcode (str), scaffold (str), strand (str), pos (i)]
        rcbc_list (list<str>): We get a list of all the rcbarcodes as they are
                             in the pool file (dicts might lose ordering) and
                             this way we can print them out in the same order later.
        counts (dict)
            rcbarcode to vector of counts
        nSamples (int)
        out_prefix: (str) Filepath to out file prefix (poolcount, colsum, ignore)
        indexes: (list<str>) List of index names

    Description:
        Note that no Thresholds are employed within this function.
    """
    
    out_fp = out_prefix + ".poolcount"
    PC_FH = open(out_fp, "w")

    #First line of poolcount file: barcode, etc. tab separated.
    PC_FH.write("\t".join(["barcode","rcbarcode", "scaffold", "strand", 
        "pos", "\t".join(indexes)]) + "\n")

    #sorted_keys = special_sorted_pool_keys(pool_d) 
    
    for rcbarcode in rcbc_list:

        barcode_list = pool_d[rcbarcode]

        if rcbarcode in counts:
            # list of counts related to indexes
            count_num_list = counts[rcbarcode]
        else:
            count_num_list = None

        out_list = [ barcode_list[0], rcbarcode, barcode_list[1], 
                barcode_list[2], str(barcode_list[3]) ]

        if count_num_list is not None:
            if len(count_num_list) < nSamples:
                count_num_list += [0] * (nSamples-len(count_num_list))
            out_list += [str(x) for x in count_num_list]
        else:
            out_list += ["0"]*nSamples

        
        PC_FH.write("\t".join(out_list) + "\n")

    PC_FH.close()
    logging.info("Wrote poolcount to " + out_fp)

    return None