import logging


def set_up_ignore(ignore, all_df, exps_df, minSampleReads, meta_ix=7, dbg_prnt=False):
    """ Setting up the index (columns of all.poolcount) names to avoid doing analysis
    Args:
        ignore: list of str with sample-index name to ignore (could have len 0)
        all_df: Data frame of all.poolcount
        exps_df: Data frame of experiments file
            Must contain cols: 'name', 'Drop'
            
        minSampleReads: int
        meta_ix: Start of where the indeces become sample/index names
    
    Returns:
        all_df, exps_df, ignore (list<str>, where str is name of indeces
                                we are ignoring)

    Description: 
            We update the experiments to ignore by performing the following tests:
            1. We take all the columns of experiments in all_df (ignoring metadata columns), 
                and take the sum over each column. We check, for each experiment,
                that the sum is greater than the value 'minSampleReads'. In other words,
                we take the sum of all the reads over all the strains for each
                experiment and check if there were enough of them, enough of them
                meaning the number is greater than the number in minSampleReads.
            2. If the Drop column is True in exps_df then we ignore that column.
                For each row in the experiments file, we have a column called 
                'Drop', and if it is indicated to be True, then we ignore that
                row. Within the row, there is also the column 'name', which indicates
                the name of the experiment to ignore.
            3. For each experiment name we choose to ignore, we remove the 
                column from all_df (where they are column names) & the row from
                exps_df (where the name is under the column 'name')
            Note that we update BOTH exps_df and all_df here.

    """
    # Creating a list to ignore out of the all.poolcount indexes 
    #   (names are updated though?)
    if len(ignore) == 0: 
        logging.info("Length of ignore list is 0") 
        # metacol is ignored 
        # We select all the columns related to experiments
        # And get the sum over the columns
        tot = all_df.iloc[:,meta_ix:].sum(axis=0)
        # We figure out the columns for which the sum of barcodes
        # found is less than minSampleReads
        ignore = []
        for c in tot.keys():
            if tot[c] < minSampleReads:
                ignore.append(c)
                logging.info(f"Ignoring experiment name: {c}."
                             f"Sum of reads: {tot[c]}")

    # The 'Drop' column means if Drop=TRUE then ignore sets column
    for ix, val in exps_df['Drop'].items():
        if bool(val):
            if exps_df['name'][ix] not in ignore:
                ignore.append(exps_df['name'][ix])

    # updating the data frames
    if(len(ignore) > 0):
        print("Ignoring " + ", ".join(ignore))
        # List of booleans related to rows with values that aren't ignored
        exps_keep =  [(not (val in ignore)) for ix, val in exps_df['name'].items()]
        if dbg_prnt:
            print("Pre removal:")
            print(exps_df['name'])
            print(exps_keep)
        new_exps_df = exps_df[exps_keep]
        if dbg_prnt:
            print("Post removal:")
            print(new_exps_df['name'])

        all_drop = [x for x in ignore if x in all_df]
        if dbg_prnt:
            print("all_drop:")
            print(all_drop)
        all_df = all_df.drop(labels=all_drop, axis=1)

        return [all_df, new_exps_df]
    else:
        print("Not ignoring any samples")

    return [all_df, exps_df]