import pandas as pd
from mappingsdict import mappings
from groupslist import GROUPSDICT
from datetime import date
import fnmatch

MAINLIST = mappings
DATE = date.today()


def oldfunc():
    df = pd.DataFrame.from_records(mappings)
    df.to_csv(f"~/Downloads/Anthology_mappings_{date}.csv")


def newfunc():
    masterlist = []
    for domdict in MAINLIST:
        for domains in domdict["domains"]:
            masterlist.append(domains)
    nodupes = set(masterlist)
    find_matches(nodupes)


def find_matches(nodupes):
    formatted_mapping = []
    for domain in nodupes:
        domaindict = {}
        group_list = []
        for domdict in MAINLIST:
            if domain in domdict["domains"]:
                group_list.append(domdict["uuid"])
            groups_names = {v: k for k, v in GROUPSDICT.items()}
            groups_names = [groups_names.get(item, item) for item in group_list]
            names = fnmatch.filter(groups_names, "*(??)")
        domaindict = {"domain": domain, "groups": names}
        formatted_mapping.append(domaindict)
    to_file(formatted_mapping)


def to_file(formatted_mapping):
    df = pd.DataFrame()
    #df = pd.DataFrame(formatted_mapping)
    df[[    "domain",
            "groups_1",
            "groups_2",
            "groups_3",
            "groups_4",
            "groups_5",
            "groups_6",
            "groups_7",
            "groups_8",
            "groups_9",
            "groups_10",
            "groups_11",
            "groups_12",
            "groups_13",
            "groups_14",
            "groups_15",
            "groups_16",
            "groups_17",
            "groups_18",
            "groups_19",
            "groups_20",
            "groups_21",
            "groups_22",
            "groups_23",
            "groups_24",
            "groups_25",
            "groups_26",
            "groups_27",
            "groups_28",
            "groups_29",
            "groups_30",
            "groups_31",
            "groups_32",
            "groups_33",
            "groups_34",
            "groups_35",
            "groups_36",
            "groups_37",
            "groups_38",
            "groups_39",
            "groups_40",
            "groups_41",
            "groups_42",
            "groups_43",
            "groups_44",
            "groups_45",
            "groups_46",
            "groups_47",
            "groups_48",
            "groups_49",
            "groups_50",
            "groups_51",
            "groups_52",
            "groups_53",
            "groups_54",
            "groups_55",
            "groups_56",
            "groups_57",
            "groups_58",
            "groups_59",
            "groups_60",
            "groups_61",
            "groups_62",
            "groups_63",
            "groups_64",
            "groups_65",
            "groups_66",
            "groups_67",
            "groups_68",
            "groups_69",
            "groups_70",
            "groups_71",
            "groups_72",
            "groups_73",
            "groups_74",
            "groups_75",
            "groups_76",
            "groups_77",
            "groups_78",
            "groups_79",
            "groups_80",
            "groups_81",
            "groups_82",
            "groups_83",
            "groups_84",
            "groups_85",
            "groups_86",
            "groups_87",
            "groups_88",
            "groups_89",
            "groups_90",
            "groups_91",
            "groups_92",
            "groups_93",
            "groups_94",
            "groups_95",
            "groups_96",
            "groups_97",
            "groups_98",
            "groups_99",
            "groups_100",
            "groups_101",
            "groups_114",
            "groups_115",
            "groups_116",
            "groups_117",
            "groups_118"]]
    print(df)
    # = pd.DataFrame(df.groups.tolist(), index=df.index)
    # df3 = pd.DataFrame(df['domains'].to_list())
    # df.to_csv(f"~/Downloads/Anthology_mappings_{DATE}.csv")


if __name__ == "__main__":
    newfunc()
