import os

import pandas as pd

base_dir = os.getcwd()
DATA_PATH = os.path.join(base_dir, "_data")
MODEL_PATH = os.path.join(base_dir, "_models")


def remove_file(path):
    try:
        os.remove(path)
    except Exception as e:
        print(e)


def save_df_to_csv(df, file):
    print(df.head(3))
    full_path = os.path.join(DATA_PATH, file)
    print(full_path)
    remove_file(full_path)
    df.to_csv(full_path, sep="|", index=False)


def save_dict_to_csv(d, file):
    print(len(d))
    full_path = os.path.join(DATA_PATH, file)
    print(full_path)
    remove_file(full_path)
    with open(full_path, "w") as f:
        for (k, v) in d.items():
            if v:
                f.write("{}|{}\n".format(k, ",".join([str(x) for x in v])))


def get_df(file, sep="|", columns=[]):
    full_path = os.path.join(DATA_PATH, file)
    # full_path = os.path.join("/Users/offic/work/GM/strategy_embedding/_data", file) # TODO
    print(full_path)
    if columns:
        df = pd.read_csv(full_path, sep=sep, names=columns)
    else:
        df = pd.read_csv(full_path, sep=sep)
    print(df.shape)
    return df
