from glob import glob
import pickle
import pandas as np
import os

class Analyze:

    @staticmethod
    def get_from_column(df, col_name, value):
        c = df[df[col_name] == value]


    #
    # LIST AVAILABLE OUTPUT DIRECTORIES
    #
    @staticmethod
    def list_output_directories(selected=None,
                                which="output*"):
        output_directories = sorted(glob(which))
        print(49 * "=")
        print("Output directories")
        for d in output_directories:
            if selected in d:
                c = "-->"
            else:
                c = "  -"

            print(f"    {c}", d)
        print()
        print(49 * "=")
        print()

    @staticmethod
    def list_data(which):
        return sorted(glob(f"{which}/*.pkl"))

    @staticmethod
    def find_cases_death(names):
        for name in names:
            b = os.path.basename(name)
            if b.startswith("Cases-Death"):
                return name

        raise ValueError("no cases found")
        return None

    @staticmethod
    def find_top(n, names, exclude=None, mark='Cases-Death', which='cum_error_cases'):
        a = None
        for name in names:
            df = pickle.load(open(name, "rb"))
            df = df.sort_values(by=[which])

            df = df.head(n)
            # display(df)

            if a is None:
                a = df
            else:
                a = np.concat([a, df])

        a = a.sort_values(by=[which])
        a['place'] = range(0, len(a))
        #
        # Mark all Cases-Death
        #
        a['first'] = ""
        a.loc[a['fields'] == mark, 'first'] = "1"

        return a
