import os
import requests
from bs4 import BeautifulSoup
from _gen_definitions import *

stupid_template = """
class {NAME}(Enum):
{DOCS}{BLAHBLAH}
"""

generated_prefix = "./src/enums/"
def magic_text(text):
    return str(text).upper().replace("-", "_").replace(".", "_").replace(" ", "").replace("/", "_")


def gen_from_list(name, lst, docs = None):
    totalstring = []
    for i in lst:
        #Fixes an issue with effects. Its not a pretty one I don't think
        if str(i).find("*") != -1:
            continue
        totalstring.append("    {} = \"{}\"".format(magic_text(i), i))
    doc_write=""
    if docs:
        doc_write = "    \"\"\"{}\"\"\"\n".format(docs)
    return stupid_template.format(NAME=name, DOCS=doc_write, BLAHBLAH="\n".join(totalstring))


def grab_page(address):
    r = requests.get(address)
    if r.status_code == 200:
        return r.content
    else:
        print("Bad url", address)
        return ""


def grab_class_list(content):
    page = BeautifulSoup(content, features="html.parser")
    table_elements = page.find("table").find_all('td', class_="text-violet-600")
    return [i.contents[0] for i in table_elements]


def grab_description(content):
    page = BeautifulSoup(content, features="html.parser")
    description = page.find("p", class_="mt-1 text-lg text-gray-500").contents[0]
    return description


def full_grab_process(page):
    content = grab_page(page)
    return (grab_class_list(content), grab_description(content))


def name_from_url(url):
    tail_name = os.path.split(url)[1]
    return "".join([i.capitalize() for i in tail_name.split("-")])


def generate_file_from_dict(path, dict):
    print("==={}===".format(path))
    with open(path, "w") as f:
        f.write("\"\"\"\nWARNING: THIS FILE IS AUTOMATICALLY GENERATED FROM _gen_everything.py\n\"\"\"")
        f.write("\n")
        f.write("from enum import Enum")
        f.write("\n")
        for name, lst in dict.items():
            print(name)
            f.write(gen_from_list(name, lst))
            f.write("\n")


def generate_file_from_web(path, lst):
    print("==={}===".format(path))
    with open(path, "w") as f:
        f.write("\"\"\"\nWARNING: THIS FILE IS AUTOMATICALLY GENERATED FROM _gen_everything.py\n\"\"\"")
        f.write("\n")
        f.write("from enum import Enum")
        f.write("\n")
        for c in lst:
            url = c
            name = name_from_url(c)
            print(name)
            fp = full_grab_process(url)
            docs = fp[1] + "\n    " + "Tailwind documentation URL: {}".format(url) 
            f.write(gen_from_list(name, fp[0], docs=docs))
            f.write("\n")


def p(st):
    return os.path.join(generated_prefix, st)


all_generates = {
    "layout": lambda: generate_file_from_web(p("layout.py"), Layout),
    "container_layouts": lambda: generate_file_from_web(p("container_layouts.py"), ContainerLayouts),
    "spacing": lambda: generate_file_from_web(p("spacing.py"), Spacing),
    "typography": lambda: generate_file_from_web(p("typography.py"), Typography),
    "backgrounds": lambda: generate_file_from_web(p("backgrounds.py"), Backgrounds),
    "borders": lambda: generate_file_from_web(p("borders.py"), Borders),
    "effects": lambda: generate_file_from_web(p("effects.py"), Effects),
    "filters": lambda: generate_file_from_web(p("filters.py"), Filters),
    "tables": lambda: generate_file_from_web(p("tables.py"), Tables),
    "transitions": lambda: generate_file_from_web(p("transitions.py"), Transitions),
    "transforms": lambda: generate_file_from_web(p("transforms.py"), Transforms),
    "interactivity": lambda: generate_file_from_web(p("interactivity.py"), Interactivity),
    "svg": lambda: generate_file_from_web(p("svg.py"), Svg),
    "accessibility": lambda: generate_file_from_web(p("accessibility.py"), Accessibilty),
    "metaclasses": lambda: generate_file_from_dict(p("metaclasses.py"), Metaclasses),
}


def main():
    for _k, v in all_generates.items():
        v()


if __name__ == "__main__":
    main()